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
100 lines (69 loc) · 2.8 KB
/
Main.java
File metadata and controls
100 lines (69 loc) · 2.8 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
96
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int numberOfUsers = -1;
Scanner scanner = new Scanner(System.in); // Создаем экземпляр класса Scanner.
Calculator calculator = new Calculator();
numberOfUsers = getNumberOfPeople(scanner);
getProductsInfo(scanner, calculator);
calculator.showCheck(numberOfUsers);
scanner.close(); // Закрываем Scanner по завершении работы.
}
public static int getNumberOfPeople(Scanner scan) {
int numb = -1;
while (true) {
System.out.println("Введите, на скольких человек необходимо разделить счёт");
numb = getInt(scan);
if (numb > 1) {
break;
} else {
System.err.println("Некорректное число");
}
}
return numb;
}
public static void getProductsInfo(Scanner scan, Calculator calculator) {
while (true) {
String nameOfProduct = "";
float priceOfProduct = 0;
System.out.println("Введите название товара");
nameOfProduct = scan.next();
while (priceOfProduct <= 0) {
System.out.println("Введите цену товара");
priceOfProduct = getFloat(scan);
if (priceOfProduct > 0) {
calculator.add(priceOfProduct);
calculator.writeToTheList(nameOfProduct, priceOfProduct);
System.out.println("Товар успешно добавлен");
} else {
System.err.println("Некорректная цена!");
}
}
System.out.println("Продолжить ввод товаров? Введите \"Завершить\"," +
" если хотите окончить ввод товаров, или любой символ, если хотите продолжить ввод");
String command = scan.next();
if (command.equalsIgnoreCase("Завершить")) {
break;
}
}
}
public static float getFloat(Scanner scan) {
try {
float number = scan.nextFloat();
return number;
} catch (InputMismatchException e) {
scan.nextLine();
return 0;
}
}
public static int getInt(Scanner scan) {
try {
int number = scan.nextInt();
return number;
} catch (InputMismatchException e) {
scan.nextLine();
return 0;
}
}
}