Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
105 lines (66 loc) · 3.22 KB
/
Copy pathMain.java
File metadata and controls
105 lines (66 loc) · 3.22 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.util.Scanner;
public class Main {
// private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numberOfPeople;
double costOfGoods;
// 1. Вводим количество посетителей.
System.out.println("На скольких человек разделить счет?");
while (true) {
while (!sc.hasNextInt()) {
System.out.println("Некорректный ввод, введите целое число.");
sc.next();
}
numberOfPeople = sc.nextInt();
if (numberOfPeople <=1) {
System.out.println("Количество посетителей должно быть больше 1.");
} else {
System.out.println("Количество посетителей - " + numberOfPeople);
break;
}
}
Calculator calc = new Calculator();
// 2.1 Вводим название товара
while (true) {
System.out.println("Введите название товара");
String productName = sc.next();
if (productName.equalsIgnoreCase("ЗАВЕРШИТЬ")){
// 3.1 Выводим список товаров
System.out.println(calc.listOfProducts);
// 3.2 Выводим цену для одного человека
double costPerPerson = calc.totalAmount / (double) numberOfPeople;
System.out.println(String.format("%.2f",costPerPerson) + " " + GetCurrency(costPerPerson));
break;
} else {
while (true) {
// 2.2 Вводим стоимость товара
System.out.println("Введите стоимость товара");
while (!sc.hasNextDouble()) {
System.out.println("Некорректный ввод, введите число.");
sc.next();
}
costOfGoods = sc.nextDouble();
if (costOfGoods > 0) {
calc.AddProductToCalculator(productName,costOfGoods);
break;
} else {
System.out.println("Введите не нулевую стоимость товара." + numberOfPeople);
}
}
}
} //while (true)
}
public static String GetCurrency(double costPerPerson){
String currency = "рублей";
int integerPartOfNumber = (int)costPerPerson;
String strIntegerPartOfNumber = Integer.toString(integerPartOfNumber);
String theLastDigit = strIntegerPartOfNumber.substring(strIntegerPartOfNumber.length() - 1);
if (theLastDigit.equals("1")){
currency = "рубль";
} else if (theLastDigit.equals("2") || theLastDigit.equals("3") || theLastDigit.equals("4")) {
currency = "рубля";
}
return currency;
}
}