Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
147 lines (137 loc) · 5.43 KB
/
Copy pathMain.java
File metadata and controls
147 lines (137 loc) · 5.43 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Food> shoppingCard = new ArrayList<Food>();
System.out.println("Добро пожаловать в Кото-Кафе, это консольное приложение для подсчета цены сьеденного корма!");
String meow;
Calculator.checkNumberOfCats();
do {
ShoppingCard (shoppingCard);
System.out.println("Хотите добавить ещё одну позицию корма?\n\nВведите 'Продолжить' чтобы добавить ещё корма, или 'Завершить' чтобы разделить счет: ");
meow = scanner.nextLine();
if (!meow.equalsIgnoreCase("Завершить") && !meow.equalsIgnoreCase("Продолжить") ){
System.out.println("\n\nОшибка: введите 'Продолжить' чтобы добавить ещё корма, или 'Завершить' чтобы разделить счет: ");
meow = scanner.nextLine();
}
} while (!meow.equalsIgnoreCase("Завершить"));
Calculator.total(shoppingCard);
System.out.println("Список заказанных кормов");
for( Food name : shoppingCard){
System.out.println(name.getName());
}
System.out.println("Сумма на каждого котика составила: " + String.format("%.2f", Calculator.totalPricePerCats) + Formator.around());
}
public static void ShoppingCard (ArrayList<Food> shoppingCard){
Scanner scanner = new Scanner(System.in);
String name;
double price = 0;
boolean validInput = false;
System.out.println("Введите название корма");
name = scanner.nextLine();
while (!validInput) {
System.out.print("Введите цену корма: ");
if (scanner.hasNextDouble()) {
price = scanner.nextDouble();
if (price <= 0) {
System.out.println("Цена корма не может быть отрицательной или равна нули (БЕСПЛАТНО НЕ КОРМИМ)!");
} else {
validInput = true;
}
} else {
System.out.println("Возможно введена точка или буква");
scanner.nextLine();
}
}
Food feed = new Food(name, price);
shoppingCard.add(feed);
}
}
class Food{
String name;
double price ;
Food (String name, double price){
this.name = name;
this.price = price;
}
public String getName() {
return this.name;
}
public double getPrice() {
return this.price;
}
}
class Calculator {
public static int countCats;
public static double totalPricePerCats;
public static int checkNumberOfCats() {
Scanner scanner = new Scanner(System.in);
int cat = 0;
boolean validInput = false;
while (!validInput) {
System.out.print("Введите количество котиков: ");
if (scanner.hasNextInt()) {
cat = scanner.nextInt();
if (cat <= 1) {
System.out.println("Котиков должно быть больше двух");
} else {
validInput = true;
}
} else {
System.out.println("Возможно введены буквы или иные символы");
scanner.nextLine();
}
}
// do {
// System.out.println("Введите количество котиков.");
// while (!scanner.hasNextInt()){
// System.out.println("Это не число");
// System.out.println("Введите количество котиков.");
// scanner.next();
// }
// cat = scanner.nextInt();
// if (cat <= 1) {
// System.out.println("Котиков должно быть больше двух");
// }
// } while (cat <= 1);
// scanner.nextLine();
System.out.println("Отлично");
countCats = cat;
return countCats;
}
public static double total (ArrayList<Food> shoppingCard){
double sum = 0;
for (Food price: shoppingCard){
sum = sum + price.getPrice();
}
sum = sum / countCats;
totalPricePerCats = sum;
return Math.round(sum);
}
}
class Formator {
public static String around() {
String output;
double money = Calculator.totalPricePerCats;
int x = (int) money % 10;
if (x % 100 >= 11 && x % 100 <= 14) {
output = " рублей";
} else {
switch (x) {
case 1:
output = " рубль";
break;
case 2:
case 3:
case 4:
output = " рубля";
break;
default:
output = " рублей";
}
}
return output;
}
}