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
46 lines (37 loc) · 1.65 KB
/
Calculator.java
File metadata and controls
46 lines (37 loc) · 1.65 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
import java.util.Scanner;
public class Calculator {
double sum = 0;
String listProduct = "Добавленные товары:\n";
public void sumProduct() {
Scanner scanner = new Scanner(System.in);
Product product = new Product();
while (true) {
System.out.println("Введите название товара");
product.name = scanner.next();
listProduct = listProduct + product.name + "\n";
boolean isRightPrice = false;
while (!isRightPrice) {
System.out.println("Введите цену товара в формате рубли.копейки");
if (scanner.hasNextDouble()) {
product.price = scanner.nextDouble();
if (product.price < 0) {
System.out.println("Цена не может быть отрицательной");
} else {
isRightPrice = true;
}
} else {
System.out.println("Вы ввели не число!");
scanner.next();
}
}
sum = sum + product.price;
System.out.println("Товар добавлен");
System.out.println("Хотите ли вы добавить еще товар?");
System.out.println("Для завершения добавления товаров напишите Завершить");
String answer = scanner.next();
if (answer.equalsIgnoreCase("Завершить")) {
break;
}
}
}
}