forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
60 lines (55 loc) · 2.41 KB
/
Calculator.java
File metadata and controls
60 lines (55 loc) · 2.41 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
import java.util.Scanner;
class Calculator {
String nameProduct;
String result;
double priceProduct;
double sum;
int number;
void countProduct() {
Scanner scanner = new Scanner(System.in);
StringBuilder builder = new StringBuilder();
while (true) {
System.out.println("Введите название товара:");
this.nameProduct = scanner.next();
if (this.nameProduct == null) continue;
if (this.nameProduct.equalsIgnoreCase("Завершить")) break;
System.out.println("Введите стоимость товара:");
while (true) {
if (scanner.hasNextDouble()) {
this.priceProduct = scanner.nextDouble();
if (this.priceProduct < 0) {
System.out.println("Некорректное значение для подсчета");
} else if (this.priceProduct >= 0) {
builder.append(this.nameProduct).append("\n");
this.sum = this.sum + this.priceProduct;
System.out.println("Товар " + this.nameProduct + " добавлен.\nХотите добавить следующий товар?");
break;
}
} else {
System.out.println("Введённое значение не является числом");
scanner.next();
}
}
}
this.result = builder.toString();
}
void numberOfPerson() {
final Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("На скольких человек разделить счёт?");
if (scanner.hasNextInt()) {
number = scanner.nextInt();
if (this.number == 1) {
System.out.println("Нет смысла делить счёт.");
} else if (this.number < 1) {
System.out.println("Некорректное значение для подсчёта.");
} else {
break;
}
} else {
System.out.println("Введенное значениемне не является числом.");
scanner.next();
}
}
}
}