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
58 lines (50 loc) · 2.23 KB
/
Main.java
File metadata and controls
58 lines (50 loc) · 2.23 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
import java.util.Scanner;
public class Main {
private static final Scanner scanner = new Scanner(System.in);
private static Calculator calculator = new Calculator();
public static void main(String[] args) {
System.out.println("Калькулятор счета приветствует вас. ");
int countPerson = getCountPerson();
getProducts();
calculator.printProductList();
calculator.splitCheck(countPerson);
}
public static int getCountPerson(){
int countPerson;
while(true) {
System.out.println("Введите количество человек. Используйте числа больше 1.");
String str = scanner.nextLine();
countPerson = str.matches("\\d+") ? Integer.parseInt(str) : 0;
if (countPerson > 1) {
break;
} else {
System.out.println("Вы ввели некорректное значение: " +
str +
". Введите число больше 1. Попробуйте еще раз.\n");
}
}
return countPerson;
}
public static void getProducts(){
while(true) {
System.out.println("Введите название товара или команду 'Завершить':");
String strName = scanner.nextLine();
if ( strName.equalsIgnoreCase("завершить")){
break;
}
else{
System.out.println("Введите стоимость товара в формате рр.кк:");
String strPrice = scanner.nextLine();
double price = strPrice.matches("\\d{1,}+(\\.\\d{2}+)") ? Double.parseDouble(strPrice) : -1;
if( price >= 0 ){
calculator.add(strName, price);
}
else{
System.out.println("Вы ввели некорректную стоимость товара: " +
strPrice +
". Товар не добавлен в список. Попробуйте еще раз.\n");
}
}
}
}
}