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
62 lines (52 loc) · 2.63 KB
/
Main.java
File metadata and controls
62 lines (52 loc) · 2.63 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
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
Locale.setDefault(new Locale("en", "US")); // Для отображения и ввода с точкой - меняем локаль
int countUsers = countUsers();
double priceCount = priceCount();
sumResult(priceCount, countUsers);
}
// Вывод списка товаров
public static double priceCount() {
ProductList products = new ProductList();
Formatter format = new Formatter();
ArrayList<Product> productsList = products.inputOrders();
System.out.println("\nДобавленные товары:");
int countOrders = 0;
double priceCount = 0;
for(Product product : productsList) {
countOrders += 1;
priceCount += product.price;
System.out.println(countOrders + ". " + product.name + " (" + format.moneyCut(product.price) + " " + format.typeMoneyRub(product.price) + ")");
}
return priceCount;
}
// Вывод суммы товаров и того, сколько нужно заплатить каждому
public static void sumResult(double money, int countUsers) {
Formatter format = new Formatter();
System.out.println("\nСумма добавленных товаров: " + format.moneyCut(money) + ' ' + format.typeMoneyRub(money));
double multiplyPrice = money / countUsers;
System.out.println("Каждый человек должен заплатить: " + format.moneyCut(multiplyPrice) + ' ' + format.typeMoneyRub(multiplyPrice));
}
// Получаем количество людей
public static int countUsers() {
while (true) {
try {
System.out.println("Введите кол-во человек, на которых будет разделён счёт:");
Scanner scanner = new Scanner(System.in);
int countUsers = scanner.nextInt();
if (countUsers > 1) {
return countUsers;
} else if (countUsers == 1) {
System.out.println("Ошибка: пользователь один. Нечего делить.");
} else {
System.out.println("Ошибка: некорректное значение для подсчёта.");
}
} catch (Exception e) {
System.out.println("Ошибка: некорректное значение для подсчёта.");
}
}
}
}