-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.java
206 lines (186 loc) · 7.36 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
198
199
200
201
202
203
204
205
206
package com.salimov.yurii.lesson06.task02H;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
/**
* 6.2 (Hibernate) Спроектировать базу «Квартиры». Каждая запись в базе содержит данные о
* квартире (район, адрес, площадь, кол. комнат, цена). Сделать возможность
* выборки квартир из списка по параметрам.
*
* @author Yuriy Salimov ([email protected])
*/
public class Main {
private final static String PERSISTENCE_UNIT = "JPA_task62H";
private static final 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();
scanner.close();
}
}
private static void menu() {
boolean exit = false;
while (!exit) {
System.out.println("\n-+-Menu-+-");
System.out.println("1: add apartment");
System.out.println("2: add random apartment");
System.out.println("3: delete apartment");
System.out.println("4: change apartment");
System.out.println("5: view apartments");
System.out.println("6: selection of apartments");
System.out.println("0: exit");
System.out.println("-+-+-+-+-+-");
System.out.print("-> ");
final String menuNumber = scanner.nextLine();
switch (menuNumber) {
case "1":
addApartment();
break;
case "2":
insertRandomApartment();
break;
case "3":
deleteApartment();
break;
case "4":
changeApartment();
break;
case "5":
viewApartments();
break;
case "6":
selectionOfApartments();
break;
case "0":
exit = true;
break;
default:
System.err.println("Unknown command! Try again...");
break;
}
}
}
private static void addApartment() {
final Apartment apartment = newApartment();
dataBase.addApartment(apartment);
}
private static void deleteApartment() {
System.out.print("Enter apartment id: ");
final long id = scanner.nextLong();
dataBase.deleteApartment(id);
}
private static void changeApartment() {
System.out.print("Enter apartment id: ");
final long id = scanner.nextLong();
final Apartment apartment = newApartment();
dataBase.changeApartment(id, apartment);
}
private static void insertRandomApartment() {
System.out.print("Enter apartments count: ");
final int count = scanner.nextInt();
final String[] addresses = {"5, Murmanska st.",
"4, prosp. Glushkova",
"55, Lomonosova st."};
final Random random = new Random();
String address;
double price;
Apartment apartment;
for (int i = 0; i < count; i++) {
address = addresses[random.nextInt(addresses.length)];
price = random.nextDouble() * 100000;
apartment = new Apartment(address, price);
dataBase.addApartment(apartment);
}
}
private static void viewApartments() {
final Collection<Apartment> list = dataBase.getApartments();
final String title = "All apartments:";
printList(title, list);
}
private static void selectionOfApartments() {
System.out.println("1: selection by region");
System.out.println("2: selection by price");
System.out.println("3: selection by area");
System.out.println("4: selection by number of room");
System.out.print("-> ");
final String menuNumber = scanner.nextLine();
switch (menuNumber) {
case "1":
selectionByRegion();
break;
case "2":
selectionByPrice();
break;
case "3":
selectionByArea();
break;
case "4":
selectionByRoom();
break;
default:
System.err.println("Unknown command! Try again...");
break;
}
}
private static void selectionByRegion() {
System.out.print("Enter district:");
String region = scanner.nextLine();
final Collection<Apartment> list = dataBase.selectionByRegion(region);
final String title = "All apartments in the region \"" + region + "\":";
printList(title, list);
}
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<Apartment> list = dataBase.selectionByPrice(priceMin, priceMax);
final String title = "All apartments are priced from " + priceMin + " to " + priceMax + ":";
printList(title, list);
}
private static void selectionByArea() {
System.out.print("Enter the minimum area:");
final double areaMin = scanner.nextDouble();
System.out.print("Enter the maximum area:");
final double areaMax = scanner.nextDouble();
final Collection<Apartment> list = dataBase.selectionByArea(areaMin, areaMax);
final String title = "All apartments with area from " + areaMin + " to " + areaMax + ":";
printList(title, list);
}
private static void selectionByRoom() {
System.out.print("Enter the minimum number of rooms: ");
final int numberMin = scanner.nextInt();
System.out.print("Enter the maximum number of rooms: ");
final int numberMax = scanner.nextInt();
final Collection<Apartment> list = dataBase.selectionByRoom(numberMin, numberMax);
final String title = "All apartments with number of rooms from " + numberMin + " to " + numberMax + ":";
printList(title, list);
}
private static Apartment newApartment() {
System.out.print("Enter region: ");
final String region = scanner.nextLine();
System.out.print("Enter address: ");
final String address = scanner.nextLine();
System.out.print("Enter area: ");
final double area = scanner.nextDouble();
System.out.print("Enter number of rooms: ");
final int number = scanner.nextInt();
System.out.print("Enter price: ");
final double price = scanner.nextDouble();
return new Apartment(region, address, area, number, price);
}
private static void printList(final String title, final Collection<Apartment> apartments) {
System.out.println(title);
for (Apartment apartment : apartments) {
System.out.println(apartment);
}
}
}