Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
71 lines (63 loc) · 2.64 KB
/
Copy pathMain.java
File metadata and controls
71 lines (63 loc) · 2.64 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.start();
}
}
class Calculator {
private int numOfPeople;
private double totalCost;
private String items;
public Calculator() {
numOfPeople = 0;
totalCost = 0.0;
items = "";
}
public void start() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Введите количество человек: ");
numOfPeople = scanner.nextInt();
if (numOfPeople == 1) {
System.out.println("Ошибка: некорректное количество человек");
} else if (numOfPeople < 1) {
System.out.println("Ошибка: некорректное значение для подсчета");
} else {
break;
}
}
System.out.println("Добавление товаров. Введите 'завершить' для окончания.");
while (true) {
System.out.println("Введите название товара: ");
String itemName = scanner.next();
if (itemName.equalsIgnoreCase("завершить")) {
break;
}
System.out.println("Введите стоимость товара: ");
double itemCost = scanner.nextDouble();
addItem(itemName, itemCost);
System.out.println("Товар успешно добавлен.");
}
System.out.println("Добавленные товары:\n" + items);
System.out.println("Сумма, которую должен заплатить каждый человек: " + calculateCostPerPerson());
}
public void addItem(String itemName, double itemCost) {
items += itemName + "\n";
totalCost += itemCost;
}
public String calculateCostPerPerson() {
double costPerPerson = totalCost / numOfPeople;
int costInt = (int) costPerPerson;
double costSharing = costPerPerson - costInt;
int roundedNum = (int) Math.floor(costSharing * 100);
String formattedCost = String.format("%.2f", costPerPerson);
if (costInt % 10 == 1 && costInt % 100 != 11) {
return formattedCost + " рубль";
} else if ((costInt % 10 >= 2 && costInt % 10 <= 4) && !(costInt % 100 >= 12 && costInt % 100 <= 14)) {
return formattedCost + " рубля";
} else {
return formattedCost + " рублей";
}
}
}