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
101 lines (90 loc) · 4.01 KB
/
Main.java
File metadata and controls
101 lines (90 loc) · 4.01 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int guests = Calculator.getGuestsNumber(scanner);
String itemList = Calculator.getItemsList(scanner);
double costPerPerson = Calculator.getCostPerPerson(itemList, guests);
String rubleSuffix = Calculator.getRubleSuffix(costPerPerson);
System.out.println("Каждый гость должен заплатить " + String.format("%.2f", costPerPerson) + " " + rubleSuffix + ".");
}
}
class Calculator {
public static int getGuestsNumber(Scanner scanner) {
int guests = 0;
boolean guestsNum = false;
while (!guestsNum) {
System.out.print("Введите количество гостей: ");
if (scanner.hasNextInt()) {
guests = scanner.nextInt();
if (guests <= 1) {
System.out.println("Некорректное значение. Количество гостей должно быть больше 1.");
} else {
guestsNum = true;
}
} else {
System.out.println("Введите корректное число!");
scanner.next();
}
}
return guests;
}
public static String getItemsList(Scanner scanner) {
double totalValue = 0.0;
boolean isGoodsEnough = true;
String itemList = "";
while (isGoodsEnough) {
System.out.print("Введите название товара: ");
String itemName = scanner.next();
boolean validCost = false;
while (!validCost) {
System.out.print("Введите стоимость товара (в формате рубли.копейки): ");
if (scanner.hasNextDouble()) {
double itemCost = scanner.nextDouble();
if (itemCost >= 0) {
totalValue += itemCost;
itemList += itemName + " - " + String.format("%.2f", itemCost) + " руб." + "\n";
System.out.println("Товар " + itemName + " успешно добавлен.");
validCost = true;
} else {
System.out.println("Стоимость товара не может быть отрицательной.");
}
} else {
System.out.println("Введите корректное число!");
scanner.next();
}
}
System.out.print("Хотите добавить ещё один товар? (Введите \"Завершить\", чтобы закончить): ");
String userChoice = scanner.next();
if (userChoice.equalsIgnoreCase("Завершить")) {
isGoodsEnough = false;
}
}
System.out.println("Добавленные товары: ");
System.out.println(itemList);
return itemList;
}
public static double getCostPerPerson(String itemList, int guests) {
double totalValue = 0;
String[] items = itemList.split("\n");
for (String item : items) {
String costString = item.substring(item.lastIndexOf(" ") + 1, item.lastIndexOf(" руб."));
totalValue += Double.parseDouble(costString);
}
return totalValue / guests;
}
public static String getRubleSuffix(double costPerPerson) {
int rubles = (int) costPerPerson;
int lastDigit = rubles % 10;
int lastTwoDigits = rubles % 100;
if (lastTwoDigits >= 11 && lastTwoDigits <= 19) {
return "рублей";
} else if (lastDigit == 1) {
return "рубль";
} else if (lastDigit >= 2 && lastDigit <= 4) {
return "рубля";
} else {
return "рублей";
}
}
}