forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBillCalculator.java
More file actions
50 lines (47 loc) · 1.69 KB
/
BillCalculator.java
File metadata and controls
50 lines (47 loc) · 1.69 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
import java.util.Scanner;
public class BillCalculator {
public void startBillCalculator() {
int people = enterNumberOfPeople();
Calculator calculator = new Calculator();
calculator.enterListOfItems();
System.out.println("Добавленные товары:");
calculator.printNamesItems();
double amountPerPerson = calculator.calculate(people);
printAmountPerPerson(amountPerPerson);
}
private int enterNumberOfPeople() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Введите количество людей");
if (scanner.hasNextInt()) {
int n = scanner.nextInt();
if (n > 1) {
return n;
}
}
scanner.nextLine();
System.out.println("Вы ввели некорректное количество людей");
}
}
private void printAmountPerPerson(double amountPerPerson) {
String ruble;
int intAmountPerPerson = (int) amountPerPerson;
if (intAmountPerPerson % 100 >= 10 && intAmountPerPerson % 100 <= 20) {
ruble = "рублей";
} else {
switch (intAmountPerPerson % 10) {
case 1:
ruble = "рубль";
break;
case 2:
case 3:
case 4:
ruble = "рубля";
break;
default:
ruble = "рублей";
}
}
System.out.printf("С каждого человека %.2f %s", amountPerPerson, ruble);
}
}