Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
82 lines (70 loc) · 3.38 KB
/
Copy pathMain.java
File metadata and controls
82 lines (70 loc) · 3.38 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Приветствие
System.out.println("Рады приветствовать вас в консольном приложении \"Калькулятор счета\"!");
//Получение входных данных о людях
Scanner scanner = new Scanner(System.in);
int quantityOfPersons = 0;
String nameOfProduct;
double priceOfProduct;
System.out.print("Введите колличество человек: ");
while (true) {
if (scanner.hasNextInt()) {
quantityOfPersons = scanner.nextInt();
if (quantityOfPersons > 1) {
break;
} else {
System.out.print("Ошибка,дальнейший расчет не имеет смысла.\nПовторите попытку ввода: ");
}
} else {
System.out.print("Недопустимый символ, введите число: ");
}
scanner.nextLine();
}
// Создаем корзину калькуляции покупок
boolean flag = true;
Basket basketCalc = new Basket();
System.out.println("Ваша корзина готова к заполнению.");
while (flag) {
System.out.print("Введите наименование товара: ");
nameOfProduct = scanner.next();
System.out.print("Введите цену товара (рубли, копейки): ");
while (true) {
if (scanner.hasNextDouble()) {
priceOfProduct = scanner.nextDouble();
if (priceOfProduct > 0) {
break;
} else {
System.out.print("Цена не может быть отрицательной. Попробуйте еще раз: ");
}
} else {
System.out.print("Ошибка, введите число: ");
}
scanner.nextLine();
}
//вызов метода добавления товара
basketCalc.addProduct(nameOfProduct, priceOfProduct);
while (true) {
System.out.print("Если желаете добавить еще товар введите \"Да\"\nЕсли желаете завершить введите \"Завершить\".\n");
String flagValue = scanner.next();
if (flagValue.equalsIgnoreCase("Завершить")) {
flag = false;
break;
} else if (flagValue.equalsIgnoreCase("да")) {
break;
} else {
System.out.println("Неверный ввод");
}
}
}
//вывод результата
System.out.println("Добавленные товары: ");
for (String el : basketCalc.list) {
System.out.println(el);
}
float lastSum = (float) basketCalc.sum / quantityOfPersons;
String rub = basketCalc.convertRub(lastSum);
System.out.printf("К оплате %.2f %s за человека.", lastSum, rub);
}
}