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
84 lines (76 loc) · 2.97 KB
/
Main.java
File metadata and controls
84 lines (76 loc) · 2.97 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
import java.util.Scanner;
public class Main {
private static Calculator calculator;
private static Scanner scanner;
public static void main(String[] args) {
scanner = new Scanner(System.in);
int numberOfPeople = askNumberOfPeople();
calculator = new Calculator(numberOfPeople);
askProductAndPrice();
showResult();
}
private static int askNumberOfPeople() {
System.out.println("Много вас?");
int numberOfPeople;
while (true) {
if (scanner.hasNextInt()) {
numberOfPeople = scanner.nextInt();
scanner.nextLine();
if (numberOfPeople > 1) {
return numberOfPeople;
}
System.out.println("Введите корректное количество людей (>1)");
} else {
System.out.println("Введите число >1");
}
}
}
public static void askProductAndPrice() {
while (true) {
System.out.println("Что кушать изволите?");
String product = scanner.nextLine();
System.out.println("Че почем?");
double price = askPrice();
calculator.addProduct(product, price);
System.out.println("Я все запомнил!");
System.out.println("Еще заказывать будете? Если нет, введите \"завершить\" ");
String input = scanner.nextLine();
if ("завершить".equalsIgnoreCase(input)) {
break;
}
}
}
private static double askPrice() {
while (true) {
double price;
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();
scanner.nextLine();
if (price <= 0) {
System.out.println("Бесплатный сыр только в мышеловке! Некорректная цена");
} else {
return price;
}
} else {
scanner.nextLine();
System.out.println("Введите цену в формате рубли.копейки");
}
}
}
private static void showResult() {
System.out.println("Добавленные товары:");
System.out.println(calculator.getProducts());
double personalBill = calculator.getPersonalBill();
double first = Math.floor(personalBill / 10) % 10;
double second = Math.floor(personalBill) % 10;
String rubName;
if (second == 1 && first != 1) {
rubName = "рубль";
} else if (second >= 2 && second <= 4 && first != 1) {
rubName = "рубля";
} else {
rubName = "рублей";
}
System.out.println(String.format("С носа : %.2f %s", personalBill, rubName));
}
}