Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
61 lines (53 loc) · 2.81 KB
/
Copy pathMain.java
File metadata and controls
61 lines (53 loc) · 2.81 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("На скольких человек необходимо разделить счёт?");
int numberOfPersons = getPersonNum(scanner);
Calculate calculate = new Calculate();
createNewProducts(scanner, calculate);
double sum = calculate.getSumForEveryHuman(numberOfPersons);
System.out.printf("Добавленные товары: \n%s\nКаждый человек должен заплатить по: %.2f %s",
calculate.allProducts, sum, Formatter.getRubleInTheCorrectCase(sum));
scanner.close();
}
public static int getPersonNum(Scanner scanner) {
while (true) {
int numberOfPersons = Utils.parseStringToInteger(scanner.next());
if (numberOfPersons == 1) {
System.out.println("Нет необходимости считать сумму на одного гостя. " +
"Пожалуйста повторите ввод.");
} else if (numberOfPersons <= 0) {
System.out.println("Некорректный ввод. Пожалуйста повторите попытку.");
} else {
return numberOfPersons;
}
}
}
public static void createNewProducts(Scanner scanner, Calculate calculate) {
while (true) {
System.out.println("Для добавления товара введите любой символ, " +
"для завершения добавления товаров введите \"завершить\"");
String userInput = scanner.next();
if (userInput.equalsIgnoreCase("завершить")) {
break;
} else {
System.out.println("Введите название товара");
scanner.nextLine();
String productName = scanner.nextLine();
System.out.println("Введите стоимость товара. Стоимость должна быть в формате" +
" рубли.копейки, например 10.45 или 11.40.");
double productPrice;
while (true) {
productPrice = Utils.parseStringToDouble(scanner.next());
if (productPrice <= 0) {
System.out.println("Некорректный ввод. Пожалуйста повторите попытку.");
} else {
break;
}
}
calculate.addProduct(new Product(productName, productPrice));
}
}
}
}