Repository navigation
Expand file tree
/
Copy pathProductAdder.java
More file actions
47 lines (43 loc) · 1.88 KB
/
Copy pathProductAdder.java
File metadata and controls
47 lines (43 loc) · 1.88 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
import java.util.Scanner;
public class ProductAdder {
static void input(Scanner scanner, Basket basket) {
while (true) {
System.out.println("Введите название товара");
String name = inputName(scanner);
System.out.println("Введите стоимость товара");
inputPrice(scanner, basket, name);
System.out.println(
"Товар добавлен в корзину\n" +
"Хотите ли добавить еще один товар?\n" +
"Чтоб завершить процесс добавления товаров введите - завершить");
if (scanner.nextLine().equalsIgnoreCase("завершить")) {
break;
}
}
}
private static String inputName(Scanner scanner) {
String name;
while ((name = scanner.nextLine().trim()).isEmpty()) {
System.out.println("Название введено неверно. Введите название товара");
}
return name;
}
private static void inputPrice(Scanner scanner, Basket basket, String name) {
while (true) {
try {
double price = Double.parseDouble(scanner.nextLine());
if (price > 0) {
basket.add(new Product(name, price));
break;
} else {
throw new NumberFormatException();
}
} catch (NumberFormatException e) {
System.out.println(
"Стоимость товара введена некорректно. " +
"Введите стоимость товара в формате: 10.45"
);
}
}
}
}