Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
78 lines (62 loc) · 3.02 KB
/
Copy pathCalculator.java
File metadata and controls
78 lines (62 loc) · 3.02 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
import java.util.ArrayList;
import java.util.Scanner;
public class Calculator {
public void calculator() {
double total = 0.0;
Scanner scanner = new Scanner(System.in);
String format = "%.2f";
ArrayList<String> products = new ArrayList<>();
ArrayList<Double> prices = new ArrayList<>();
outerLoop:
while (true) {
System.out.println("Введите наименование товара (или 'Завершить' для завершения)");
String name = scanner.nextLine().trim();
if (name.equalsIgnoreCase("Завершить")) {
break;
}
products.add(name);
double priceNum;
while (true) {
System.out.println("Введите стоимость товара в формате руб.копейки");
try {
String priceInput = scanner.nextLine().trim().replace(',', '.');
priceNum = Double.parseDouble(priceInput);
if (priceNum <= 0) {
System.out.println("Введите положительное число");
} else {
prices.add(priceNum);
break;
}
} catch (NumberFormatException e) {
System.out.println("\nОшибка: Введенный текст не является числом.\n");
}
}
total += priceNum;
while (true) {
System.out.println("Хотите добавить еще одну позицию?\nВыберети вариант и наберите номер ответа.\n1. ДА\n2. НЕТ");
String question = scanner.nextLine().trim();
if (question.equalsIgnoreCase("1")) {
break;
} else if (question.equalsIgnoreCase("2")) {
break outerLoop;
} else {
System.out.println("\nНе верно. Введите только 1 или 2.\n");
}
}
}
System.out.println("\nCписок продуктов:");
for (int i=0; i<products.size(); i++){
String product = products.get(i);
double priceСonversion = prices.get(i);
System.out.printf("Товар: " + product + " Стоимость %.2f " , priceСonversion );
System.out.println(Ruble.getRuble(priceСonversion));
}
System.out.print("\nОбщая стоимость всех товаров = " + String.format(format,total));
System.out.println(" " + Ruble.getRuble(total) + "\n");
Persons person = new Persons();
double conversion = Double.parseDouble(person.countPersons());
double division = total / conversion;
System.out.printf("Итого с каждого по: %.2f ", division);
System.out.println(Ruble.getRuble(division));
}
}