Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
67 lines (60 loc) · 2.95 KB
/
Copy pathCalculator.java
File metadata and controls
67 lines (60 loc) · 2.95 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
import java.util.Scanner;
public class Calculator {
//класс и метод подсчета общей стоимости товара
public void calculate(int guests) {
String food = "Dobavlennie tovari: \n";
Double price = 0.0;
while (true) {
System.out.println("Vvedite nazvanie produkta ili zavershit: ");
Scanner scanner = new Scanner(System.in);
String inputFood = scanner.next();
//если ничего не добавлено и введено "завершить"
if (inputFood.equalsIgnoreCase("Zavershit") && food.equals("Dobavlennie tovari: \n")) {
break;
} else if (inputFood.equalsIgnoreCase("Zavershit")) {
System.out.println(food);
System.out.println("Kagdiy chelovek zaplatit: " + String.format("%.2f", price / guests) + " " + getRubleAddition(price / guests));
break;
} else {
System.out.println("Vvedite cenu tovara [rub.cop]");
Double inputPrice = 0.0;
String inputString;
// программа принимала значение стоимости через точку,но при вводе запятой крашилась, теперь программа принимает как точку, так и запятую, при вводе
// некорректного значения цены выводится сообщение о том, что значение введено некорректно
try {
inputString = scanner.next();
inputString = inputString.replace(",", ".");
inputPrice = Double.valueOf(inputString);
} catch (Exception e) {
System.out.println("Vvedite znachenie stoimosti zanovo i korrektno v ukazanom formate!");
continue;
}
if (inputPrice > 0) {
food = food + inputFood + "\n";
price = price + inputPrice;
System.out.println("Tovar " + inputFood + " Dobavlen uspeshno, ego cena sostavlaet: " + inputPrice + " " + getRubleAddition(inputPrice));
} else {
//случай когда цена товара меньше 0
System.out.println("Cena tovara ne mozhet bit menshe 0!");
}
}
}
}
//метод для написания рублей в правильном падеже
public String getRubleAddition(Double num) {
int preLastDigit = (int) (num % 100 / 10);
if (preLastDigit == 1) {
return "rubley";
}
switch ((int) (num % 10)) {
case 1:
return "rubl";
case 2:
case 3:
case 4:
return "rublya";
default:
return "rubley";
}
}
}