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
96 lines (88 loc) · 4.33 KB
/
Main.java
File metadata and controls
96 lines (88 loc) · 4.33 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
92
93
94
95
96
import java.util.Scanner;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
static int numOfPersons;
static double check = 0;
static Scanner scanner = new Scanner(System.in);
static ArrayList<Product> products = new ArrayList<>();
static Calculate calc = new Calculate();
public static void main(String[] args) {
String s;
System.out.println("На скольких человек необходимо разделить счёт:");
s = scanner.next();
while (true) {
if (s.equalsIgnoreCase("завершить")) {
System.out.println("Программа завершена");
return;
}
if (s.matches("\\d+")) {
numOfPersons = Integer.parseInt(s);
if (numOfPersons == 1) {
System.out.println("Количество человек равно 1.\nЭто некорректное значение для подсчёта");
System.out.println("Введите количество человек:");
s = scanner.next();
} else if (numOfPersons < 1) {
System.out.println("Количество человек меньше 1.\nЭто некорректное значение для подсчёта");
System.out.println("Введите количество человек:");
s = scanner.next();;
} else break;
} else {
System.out.println("Это некорректное значение для подсчёта");
System.out.println("Введите количество человек:");
s = scanner.next();
}
}
while (true) {
System.out.println("Введите блюдо и цену.\nЕсли добавили все позиции введите 'Завершить'");
s = scanner.next();
if (!s.equalsIgnoreCase("завершить")) {
s += scanner.nextLine();
check += calc.calculate(s, products);
} else {
System.out.println("Добавленные товары:");
for (Product product : products) {
product.print();
}
if (check / numOfPersons > 1)
System.out.println(String.format("Итого: %.2f рубля с каждого", check / numOfPersons));
else
System.out.println(String.format("Итого: %.2f рубль с каждого", check / numOfPersons));
return;
}
}
}
public static class Calculate {
public static double calculate(String s, ArrayList products) {
Pattern pattern = Pattern.compile(" [0-9]*\\.?[0-9]+$");
Matcher matcher = pattern.matcher(s);
if (matcher.find())
if (matcher.start() > 1) {
double check = 0;
check += Double.parseDouble(s.substring(matcher.start(), matcher.end()));
Product product = new Product(s.substring(0, matcher.start() - 1), Double.parseDouble(s.substring(matcher.start(), matcher.end())));
product.printAdd();
products.add(product);
return check;
}
System.out.println("Некоректный ввод!\nВвод осуществляется в формате:Название блюда РУБЛИ.КОПЕЙКИ");
// System.out.println(s.substring(matcher.start(), matcher.end()));
return 0;
}
}
public static class Product {
private String name;
private double price;
Product(String name, double price) {
this.name = name;
this.price = price;
}
public void printAdd() {
System.out.println(String.format("Блюдо: %s по цене %.2f рублей добавленно", name, price));
}
public void print() {
System.out.println(String.format("Блюдо: %s по цене %.2f рублей", name, price));
}
}
}