Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
79 lines (71 loc) · 3.22 KB
/
Copy pathCalculator.java
File metadata and controls
79 lines (71 loc) · 3.22 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
import java.util.Scanner;
public class Calculator {
private final int numberOfPerson; // количество человек, на которых нужно делить счёт
private String goods = ""; // список товаров в заказе
private double sum = 0; // сумма по счёту
public Calculator(int numberOfPerson) {
this.numberOfPerson = numberOfPerson;
}
public void addGood(Good good) {
this.sum += good.getCost();
String addedGood = String.format("%s\n", good.getName());
System.out.printf("Товар %s стоимостью %.2f успешно добавлен в счёт\n",
good.getName(), good.getCost());
this.goods = this.goods + addedGood;
}
public void operate() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Введите название товара или ЗАВЕРШИТЬ для окончания ввода: ");
String input = scanner.nextLine();
if (input.equalsIgnoreCase("завершить")) {
printGoods();
printSumPerPerson();
break;
} else {
String name = input;
System.out.println("Введите стоимость товара в формате [руб.коп]:");
// проверка на корректный double
while (true) {
input = scanner.nextLine();
try {
double cost = Double.parseDouble(input);
if (cost < 0) {
System.out.println("Стоимость должна быть неотрицательной," +
"введите корректную стоимость в формате [руб.коп]");
continue;
}
addGood(new Good(name, cost)); // добавляем товар в счет
break;
} catch (Exception e) {
System.out.println("Введите корректную стоимость товара в формате [руб.коп]");
}
}
}
}
}
public void printGoods() {
System.out.println("Добавленные товары:");
System.out.println(goods);
}
public void printSumPerPerson() {
double sumPerPerson = sum / numberOfPerson;
System.out.printf("Сумма на человека: %.2f %s\n", sumPerPerson, rublesDeclination(sumPerPerson));
}
public String rublesDeclination(double number) {
int floorNumber = (int)Math.floor(number);
String result = "рублей";
if (floorNumber % 100 >= 11 && floorNumber % 100 < 20) result = "рублей";
else switch (floorNumber % 10) {
case 1:
result = "рубль";
break;
case 2:
case 3:
case 4:
result = "рубля";
break;
}
return result;
}
}