forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
62 lines (61 loc) · 2.85 KB
/
Calculator.java
File metadata and controls
62 lines (61 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.ArrayList;
import java.util.Scanner;
public class Calculator {
private ArrayList<Item> items;
private int numberOfPeople;
public Calculator() { items = new ArrayList<>();}
public void start() {
Scanner scanner = new Scanner(System.in);
boolean validInput = false;
while (!validInput) {
System.out.println("На скольких человек необходимо разделить счёт:");
String inmput = scanner.nextLine().trim();
if (inmput.matches("\\d+")) {
numberOfPeople = Integer.parseInt(inmput);
if (numberOfPeople <= 1) {
System.out.println("Ошибка: Введите корректное количество гостей, больше одного.");
} else {
validInput = true;
}
}
}
while (true) {
System.out.println("Введите название товара и его стоимость в формате 'название стоимость': например, пиво 58.99\nЛибо введите команду 'Завершить' для того, чтоб завершить процесс добавления товаров.");
String line = scanner.nextLine().trim();
if (line.equalsIgnoreCase("завершить")) {
break;
}
String[] parts = line.split(" ");
if (parts.length != 2 || !isValidPrice(parts[1])) {
System.out.println("Ошибка: Неккоректный формат ввода или некорректная сумма товара.");
continue;
}
String name = parts[0];
double price = Double.parseDouble(parts[1]);
items.add(new Item(name, price));
System.out.println("Товар успешно добавлен.");
System.out.println("Хотите добавить еще один товар?");
}
System.out.println("Добавленные товары:");
for(Item item : items) {
System.out.println(item.getName() + " - " + item.getPrice());
}
double total = 0;
for (Item item : items) {
total += item.getPrice();
}
double perPerson = total / numberOfPeople;
String rublesString = Formatter.formatRubles(perPerson);
System.out.println("Каждый человек должен заплатить: " + rublesString);
}
private boolean isValidPrice(String priceStr) {
String[] parts = priceStr.split("\\.");
if (parts.length !=2) {
return false;
}
if (!parts[0].matches("\\d+") || !parts[1].matches("\\d{2}")){
return false;
}
return true;
}
}