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
110 lines (97 loc) · 4.13 KB
/
Main.java
File metadata and controls
110 lines (97 loc) · 4.13 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// ваш код начнется здесь
// вы не должны ограничиваться только классом Main и можете создавать свои классы по необходимости
System.out.println("Привет Мир");
Scanner scanner = new Scanner(System.in);
//считываем кол-во гостей
int visitor = 0;
System.out.println ("Введите колличество гостей");
while (true) {
String str = scanner.next();
while (!isNumeric(str)) {
System.out.println("Введите колличество гостей");
str = scanner.next();
}
visitor = Integer.parseInt(str);
if ((visitor <= 1) | (visitor == 0)) {
System.out.println("Некорректное кол-во гостей");
System.out.println("Введите колличество гостей");
} else if (visitor > 1) {
break;
}
}
//считываем кол-во товаров
boolean enough = true;
Product list = new Product();
Calculate menu_list = new Calculate();
while (enough) {
System.out.println("Введите название ");
list.name = scanner.next();
System.out.println("Введите цену");
String str = scanner.next();
while (!isNumeric(str)) {
System.out.println("Введите цену с разделителем \".\" ");
str = scanner.next();
}
list.price = Double.parseDouble(str);
menu_list.getAddList(list.name, list.price);
menu_list.getListPrice(list.price);
System.out.println("Хотите ввести ещё товар?");
System.out.println("Eсли вы хотите ввести ещё товар, введите ДА");
System.out.println("Eсли вы хотите ввести ещё товар, введите Завершить ");
String answer = scanner.next();
if (answer.equalsIgnoreCase("завершить")) {
enough = false;
break;
}
}
if (!enough){
System.out.println("Добавленные товары:");
System.out.println(menu_list.receipt);
float pay = (float) (menu_list.pay/visitor);
String rubString = "";
int rub = (int) (pay % 10);
int rubTeen = (int) (pay % 100);
if ((rub == 0 ) || (rub == 5) || (rub == 6) || (rub == 7 ) || (rub == 8 ) || (rub == 9 ) || ((rubTeen >= 11) && (rubTeen <= 14))){
rubString = "рублей";
} else if ((rub == 2) || (rub == 4) || (rub == 3) ){
rubString = "рубля";
} else if (rub == 1) {
rubString = "рубль";
}
String message = "Каждый должен заплатить %.2f %s";
System.out.println(String.format(message, pay, rubString));
}
}
public static class Calculate{
String receipt = "";
double pay = 0;
public void getAddList (String name, double price) {
receipt = receipt + String.format("%s %.2f \n",
name,
price);
System.out.println("Товар успешно добавлен");
}
public void getListPrice(double price){
pay = pay + price;
}
}
public static class Product {
String name;
double price;
}
public static boolean isNumeric(String string) {
double value;
if(string == null || string.equals("")) {
return false;
}
try {
value = Double.parseDouble(string);
return true;
} catch (NumberFormatException e) {
}
return false;
}
}