forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
59 lines (58 loc) · 2.39 KB
/
Main.java
File metadata and controls
59 lines (58 loc) · 2.39 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
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
static String errorMessage = "Значение введено некорректно, пожалуйста, повторите ввод";
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int peopleCount = inputPeopleCount(scanner);
Calculator calculator = new Calculator(peopleCount);
inputProducts(scanner, calculator);
calculator.showProductsList();
calculator.calculateDividedSum();
calculator.showDividedSum();
}
public static int inputPeopleCount(Scanner scanner) {
int peopleCount;
System.out.println("Введите количество человек, на которое хотите разделить сумму: ");
while(true) {
try {
peopleCount = scanner.nextInt();
if (peopleCount <= 1) {
System.out.println(errorMessage);
} else break;
}
catch(InputMismatchException e) {
System.out.println(errorMessage);
scanner.nextLine();
}
}
scanner.nextLine();
return peopleCount;
}
public static void inputProducts(Scanner scanner, Calculator calculator) {
String productName;
double productPrice;
while(true) {
System.out.println("Введите название товара: ");
productName = scanner.nextLine();
if (productName.equalsIgnoreCase("завершить")) break;
try {
System.out.println("Введите стоимость товара: ");
productPrice = scanner.nextDouble();
if (productPrice <= 0) {
System.out.println(errorMessage);
scanner.nextLine();
continue;
}
}
catch (InputMismatchException e) {
System.out.println(errorMessage);
scanner.nextLine();
continue;
}
scanner.nextLine();
calculator.addProduct(new Product(productName, productPrice));
System.out.println("Хотите добавить ещё один товар? Если нет, введите \"завершить\"");
}
}
}