forked from Yandex-Practicum/Java-Module-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
91 lines (78 loc) · 3.1 KB
/
App.java
File metadata and controls
91 lines (78 loc) · 3.1 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
import java.util.Scanner;
import java.lang.Math;
public class App {
Scanner sc = new Scanner(System.in);
Calculator calculator = new Calculator();
public void run() {
int peopleCount = getPeopleCount();
readItems();
double sum = calculator.getSum();
String info = calculator.getItemsAsStr();
System.out.println(info);
double avg = sum / peopleCount;
double roundedValue = Math.floor(avg);
String template;
if (roundedValue % 100 > 9 && roundedValue % 100 < 21){
template = "%.2f рублей";
}
else if (roundedValue % 10 == 1) {
template = "%.2f рубль";
}
else if ( roundedValue % 10 > 1 && roundedValue % 10 < 5 ) {
template = "%.2f рубля";
}
else {
template = "%.2f рублей";
}
System.out.println(String.format(template, avg));
}
private int getPeopleCount(){
while(true){
try {
System.out.println("Укажите колличество человек");
String myString = sc.next();
int peopleCount = Integer.parseInt(myString);
if (peopleCount <= 1) {
System.out.println("Колличество человек должно быть больше 1");
continue;
}
else {
return peopleCount;
}
} catch (NumberFormatException e) {
System.out.println("Неревный ввод");
}
}
}
private void readItems(){
while (true) {
System.out.println("Введите название продукта");
String title = sc.next();
while (true) {
try {
System.out.println("Введите цену продукта");
String myString1 = sc.next();
double price = Double.parseDouble(myString1);
if (price > 0) {
Item item = new Item(title, price);
calculator.addItem(item);
System.out.println("Товар успешно добавлен");
System.out.println("Если это все продукты, то введите 'exit', иначе введите любой символ");
String finish = sc.next();
if (finish.equalsIgnoreCase("exit")) {
break;
} else {
continue;
}
} else {
System.out.println("Неревный ввод");
continue;
}
} catch (NumberFormatException e) {
System.out.println("Неревный ввод");
}
}
break;
}
}
}