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
69 lines (62 loc) · 2.19 KB
/
Main.java
File metadata and controls
69 lines (62 loc) · 2.19 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int guests = getGuests(scanner);
Calculator calc = new Calculator();
while(true){
System.out.println("Введите товар");
String name = scanner.next();
System.out.println("Введите стоимость, цену введите дробную с точкой");
scanner.nextLine();
Double cost = insertDouble(scanner);
calc.listItems.add(new Pair(name, cost));
System.out.println("Товар успешно добавлен, завершить подсчет?");
String answer = scanner.next();
if(answer.toLowerCase().contains("завершить")){
break;
}
}
calc.printResult(guests);
scanner.close();
}
public static int getGuests(Scanner scanner){
int count;
while(true){
System.out.println("Сколько гостей?");
count = insertCount(scanner);
if(count <= 1){
System.out.println("Некорректное значение для подсчета");
}
else{
break;
}
}
return count;
}
public static int insertCount(Scanner scanner){
int count;
try{
count = Integer.parseInt(scanner.nextLine());
}
catch (Exception e){
System.out.println("Введите целочисленное");
count = insertCount(scanner);
}
return count;
}
public static Double insertDouble(Scanner scanner){
double count;
try{
count = Double.parseDouble(scanner.nextLine());
if(count < 0){
throw new Exception("Negative Double");
}
}
catch (Exception e){
System.out.println("Введите положительное дробное число с точкой");
count = insertDouble(scanner);
}
return count;
}
}