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
57 lines (53 loc) · 2.78 KB
/
Main.java
File metadata and controls
57 lines (53 loc) · 2.78 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Объявим все что необходимо для работы
Calculator calculator = new Calculator();
Scanner scan = new Scanner(System.in);
int peopleCount;
while (true) {
System.out.println("На сколько человек необходимо разделить счет?");
try {
peopleCount = Integer.parseInt(scan.next());
if (peopleCount < 1) {
System.out.println("Ошибка ввода попробуйте еще раз!");
} else if (peopleCount == 1) {
System.out.println("Счет не может быть разделен на одного человека. Попробуйте еще раз!");
} else {
break;
}
} catch (NumberFormatException ex) {
System.out.println("Ошибка ввода! Возможен только ввод целых чисел.");
}
}
// переменные для записи товара
double price;
String goodsName;
while (true) {
System.out.println("Введите наименование товара: ");
goodsName = scan.next();
while (true) {
System.out.println("Введите стоимость товара: ");
try {
price = Double.parseDouble(scan.next());
if (price <= 0) {
System.out.println("Стоимость товара не может быть отрицательной или меньше 0");
}
break;
} catch (NumberFormatException ex) {
System.out.println("Ошибка ввода! Необходимо ввести стоимость товара в формате рубли.копейки.");
}
}
calculator.addGoods(goodsName, price);
System.out.println("Товар добавлен в корзину!");
System.out.println("Хотите ли вы добавить еще товар?");
if (scan.next().toLowerCase().compareTo("завершить") == 0) {
break;
}
}
System.out.println("Добавленные товары: " + "\n" + calculator.basket);
System.out.println("Стоимость счета на каждого человека составляет: " +
String.format("%.2f", calculator.allPriceSum / peopleCount) + " " + calculator.correctStringCurrency(peopleCount));
scan.close();
}
}