Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
64 lines (56 loc) · 2.15 KB
/
Copy pathCalculator.java
File metadata and controls
64 lines (56 loc) · 2.15 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
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 + getPrice();
}
}
System.out.println("Добавленные товары:\n" + food);
double result = price / people;
String temp = "Каждый человек должен заплатить %.2f" + getDeclination((int) Math.floor(result));
System.out.println(String.format(temp, result));
}
static double getPrice() {
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 getDeclination(int rouble) {
if ((rouble % 100 / 10) == 1){
return " рублей.";
}
switch (rouble % 10){
case 1:
return " рубль.";
case 2:
case 3:
case 4:
return " рубля.";
default:
return " рублей.";
}
}
}