Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
50 lines (47 loc) · 1.91 KB
/
Copy pathCalculator.java
File metadata and controls
50 lines (47 loc) · 1.91 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
import java.util.ArrayList;
import java.util.Scanner;
public class Calculator {
int people;
double sum;
ArrayList<Product> items;
Calculator(int people) {
this.people = people;
this.sum = 0;
this.items = new ArrayList<>();
}
void toSumItems() {
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("Введите название товара:");
String item = scanner.nextLine();
if (item.replaceAll("\\s", "").isEmpty()) {
System.out.println("Введите какой-то текст");
} else {
while (true) {
System.out.println("Введите стоимость товара:");
while (!scanner.hasNextDouble()) {
System.out.println("Введите число!");
scanner.nextLine();
}
double itemCost = scanner.nextDouble();
if (itemCost < 0) {
System.out.println("Введите положительное число!");
} else {
scanner.nextLine();
Product newProduct = new Product(item, itemCost);
System.out.println("Товар успешно добавлен.");
sum += newProduct.cost;
items.add(newProduct);
break;
}
}
System.out.println("Хотите ввести еще товар? Для выхода введите слово Завершить");
String answer = scanner.next();
if (answer.equalsIgnoreCase("Завершить")) {
scanner.close();
break;
}
}
}
}
}