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
91 lines (83 loc) · 4.46 KB
/
Main.java
File metadata and controls
91 lines (83 loc) · 4.46 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
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static boolean isInt(String people){
try {
int peopleInt = Integer.parseInt (people);
if (peopleInt <= 1){
System.out.println("Введено не корректное кол-во человек");
return false;
} else {
return true;
}
} catch (NumberFormatException e) {
System.out.println("Введено не корректное значение, используйте целые числа");
return false;
}
}
public static boolean isPriceDouble(String strPrice){
try {
double price = Double.parseDouble(strPrice);
if (price >= 0.01) {
return true;
} else {
System.out.println("Введено не корректное значение, стоимость должна быть больше 0");
}
} catch (NumberFormatException e) {
System.out.println("Введено не корректное значение, стоимость должна быть в формате {рубли.копейки}");
System.out.println("Пример:\n Помидор 10.45");
return false;
}
return false;
}
public static void main(String[] args) {
ArrayList<Product> products = new ArrayList<Product>(); // создаем список товаров
Scanner scanner = new Scanner(System.in);
int people = Formater.peopleCorrectly(); // получаем интовое колво людей
Calculator calculator = new Calculator(people);
System.out.println("Добавление товаров в калькулятор");
System.out.println("Пример:\n Помидор 10.45");
boolean isAddProduct = true;
while (isAddProduct) { // собираем товары
String product = scanner.next();
if (product.equalsIgnoreCase("завершить")) {
break;
}
// принимаем не известный тип данных
String strPrice = scanner.next();
if (strPrice.equalsIgnoreCase("завершить")) {
break;
}
if (isPriceDouble(strPrice)){
//товар корректный
double price = Double.parseDouble(strPrice);
products.add(new Product(product, price)); //добавление товара в товары
System.out.println(product + " " + Math.floor(price*100)/100 + ". Данный товар был успешно добавлен");
calculator.account += Math.floor(price*100)/100;
System.out.println("Общий чек составляет:" + calculator.account);
isAddProduct = false;
}
System.out.println("Хотите ли Вы добавить ещё один товар?");
while (!isAddProduct){ // проверяем хочет ли пользователь добавить еще товар и переспрашиваем при не понятном ответе
String ans = scanner.nextLine().trim();
if (ans.equalsIgnoreCase("нет") || ans.equalsIgnoreCase("завершить")){
break;
} else if (ans.equalsIgnoreCase("да")) {
System.out.println("Введите название и цену");
isAddProduct = true;
} else {
System.out.println("Введите да или нет");
}
}
}
// вывод всех товаров
System.out.println("Добавленные товары:\n");
for (Product product : products){
String output = product.title + " " + product.price + " " + Formater.rub(product.price);
System.out.println(output);
}
// вывод сколько должен заплатить каждый
System.out.print("Каждый должен заплатить: ");
System.out.println(Math.floor(calculator.personalAccount()*100)/100 + " " + Formater.rub(Math.floor(calculator.personalAccount()*100)/100));
}
}