Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
83 lines (77 loc) · 3.77 KB
/
Copy pathCalculator.java
File metadata and controls
83 lines (77 loc) · 3.77 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Calculator {
Scanner scanner = new Scanner(System.in);
Formatter formatter = new Formatter();
List<String> userProduct = new ArrayList<>();
double sumOfPrice;
int countFriends;
public void calcCountOfFriends() {
while (true) {
if (scanner.hasNextInt()) {
countFriends = scanner.nextInt();
if (countFriends == 1) {
System.out.println("Нет смысла рассчитывать! Платишь сам!");
System.exit(0);
} else if (countFriends <= 0 || countFriends > 1000000) {
System.out.println("Некорректные данные для расчетов или вас ооочень много! Попробуй ввести снова!");
scanner.nextLine();
} else {
scanner.nextLine();
break;
}
} else {
System.out.println("Некорректные данные для расчетов! Попробуй ввести снова!");
scanner.nextLine();
}
}
}
public void insertPriceProduct() {
while (true) {
System.out.println("Введи стоимость товара в формате ХХ.ХХ");
String insertPrice = scanner.nextLine();
double normalPrice;
if (insertPrice.isEmpty()) {
System.out.println("Цена пуста!");
} else {
try {
normalPrice = Double.parseDouble(insertPrice.replace(',', '.'));
if (normalPrice <= 0) {
System.out.println("Цена не может быть меньше или равна нулю!");
} else {
sumOfPrice = sumOfPrice + normalPrice;
System.out.print("Товар и цена успешно добавлены!");
break;
}
} catch (NumberFormatException e) {
System.out.println("Похоже вместо цены ты ввел буквы или символы, попробуй снова!");
}
}
}
}
public void insertProduct() {
while (true) {
System.out.println("Введи наименование товара!");
String insertProduct = scanner.nextLine();
if (insertProduct.equalsIgnoreCase("завершить")) {
break;
} else if (insertProduct.isEmpty()) {
System.out.println("Пустое наименование товара!");
} else {
userProduct.add(insertProduct);
insertPriceProduct();
System.out.println("\nХотите добавить еще? Чтобы завершить ввод - напиши 'завершить'!");
insertProduct = scanner.nextLine();
if (insertProduct.equalsIgnoreCase("завершить")) {
break;
} else {
continue;
}
}
}
System.out.printf("Добавленные товары:\n" + String.join("\n", userProduct));
System.out.printf("\nНа общую сумму: %.2f %s", sumOfPrice, formatter.formatRubles(sumOfPrice));
System.out.printf("\nНа каждого из %d по - %.2f %s", countFriends, sumOfPrice / countFriends, formatter.formatRubles(sumOfPrice / countFriends));
}//Обработка пустой строки с друзьями, Посмотреть по ТЗ
}