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
128 lines (122 loc) · 4.49 KB
/
Main.java
File metadata and controls
128 lines (122 loc) · 4.49 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
123
124
125
126
127
128
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Guests guests = new Guests();
CalculatorOfGoods calculator = new CalculatorOfGoods();
int numberOfGuests = guests.inputNumberOfGuests();
calculator.inputGoods();
calculator.showGoods();
calculator.showExpensePerGuest(numberOfGuests);
}
}
class Guests {
public int inputNumberOfGuests(){
Scanner scanner = new Scanner(System.in);
System.out.println("Введите число гостей:");
while (true){
if (scanner.hasNextInt()) {
int numberOfGuests = scanner.nextInt();
if (numberOfGuests > 1){
return numberOfGuests;
}
else
showErrorMessage();
}
else {
scanner.nextLine();
showErrorMessage();
}
}
}
private void showErrorMessage(){
System.out.println("Некорректное значение! Число гостей должно быть целым положительным числом больше 1. \nПопробуйте еще раз:");
}
}
class CalculatorOfGoods {
private double sumPrice = 0;
private ArrayList<Good> goodsList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
public void inputGoods(){
String name;
double price;
while(true) {
System.out.println("Введите название товара и его стоимость:");
name = scanner.next();
price = inputCorrectPriceOfGood(name);
goodsList.add(new Good(name, price));
sumPrice = sumPrice + price;
System.out.println("Товар " + name + " успешно добавлен в счет!");
System.out.println("Добавить еще один товар?");
String answer = scanner.next();
if (answer.equalsIgnoreCase("завершить"))
break;
}
}
private double inputCorrectPriceOfGood(String name){
double price;
while (true) {
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();
scanner.nextLine();
if (price > 0)
return price;
else {
showErrorMessage(name);
}
}
else {
scanner.nextLine();
showErrorMessage(name);
}
}
}
private void showErrorMessage(String name){
System.out.println("Некорректное значение стоимости! Стоимость должна быпь положительным числом в формате рубли.копейки \nПопробуйте еще раз:");
System.out.print(name + " ------ ");
}
public void showGoods() {
System.out.println("\nДобавленные товары:");
for (Good good : goodsList) {
System.out.println(good.name);
}
}
public void showExpensePerGuest(int numberOfGuests){
double expensePerGuest = sumPrice/numberOfGuests;
FormatterOfNumber formatterOfNumber = new FormatterOfNumber();
String formattedString = formatterOfNumber.formatDoubleToString(expensePerGuest);
System.out.println(String.format("\nКаждый гость должен заплатить: %.2f %s",expensePerGuest, formattedString));
}
}
class Good {
String name;
double price;
public Good(String name, double price) {
this.name = name;
this.price = price;
}
}
class FormatterOfNumber {
String formatDoubleToString(double value) {
String formattedString = "";
int intPart = (int) Math.floor(value);
int valueToFormat = intPart % 100;
if (valueToFormat >= 10 && valueToFormat <= 20)
formattedString = "рублей";
else {
valueToFormat = intPart % 10;
switch (valueToFormat) {
case 1:
formattedString = "рубль";
break;
case 2, 3, 4:
formattedString = "рубля";
break;
case 0, 5, 6, 7, 8, 9:
formattedString = "рублей";
break;
}
}
return formattedString;
}
}