Repository navigation
Expand file tree
/
Copy pathProduct.java
More file actions
58 lines (51 loc) · 2.1 KB
/
Copy pathProduct.java
File metadata and controls
58 lines (51 loc) · 2.1 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
import java.util.Scanner;
public class Product {
Scanner scanner = new Scanner(System.in);
float price =0;
String product;
String fullProduct = "Добавленные товары: ";
float fullPrice;
void addProduct() {
Scanner scanner = new Scanner(System.in);
do {
System.out.print("Введите название товара: ");
product = scanner.nextLine().trim();
} while (product.isEmpty());
boolean validPrice = false;
do {
System.out.print("Введите стоимость товара в формате \"рубли.копейки\": ");
String priceString = scanner.nextLine().trim();
try {
price = Float.parseFloat(priceString);
if (price >= 0) {
validPrice = true;
} else {
System.out.println("Стоимость товара не может быть отрицательной!");
}
} catch (NumberFormatException e) {
System.out.println("Некорректный формат стоимости товара!");
}
} while (!validPrice);
System.out.println("Товар успешно добавлен!\n");
fullProduct += "\n" + product;
fullPrice += price;
}
void addMoreProduct(){
String end = "Завершить";
String yes = "Да";
while (true) {
System.out.println("Хотите добавить ещё товар?\nДля продолжения введите: " + yes + "\nДля завершения введите: " + end);
product = scanner.nextLine();
if (yes.equalsIgnoreCase(product)){
addProduct();
continue;
}
if (end.equalsIgnoreCase(product)){
System.out.println(fullProduct);
String.format("%.2f",fullPrice);
System.out.println("Сумма товаров: " + fullPrice);
break;
}
}
}
}