Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
61 lines (51 loc) · 1.94 KB
/
Copy pathCalculator.java
File metadata and controls
61 lines (51 loc) · 1.94 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
import java.util.Locale;
import java.util.Scanner;
public class Calculator {
private Scanner scanner = new Scanner(System.in);
private String dishes = "Добавленные товары:\n ";
private double summary = 0;
void addDishes() {
while (true) {
System.out.println("Введите название продукта.");
dishes += scanner.next() + " \n";
System.out.println("Теперь введите его стоимость в формате [руб.коп.].");
while (!scanner.hasNextDouble()) {
scanner.next();
System.out.println("Ошибка, введите корректное число.");
}
while (true) {
double i = scanner.nextDouble();
if (i <= 0) {
System.out.println("Введите корректную стоимость продукта.");
} else {
summary += i;
break;
}
}
System.out.println("Успех! \nХотите ли добавить еще продукт?");
String answer = scanner.next();
if (answer.equalsIgnoreCase("завершить")) {
break;
}
}
}
void showResult(int guests) {
double result = summary / guests;
String rubleTitle = rubleCase(result);
System.out.println(dishes);
System.out.println(String.format("%.2f", result) + " " + rubleTitle);
}
private String rubleCase(double money) {
int floorMoney = (int) Math.floor(money);
int lastDigit = floorMoney % 10;
if (lastDigit == 1) {
return "рубль ";
}
else if (lastDigit == 2 || lastDigit == 3 || lastDigit == 4) {
return "рубля";
}
else {
return "рублей ";
}
}
}