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
93 lines (83 loc) · 3.2 KB
/
Main.java
File metadata and controls
93 lines (83 loc) · 3.2 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
System.out.println("Введите число гостей (от 2 человек):");
int guests = 0;
try {
guests = myObj.nextInt();
myObj.nextLine(); // Очистка буфера после чтения числа
} catch (NumberFormatException e) {
System.out.println("Некорректное число гостей");
return;
}
if (guests <= 1) {
System.out.println("Некорректное число гостей");
return;
}
System.out.println("Введенное количество гостей: " + guests);
List<Item> items = new ArrayList<>();
while (true) {
System.out.println("Введите название товара:");
String itemName = myObj.nextLine();
if (itemName.equalsIgnoreCase("завершить")) {
break;
}
System.out.println("Введите стоимость товара:");
double itemPrice = 0;
try {
itemPrice = Double.parseDouble(myObj.nextLine());
} catch (NumberFormatException e) {
System.out.println("Неверная цена товара");
return;
}
if (itemPrice <= 0) {
System.out.println("Неверная цена товара");
return;
}
Item item = new Item(itemName, itemPrice);
items.add(item);
System.out.println("Добавлен товар: " + itemName + ", Цена: " + itemPrice);
System.out.println("Хотите ли добавить еще товар? (Введите 'Завершить', чтобы закончить)");
}
System.out.println("Список товаров:");
for (Item item : items) {
System.out.println(item.getName() + ", Цена: " + item.getPrice());
}
double result = calculateTotalGoods(items) / guests;
System.out.printf("Сумма на человека: %.2f %s", result, rubleEnding(result));
}
public static String rubleEnding(double amount) {
int rubles = (int) amount;
if (rubles % 10 == 1 && rubles % 100 != 11) {
return "рубль";
} else if (rubles % 10 >= 2 && rubles % 10 <= 4 && (rubles % 100 < 10 || rubles % 100 >= 20)) {
return "рубля";
} else {
return "рублей";
}
}
public static double calculateTotalGoods(List<Item> items) {
double totalGoods = 0;
for (Item item : items) {
totalGoods += item.getPrice();
}
return totalGoods;
}
}
class Item {
private String name;
private double price;
public Item(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}