forked from Yandex-Practicum/Java-Module-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
84 lines (76 loc) · 3.91 KB
/
Main.java
File metadata and controls
84 lines (76 loc) · 3.91 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int personNumber = getPersonNumber();
double sumToPay;
// Создается калькулятор
Calculator calculator = new Calculator(personNumber);
// Пользователь вводит наименование и стоимость товара для подсчета
boolean stopEnterProduct = false;
boolean stopEnterPrice = false;
while (!stopEnterProduct){
System.out.println("Введите название товара");
if (scanner.hasNextLine()){
String productName = scanner.nextLine();
if(productName.equalsIgnoreCase("завершить")){
stopEnterProduct = true;
} else {
// Пользователь вводит стоимость товара для подсчета
while (!stopEnterPrice){
System.out.println("Введите стоимость товара в рублях");
if(scanner.hasNextDouble()){
double productPrice = scanner.nextDouble();
if (productPrice < 0){
System.out.println("Некорректное значение");
continue;
}
Product product = new Product(productName, productPrice);
calculator.addProduct(product);
scanner.nextLine();
stopEnterPrice = true;
} else {
if (scanner.nextLine().equalsIgnoreCase("завершить")) {
stopEnterPrice = true;
} else{
System.out.println("Некорректное значение");
}
}
}
}
}
}
System.out.println("Добавленные товары:");
calculator.printProductList();
sumToPay = calculator.calculateSumToPay();
if (Math.floor(sumToPay) == 1)
System.out.println(String.format("Каждый должен заплатить по %.2f рубль",sumToPay));
else if (Math.floor(sumToPay) == 2 || Math.floor(sumToPay) == 3 || Math.floor(sumToPay) == 4)
System.out.println(String.format("Каждый должен заплатить по %.2f рубля",sumToPay));
else
System.out.println(String.format("Каждый должен заплатить по %.2f рублей",sumToPay));
}
public static int getPersonNumber(){
// Запрашиваем у пользователя количество человек, на которых нужно разделить счет
Scanner scanner = new Scanner(System.in);
int personNumber;
while (true){
System.out.println("На скольких человек нужно разделить счет?");
if(scanner.hasNextInt()){
personNumber = scanner.nextInt();
if (personNumber == 1){
System.out.println("Нет смысла что то считать и делить");
}
else if (personNumber < 1){
System.out.println("Некорректное значение для подсчета");
}
else {
return personNumber;
}
} else{
System.out.println("Некорректное значение для подсчета");
scanner.nextLine();
}
}
}
}