Repository navigation
Expand file tree
/
Copy pathBillCalculator.java
More file actions
79 lines (69 loc) · 2.53 KB
/
Copy pathBillCalculator.java
File metadata and controls
79 lines (69 loc) · 2.53 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 BillCalculator {
public int numberOfPeople = 0;
public String dishesList = "";
public double billAmount = 0.0;
Scanner scanner;
public BillCalculator (Scanner scanner) {
this.scanner = scanner;
}
public void inputParameters () {
System.out.println ("Введите количество человек, на которых нужно поделить счет");
int i = 0;
while (!scanner.hasNextInt()) {
System.out.println("Введите целое число");
scanner.next();
}
while (true) {
i = scanner.nextInt();
if (i < 2) {
System.out.println("Количество человек не должно быть меньше двух");
}
else {
numberOfPeople = i;
break;
}
}
}
public void addDishes () {
while (true) {
System.out.println ("Введите название блюда");
String name = scanner.next();
if (name.equalsIgnoreCase("завершить")) {
break;
}
System.out.println ("Введите цену блюда в формате \"рубли.копейки\"");
while (!scanner.hasNextDouble()) {
System.out.println("Некорректный ввод цены, введите цену блюда в формате \"рубли.копейки\"");
scanner.next();
}
double price = scanner.nextDouble();
dishesList = dishesList + name + "\n";
billAmount = billAmount + price;
}
}
public void printResult () {
System.out.println("Добавленные товары:\n"+dishesList);
double costPerPerson = billAmount / numberOfPeople;
String costText = "%.2f ";
System.out.println("Сумма на человека: " + String.format(costText,costPerPerson) + addCorrectRubleString(costPerPerson));
}
public String addCorrectRubleString (double billAmount) {
int amount = (int) billAmount;
if(amount > 100)
amount %= 100;
if(amount > 20)
amount %= 10;
switch(amount)
{
case 1:
return "Рубль";
case 2:
case 3:
case 4:
return "Рубля";
default:
return "Рублей";
}
}
}