forked from Yandex-Practicum/Java-Module-Project-YP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
97 lines (83 loc) · 3.93 KB
/
Main.java
File metadata and controls
97 lines (83 loc) · 3.93 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int count = 0;
Scanner scanner = new Scanner(System.in);
do {
System.out.println("На сколько человек необходимо разделить счёт?");
String countStr = scanner.next();
if (!countStr.matches("[1-9]+")) {
System.out.println("Не корректный ввод");
} else {
count = Integer.parseInt(countStr);
if (count == 1) {
System.out.println("Подсчет для одного человека");
addProductList(1);
} else if (count < 1) {
System.out.println("Некорректное значение для подсчёта");
} else {
System.out.println("Подсчет для " + count + " человек");
addProductList(count);
}
}
}
while (count < 2);
}
private static void addProductList(int count) {
ArrayList<Product> productList = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
String nameFormat;
boolean active = true;
while (active) {
System.out.println("Введите название товара");
String name = scanner.next();
nameFormat = name.trim().toUpperCase().toLowerCase();
if (nameFormat.contains("завершить"))
break;
if (!nameFormat.chars().allMatch(Character::isLetter)) {
System.out.println("Не корректное название товара");
} else {
do {
System.out.println("Введите цену товара в формате рубли.копейки");
String str = scanner.next();
if (str.trim().toUpperCase().toLowerCase().contains("завершить")) {
active = false;
break;
}
if (!str.matches("[0-9]+\\.[0-9]+")) {
System.out.println("Не корректная цена товара");
} else {
double price = Double.parseDouble(str);
Product product = new Product(nameFormat, price);
productList.add(product);
System.out.println("Товар " + product.name + " с ценой " + product.price + " успешно добавлен в корзину");
System.out.println("Хотите добавить еще?");
break;
}
} while (true);
}
}
System.out.println("===========================");
sum(count, productList);
}
private static void sum(int count, ArrayList<Product> productList) {
double sum = productList.stream().mapToDouble(product1 -> product1.price).sum();
System.out.println("Список продуктов: ");
productList.forEach((s) -> System.out.println(s.name + " : " + s.price));
System.out.println("Итоговая сумма: " + parseRubCase(sum));
System.out.println("Итоговая сумма на каждого человека: " + parseRubCase(sum / count));
System.out.println("===========================");
}
public static String parseRubCase(double price) {
int inInt = (int) price;
String cased;
if (inInt % 100 / 10 == 1 || inInt % 10 >= 5 || inInt % 10 == 0)
cased = "рублей";
else if (inInt % 10 == 1)
cased = "рубль";
else
cased = "рубля";
return String.format("%.2f", price) + " " + cased;
}
}