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
123 lines (105 loc) · 3.78 KB
/
Main.java
File metadata and controls
123 lines (105 loc) · 3.78 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// dev branch for Y.Practicum
import java.lang.invoke.ConstantCallSite;
import java.util.Scanner;
class Dish {
String name;
double price;
Dish(String name, double price) {
this.name = name;
this.price = price;
}
}
class Bill {
int numberOfPersons;
String listOfDishes;
double totalSum;
Bill(int numberOfPersons) {
this.numberOfPersons = numberOfPersons;
this.listOfDishes = "";
this.totalSum = 0;
}
public void addDish(Dish dish) {
this.listOfDishes = this.listOfDishes + (this.listOfDishes.length()==0 ? "" : "\n") + dish.name;
this.totalSum += dish.price;
System.out.println("Блюдо " + dish.name + " добавлено в чек");
}
public void print() {
System.out.println("Добавленные товары:");
System.out.println(listOfDishes);
}
}
class Calculator {
Bill bill;
Calculator(Bill bill) {
this.bill = bill;
}
public double payment() {
return bill.totalSum/bill.numberOfPersons;
}
public String formattedOutPut() {
double payment = this.payment();
return String.format("%.2f " + this.rubbleProperCase(payment), payment);
}
private String rubbleProperCase(Double sum) {
int intSum = (int)Math.floor(sum);
if (intSum%10==1 && intSum%100!=11) {
return "рубль";
}
else if (intSum%10==2 && intSum%100!=12) {
return "рубля";
}
else if (intSum%10==3 && intSum%100!=13) {
return "рубля";
}
else if (intSum%10==4 && intSum%100!=14) {
return "рубля";
}
else {
return "рублей";
}
}
}
public class Main {
public static void main(String[] args) {
Bill bill;
Scanner scanner = new Scanner(System.in);
//Получаем количество гостей
while (true) {
System.out.print("На сколько гостей разбить чек? ");
if (scanner.hasNextInt()) {
int numberOfPersons = scanner.nextInt();
if (numberOfPersons > 1) {
bill = new Bill(numberOfPersons);
break;
}
}
scanner.nextLine();
System.out.println("Неверное значение. Количество гостей должно быть больше 1. Попробуйте еще раз.");
}
scanner.nextLine();//Вынужденная мера для обхода ошибки https://stackoverflow.com/questions/23450524/java-scanner-doesnt-wait-for-user-input
while (true) {
System.out.print("Введите название блюда или команду Завершить: ");
String inputString = scanner.nextLine();
if (inputString.equalsIgnoreCase("завершить")) {
break;
}
while (true) {
System.out.print("Введите цену блюда: ");
if (scanner.hasNextDouble()) {
double inputPrice = scanner.nextDouble();
if (inputPrice>0) {
Dish dish = new Dish(inputString, inputPrice);
bill.addDish(dish);
break;
}
}
scanner.nextLine();
}
scanner.nextLine();//аналогично, комментарию выше
}
bill.print();//выводим счет на экран
Calculator calc = new Calculator(bill);
System.out.println(calc.formattedOutPut());//выводим итог расчета
scanner.close();
}
}