Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
89 lines (74 loc) · 3.44 KB
/
Copy pathCalculator.java
File metadata and controls
89 lines (74 loc) · 3.44 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
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Scanner;
public class Calculator {
int persons;
float price;
String name;
int countingPersons(){
try {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Введите количество человек:");
persons = scanner.nextInt();
if (persons < 1) {
System.out.println("Вы ввели некорректное число человек");
} else {
return persons;
}
}
}
catch (InputMismatchException e){
System.out.println("Вы ввели некорректный символ вместо числа. Введите данные заново");
return countingPersons();
}
}
void countingProducts(){
LinkedList<Product> list = new LinkedList<>();
Scanner scanner = new Scanner(System.in);
String exit = "ЗАВЕРШИТЬ";
int persons = countingPersons();
float sum = 0;
System.out.println("Введите название товара:" +
"\nВведите 'Завершить' для того, чтобы завершить процесс добавления товаров.");
while (true) {
name = scanner.next();
if (name.equalsIgnoreCase(exit)) {
sum = sum / persons;
String sumProduct = "%.2f";
Converter converter = new Converter(sum);
System.out.println("Добавленные товары:");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).name + " " + list.get(i).price);
}
System.out.println("Каждый должен заплатить: " + String.format(sumProduct, sum)
+ " " + converter.check());
break;
}
System.out.println("Введите стоимость товара в формате: [руб,коп]:");
while (true) {
try {
price = scanner.nextFloat();
if (price <= 0) {
System.out.println("Вы ввели некорректную сумму товара! Попробуйте ввести сумму товара снова.");
}
else{
break;
}
}
catch (InputMismatchException e){
if ((Integer.toString((int)price).equalsIgnoreCase(exit))){
break;
}
System.out.println("Вы ввели некорректный символ вместо числа или ввели некоректное число.");
countingProducts();
}
}
Product product = new Product(name, price);
list.add(product);
sum = (sum + product.price);
System.out.println("Товар успешно добавлен! Хотите добавить ещё один товар?" +
"\nИли введите 'Завершить', чтобы завершить процесс добавления товаров.");
}
}
}