Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
110 lines (93 loc) · 3.66 KB
/
Copy pathMain.java
File metadata and controls
110 lines (93 loc) · 3.66 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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
BillCalculator calculator = new BillCalculator();
System.out.println("На скольких человек необходимо разделить счёт?");
int peopleCount;
while (true) {
try {
peopleCount = Integer.parseInt(scanner.nextLine());
if (peopleCount > 1) {
calculator.setNumberOfPeople(peopleCount);
break;
} else {
System.out.println("Введите корректное количество людей (больше 1):");
}
} catch (NumberFormatException e) {
System.out.println("Пожалуйста, введите число.");
}
}
while (true) {
System.out.println("Введите название товара и его стоимость (или 'Завершить' для завершения):");
String input = scanner.nextLine();
if ("Завершить".equalsIgnoreCase(input)) {
break;
} else {
try {
String[] parts = input.split(" ");
String name = parts[0];
double price = Double.parseDouble(parts[1]);
calculator.addProduct(new Product(name, price));
System.out.println("Товар '" + name + "' по цене " + price + " добавлен.");
} catch (Exception e) {
System.out.println("Некорректный ввод, попробуйте еще раз.");
}
}
}
System.out.println("Добавленные товары:");
calculator.getProducts().forEach(product -> System.out.println(product.getName() + " по цене " + product.getPrice()));
double amountPerPerson = calculator.getAmountPerPerson();
System.out.println("Каждый должен заплатить: " + Formatter.formatAmount(amountPerPerson) + " " + Formatter.getRublesWord(amountPerPerson));
}
}
class Product {
private final String name;
private final double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
}
class BillCalculator {
private final List<Product> products = new ArrayList<>();
private int numberOfPeople;
public void setNumberOfPeople(int numberOfPeople) {
this.numberOfPeople = numberOfPeople;
}
public void addProduct(Product product) {
products.add(product);
}
public double getTotalAmount() {
return products.stream().mapToDouble(Product::getPrice).sum();
}
public double getAmountPerPerson() {
return getTotalAmount() / numberOfPeople;
}
public List<Product> getProducts() {
return products;
}
}
class Formatter {
public static String formatAmount(double amount) {
return String.format("%.2f", amount);
}
public static String getRublesWord(double amount) {
long rubles = (long) 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 "рублей";
}
}
}