Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
95 lines (73 loc) · 3.52 KB
/
Copy pathMain.java
File metadata and controls
95 lines (73 loc) · 3.52 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
84
85
86
87
88
89
90
91
92
93
94
95
import java.util.Scanner;
public class Main {
static Scanner scanner;
static Calculator calculator;
static int peoples = 0;
static boolean isPeoplesSetCorrectly = true;
static boolean isProductPurchased = false;
static String stopProductPurchased = "Завершить";
public static void main(String[] args) {
scanner = new Scanner(System.in);
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
while (isPeoplesSetCorrectly) {
System.out.println("На сколько человек делим счет?");
try {
peoples = scanner.nextInt();
if (peoples == 1) {
showErrorMessage("Количество человек должно быть более 1.");
}
if (peoples < 1) {
showErrorMessage("Маловато будет. Это менее 1.");
}
if (peoples > 1) {
isPeoplesSetCorrectly = false;
isProductPurchased = true;
}
} catch (Exception e) {
showErrorMessage(e.getMessage());
}
}
calculator = new Calculator();
calculator.setPeoples(peoples);
while (isProductPurchased) {
System.out.println("Ведите название товара.");
String productName;
double productPrice;
productName = scanner.next();
boolean isPriceSetCorrectly = false;
while (!isPriceSetCorrectly) {
System.out.println("Введите цену товара в формате <рубли.копейки>.");
String stringProductPrice = scanner.next().trim();
if (stringProductPrice.contains(",")) {
stringProductPrice = stringProductPrice.replace(",", ".");
}
try {
productPrice = Double.parseDouble(stringProductPrice);
if (productPrice <= 0.0) {
showErrorMessage("Цена товара слишком мала. " + productPrice);
break;
} else {
Product product = new Product(productName, productPrice);
System.out.println(calculator.addProduct(product));
isPriceSetCorrectly = true;
}
} catch (Exception e) {
System.out.println(e.getMessage());
showErrorMessage("Цена должна быть указана в виде <xx.yy>." + e.getMessage());
}
}
System.out.println("Желаете добавить еще товар? Для завершения наберите <Завершить>.");
String buyerWantsBuy = scanner.next();
if (buyerWantsBuy.trim().equalsIgnoreCase(stopProductPurchased)) {
isProductPurchased = false;
}
}
System.out.println(calculator.getAllProductNames());
System.out.println(calculator.getFinalPrice());
System.out.println(calculator.getPriceForOnePeople());
}
static void showErrorMessage(String errorString) {
System.out.println("Ошибка! " + errorString);
}
}