Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.util.ArrayList;
import java.util.Scanner;

public class Calculator {
Scanner scanner = new Scanner(System.in);

public void calculate(int peopleCount) {
ArrayList<Order> orders = new ArrayList<>();
double sum = 0;
String name;
double price;
while (true) {
System.out.println("Введите название товара и сумму, в формате рубли.копейки, " +
"для завершения добавления товаров введите \"Завершить\"");
String input = scanner.nextLine();
if (input.equalsIgnoreCase("Завершить")) {
break;
} else if (input.contains("-")) {
System.out.println("Отрицательная сумма, введите корректную сумму");
continue;
} else {
try {
name = input.replaceAll("[\\d.]", "").trim();
price = Double.parseDouble(input.replaceAll("[^.\\d]", "").trim());
} catch (NumberFormatException e) {
System.out.println("Не введено наименование позиции или сумма.");
continue;
}
orders.add(new Order(name, price));
}
}
System.out.println("Добавленные товары:");
for (int i = 0; i < orders.size(); i++) {
sum += orders.get(i).price;
System.out.println(orders.get(i).name + " " + orders.get(i).price);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

orders.get(i) повторяется три раза, лучше взять один раз, записать в переменную и использовать её

}
double sumPerCount = sum / peopleCount;
System.out.println("Каждый человек должен заплатить " + String.format("%.2f", sumPerCount) + " "
+ pluralsCase(sumPerCount));
}

private static String pluralsCase(Double sum) {
int sumCase = sum.intValue();

if (sumCase % 10 == 1 && sumCase % 100 == 1) {
return "рубль";
} else if (sumCase % 10 >= 2 && sumCase % 10 <= 4 &&
(sumCase % 100 < 10 || sumCase % 100 >= 20)) {
return "рубля";
} else {
return "рублей";
}
}
}
25 changes: 22 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
Scanner scanner = new Scanner(System.in);
Calculator calculator = new Calculator();
int count = 0;

System.out.println("На какое количество людей должен быть разделен счет?");
while (count <= 1) {
String peopleCount = scanner.nextLine();
try {
count = Integer.parseInt(peopleCount);
} catch (NumberFormatException e) {
System.out.println("Количество нужно вводить в цифровом формате.");
count = 0;
continue;
}
if (count <= 1) {
System.out.println("Количество людей не может быть меньше двух.");
}
}

calculator.calculate(count);
}
}
12 changes: 12 additions & 0 deletions src/main/java/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@


public class Order {
String name;
Double price;

public Order(String name, Double price) {
this.name = name;
this.price = price;
}
}