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
72 lines (63 loc) · 2.76 KB
/
Main.java
File metadata and controls
72 lines (63 loc) · 2.76 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int visitorCount = getVisitorCount();
calculateGoods(visitorCount);
}
private static int getVisitorCount() {
int visitorCount;
while (true) {
System.out.println("На какое количество людей разделить чек?");
Scanner scanner = new Scanner(System.in);
String visitorCountString = scanner.next();
visitorCount = parseStringIntoInt(visitorCountString);
if (visitorCount == 1) {
System.out.println("Нет смысла использовать программу для 1-го человека");
} else if (visitorCount < 1) {
System.out.println("Вы ввели некорректное значение");
} else {
break;
}
}
return visitorCount;
}
private static void calculateGoods(int visitorCount) {
Calculator calculator = new Calculator();
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("Пожалуйста, введите название товара или команду 'завершить' чтобы закончить расчет");
String goodName = scanner.nextLine();
if (goodName.equalsIgnoreCase("завершить")) {
break;
}
System.out.println("Пожалуйста введите цену товара");
double goodPrice;
String goodPriceString = scanner.next();
goodPrice = parseStringIntoDouble(goodPriceString);
if (goodPrice < 0) {
System.out.println("Вы ввели некорректное значение");
} else {
Good good = new Good(goodName, goodPrice);
calculator.addGood(good, 1);
System.out.println("Товар был успешно добавлен");
}
}
calculator.showOrder();
double sumToPayPerPerson = calculator.getOrderSum() / visitorCount;
System.out.println("Каждый должен заплатить: " + String.format("%.2f", sumToPayPerPerson) + " " + calculator.getRubleInRightFormat(sumToPayPerPerson));
}
private static int parseStringIntoInt(String string) {
try {
return Integer.parseInt(string);
} catch (NumberFormatException e) {
return -1;
}
}
private static double parseStringIntoDouble(String string) {
try {
return Double.parseDouble(string);
} catch (NumberFormatException e) {
return -1;
}
}
}