-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.java
197 lines (179 loc) · 7.04 KB
/
Main.java
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.salimov.yurii.lesson07.task01;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
/**
* 7.1 Создать таблицу «Меню в ресторане». Колонки: название блюда, его стоимость,
* вес, наличие скидки. Написать код для добавления записей в таблицу и их выборки
* по критериям «стоимость от-до», «только со скидкой», выбрать набор блюд так,
* чтобы их суммарный вес был не более 1 КГ.
*
* @author Yuriy Salimov ([email protected])
*/
public class Main {
private final static String PERSISTENCE_UNIT = "JPA_task71";
private final static Scanner scanner = new Scanner(System.in);
private static DataBase dataBase;
public static void main(String[] args) {
try {
dataBase = DataBase.getInstance();
dataBase.connect(PERSISTENCE_UNIT);
menu();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
dataBase.disconnect();
}
}
private static void menu() {
while (true) {
System.out.println("\n-+-Menu-+-");
System.out.println("1: add dish");
System.out.println("2: add random dish");
System.out.println("3: delete dish");
System.out.println("4: change dish");
System.out.println("5: view dishes");
System.out.println("6: selection of dishes");
System.out.println("0: exit");
System.out.println("-+-+-+-+-+-");
System.out.print("-> ");
String menuNumber = scanner.nextLine();
switch (menuNumber) {
case "1":
addDish();
break;
case "2":
insertRandomDishes();
break;
case "3":
deleteDish();
break;
case "4":
changeDish();
break;
case "5":
viewDishes();
break;
case "6":
selectionOfDishes();
break;
case "0":
return;
default:
System.err.println("Unknown command! Try again...");
break;
}
}
}
private static void addDish() {
final Dish dish = newDish();
dataBase.addDish(dish);
}
private static void insertRandomDishes() {
System.out.print("Enter dish count: ");
final int count = scanner.nextInt();
final Random random = new Random();
for (int i = 0; i < count; i++) {
final String name = "name " + i;
final double price = random.nextDouble() * 300;
final double weight = random.nextDouble() * 1000;
final double discount = random.nextDouble() * 100;
final Dish dish = new Dish(name, weight, price, discount);
dataBase.addDish(dish);
}
}
private static void deleteDish() {
System.out.print("Enter dish id: ");
final long id = scanner.nextLong();
dataBase.deleteDish(id);
}
private static void changeDish() {
System.out.print("Enter dish id: ");
final long id = scanner.nextLong();
final Dish dish = newDish();
dish.setId(id);
dataBase.changeDish(dish);
}
private static void viewDishes() {
final Collection<Dish> list = dataBase.getDishes();
final String title = "All dishes:";
printList(title, list);
}
private static void selectionOfDishes() {
System.out.println("1: selection by price");
System.out.println("2: selection by weight");
System.out.println("3: selection by discount");
System.out.println("4: selection by all discount");
System.out.print("-> ");
final String menuNumber = scanner.nextLine();
switch (menuNumber) {
case "1":
selectionByPrice();
break;
case "2":
selectionByWeight();
break;
case "3":
selectionByDiscount();
break;
case "4":
selectionByAllDiscount();
break;
default:
System.err.println("Unknown command! Try again...");
break;
}
}
private static void selectionByPrice() {
System.out.print("Enter price min: ");
final double priceMin = scanner.nextDouble();
System.out.print("Enter price max: ");
final double priceMax = scanner.nextDouble();
final Collection<Dish> list = dataBase.selectionByPrice(priceMin, priceMax);
final String title = "All dishes are priced from " + priceMin + " to " + priceMax + ":";
printList(title, list);
}
private static void selectionByWeight() {
System.out.print("Enter weight min: ");
final double weightMin = scanner.nextDouble();
System.out.print("Enter weight max: ");
final double weightMax = scanner.nextDouble();
final Collection<Dish> list = dataBase.selectionByWeight(weightMin, weightMax);
final String title = "All dishes with a weight from " + weightMin + " to " + weightMax + ":";
printList(title, list);
}
private static void selectionByDiscount() {
System.out.print("Enter discount min: ");
final double discountMin = scanner.nextDouble();
System.out.print("Enter discount max: ");
final double discountMax = scanner.nextDouble();
final Collection<Dish> list = dataBase.selectionByDiscount(discountMin, discountMax);
final String title = "All dishes with a discount from " + discountMin + " to " + discountMax + ":";
printList(title, list);
}
private static void selectionByAllDiscount() {
final Collection<Dish> list = dataBase.selectionByDiscount();
final String title = "All dishes with a discount:";
printList(title, list);
}
private static Dish newDish() {
System.out.print("Enter name: ");
final String name = scanner.nextLine();
System.out.print("Enter weight: ");
final double weight = scanner.nextDouble();
System.out.print("Enter price: ");
final double price = scanner.nextDouble();
System.out.print("Enter discount: ");
final double discount = scanner.nextDouble();
System.out.print("Enter specification: ");
final String specification = scanner.nextLine();
return new Dish(name, weight, price, discount, specification);
}
private static void printList(final String title, final Collection<Dish> dishes) {
System.out.println(title);
for (Dish dish : dishes) {
System.out.println(dish);
}
}
}