Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
61 lines (53 loc) · 2.07 KB
/
Copy pathCalculator.java
File metadata and controls
61 lines (53 loc) · 2.07 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
import java.util.InputMismatchException;
import java.util.Scanner;
public class Calculator {
static void calculate(int people) {
String food = "";
double price = 0.0;
while (true) {
System.out.println("Хотите еще добавить товар?, если да то Введите название товара" +
"\nДля завершения введите 'Завершить'");
Scanner scanner = new Scanner(System.in);
String inputFood = scanner.next();
if (inputFood.equalsIgnoreCase("Завершить")) {
break;
} else {
food = food + inputFood + "\n";
price = price + price();
}
}
int rouble = (int) Math.round(price);
System.out.println("Добавленные товары:\n" + food); //String.join
String message = "Каждый человек должен заплатить %.2f" + " " + declination(rouble);
System.out.println(String.format(message, (price / people)));
}
static double price() {
while (true) {
System.out.println("Введите стоимость в формате: 'рубли.копейки' [10,45, 11,40]");
double inputPrice = 0;
try {
Scanner scanner = new Scanner(System.in);
inputPrice = scanner.nextDouble();
if (inputPrice <= 0){
System.out.println("Неверное значение");
} else {
return inputPrice;
}
} catch (InputMismatchException e) {
System.out.println("Неверное значение");
}
}
}
private static String declination(int rouble) {
switch (rouble % 10) {
case 1:
return "рубль";
case 2:
case 3:
case 4:
return "рубля";
default:
return "рублей";
}
}
}