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
52 lines (50 loc) · 2.2 KB
/
Main.java
File metadata and controls
52 lines (50 loc) · 2.2 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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int persons = -1;
Scanner scaner = new Scanner(System.in);
while (persons <= 1) {
System.out.println("Введите количество человек");
if (scaner.hasNextInt()) {
persons = scaner.nextInt();
if (persons <= 1){
System.out.println("Введено некорректное значение (должно быть больше 1)");
}
} else {
System.out.println("Введено некорректное значение (должно быть число)");
scaner.next();
}
}
System.out.println("Количество человек для расчета " + persons);
Calculator calculator = new Calculator();
while (true) {
System.out.println("Введите название товара (для завершения введите \"Завершить\"):");
String name = scaner.next();
if (name.equalsIgnoreCase("Завершить")) {
break;
} else {
double price = askForPrice(scaner);
calculator.addProduct(new Product(name, price));
}
}
scaner.close();
calculator.printProducts();
calculator.calculateForPersons(persons);
}
public static double askForPrice(Scanner scanerPrice) {
while (true) {
System.out.println("Введите стоимость товара (в формате рубли.копейки)");
if (scanerPrice.hasNextFloat()){
double price = scanerPrice.nextDouble();
if (price > 0) {
return price;
} else {
System.out.println("Стоимость не может быть меньше или равна нулю");
}
}else {
System.out.println("Формат стоимости должен быть: рубли.копейки");
scanerPrice.next();
}
}
}
}