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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
| package com.trainigcenter.carsofrentv3.frames;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import com.trainigcenter.carsofrentv3.check.CheckEnterValues;
import com.trainigcenter.carsofrentv3.check.GetAndProcessingOfDate;
import com.trainigcenter.carsofrentv3.dao.DaoСars;
import com.trainigcenter.carsofrentv3.db.DB;
import com.trainigcenter.carsofrentv3.entity.Сars;
public class TableFrameCars extends JFrame {
private static final long serialVersionUID = 1L;
private DB db;
private int idRole;
private int idUser;
private String name;
private MyJPanel panel,panelCars, panelCostDay;
private JButton update;
private JButton choose;
private JButton orderRegistration;
private MyTable table, tableCostDay;
private JScrollPane scroll, scrollCostDay;
private JTabbedPane tabbed;
public TableFrameCars(DB db, int idRole) throws HeadlessException {
super();
this.db = db;
this.idRole = idRole;
this.setTitle("База автомобилей.");
this.setSize(790, 500);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.initComponents();
this.action();
this.setVisible(true);
}
public TableFrameCars(DB db, int idUser, String name, int idRole) throws HeadlessException {
super();
this.db = db;
this.idUser = idUser;
this.name = name;
this.idRole = idRole;
this.setTitle("База автомобилей.");
this.setSize(790, 500);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.initComponents();
this.action();
this.setVisible(true);
}
/**
* Задает ширину для указанного столбца
* @param index - номер столбца
* @param width - ширина столбца
*/
private void setSizeColumnAndRows(int index, int width) {
TableColumnModel tableColumnModel = table.getColumnModel();
TableColumn tableColumn = tableColumnModel.getColumn(index);
tableColumn.setPreferredWidth(width);
}
private void initComponents() {
Font font = new Font("Arbat-Bold", Font.PLAIN, 16);
Color yellow = new Color(255, 255, 0);
Color blue = new Color(0, 0, 255);
update = new JButton("Записать изменения в Базу Данных.");
update.setFont(font);
update.setForeground(Color.BLUE);
update.setBackground(yellow);
update.setPreferredSize(new Dimension(350, 35));
choose = new JButton("Возврат в окно выбора таблиц.");
choose.setFont(font);
choose.setForeground(Color.BLUE);
choose.setBackground(yellow);
choose.setPreferredSize(new Dimension(350, 35));
orderRegistration = new JButton("Оформить заказ.");
orderRegistration.setFont(font);
orderRegistration.setForeground(Color.YELLOW);
orderRegistration.setBackground(blue);
orderRegistration.setPreferredSize(new Dimension(350, 35));
table = new MyTable(db.query("SELECT * FROM cars"));
table.setRowHeight(25);
/* TableColumnModel cm = table.getColumnModel();
cm.removeColumn(cm.getColumn(3));*/
/*table.removeColumn(table.getColumnModel().getColumn(0));*/
tableCostDay = new MyTable(db.query("SELECT * FROM costday"));
tableCostDay.setRowHeight(25);
this.setSizeColumnAndRows(0, 15);
this.setSizeColumnAndRows(1, 250);
this.setSizeColumnAndRows(2, 35);
this.setSizeColumnAndRows(3, 50);
this.setSizeColumnAndRows(4, 90);
this.setSizeColumnAndRows(5, 45);
this.setSizeColumnAndRows(5, 45);
scroll = new JScrollPane(table);
scroll.setPreferredSize(new Dimension(765, 150));
scrollCostDay = new JScrollPane(tableCostDay);
scrollCostDay.setPreferredSize(new Dimension(200, 130));
panel = new MyJPanel();
panelCars = new MyJPanel();
panelCars.add(scroll);
panelCars.add(orderRegistration);
panelCostDay = new MyJPanel();
panelCostDay.add(update);
panelCostDay.add(scrollCostDay);
panelCostDay.setBackground(blue);
tabbed = new JTabbedPane();
tabbed.setPreferredSize(new Dimension(790, 500));
tabbed.addTab("Выбор машины", panelCars);
tabbed.addTab("Стоимость аренды", panelCostDay);
panel.add(tabbed);
this.add(panel);
}
/*panel.add(update);
panel.add(scroll);
panel.add(orderRegistration);
panel.add(choose);
panel.add(scrollCostDay);*/
/**
* public void mouseEntered(MouseEvent e) - наведение мыши
* public void mouseExited(MouseEvent e)- нажимание мыши
*/
private void action() {
table.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_SPACE) {
// пишем алгоритм обработки данных по нажатию клавиши Enter или пробел
}
}
});
table.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
update.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (idRole == 1) {
ResultSet rs = db.query("SELECT * FROM cars");
boolean isWriteDataID = false;
boolean isWriteDataSpeed = false;
boolean isWriteYear = false;
boolean isWriteFuel = false;
boolean isWriteIdCost = false;
boolean isIdNotNumber = false;
boolean isSelectedRow = false;
if (CheckEnterValues.checkIsSelectedRow(table.getSelectedRow())) {
System.out.println(" Не выделена строка. Запись в Базу данных - не производится");
} else {
isSelectedRow = true;
String idCheck = String.valueOf(table.getValueAt(table.getSelectedRow(), 0));
String modelCheck = String.valueOf(table.getValueAt(table.getSelectedRow(), 1));
String speedCheck = String.valueOf(table.getValueAt(table.getSelectedRow(), 2));
String colorCheck = String.valueOf(table.getValueAt(table.getSelectedRow(), 3));
String yearCheck = String.valueOf(table.getValueAt(table.getSelectedRow(), 4));
String fuelCheck = String.valueOf(table.getValueAt(table.getSelectedRow(), 5));
String costCheck = String.valueOf(table.getValueAt(table.getSelectedRow(), 6));
if (CheckEnterValues.checkIsDigit(idCheck, "id")) {
isIdNotNumber = true;
}
if (!isIdNotNumber) {
rs = db.query("SELECT * FROM cars WHERE id = " + idCheck);
try {
if (rs.next()) {
try {
int idSql = 0;
idSql = rs.getInt("id");
System.out.println("idSql = " + idSql);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
} catch (SQLException e1) {
e1.printStackTrace();
}
isWriteDataID = true;
} else {
isWriteDataID = false;
}
if (!CheckEnterValues.checkIsDigit(speedCheck, "speed")) {
int speedCheckInt = Integer.valueOf(speedCheck);
if (speedCheckInt < 200 || speedCheckInt > 400) {
System.out.println("проверка на достоверноое значение скорости\n");
isWriteDataSpeed = false;
String message1 = " В поле 'speed' нельзя занести данное значение.\n"
+ " Спорткар не может развить такую скорость";
String message2 = "Поля таблицы.";
JOptionPane.showMessageDialog(panel, message1, message2,
JOptionPane.INFORMATION_MESSAGE);
} else {
isWriteDataSpeed = true;
}
} else {
isWriteDataSpeed = false;
System.out.println("id - введены не цифры");
}
if (!CheckEnterValues.checkIsDigit(yearCheck, "year_manufacture")) {
int yearCheckInt = Integer.valueOf(yearCheck);
if (yearCheckInt < 1950 || yearCheckInt > 2019) {
System.out.println("проверка на достоверное значение года выпуска\n");
isWriteYear = false;
String message1 = " В поле 'year_manufacture' нельзя занести данное значение.\n"
+ " Спорткары в данный период времени еще не выпускались";
String message2 = "Поля таблицы.";
JOptionPane.showMessageDialog(panel, message1, message2,
JOptionPane.INFORMATION_MESSAGE);
} else {
isWriteYear = true;
}
} else {
isWriteYear = false;
System.out.println("id - введены не цифры");
}
if (!CheckEnterValues.checkIsDigit(fuelCheck, "fuel_flow")) {
int fuelCheckInt = Integer.valueOf(fuelCheck);
if (fuelCheckInt < 1 || fuelCheckInt > 14) {
System.out.println("проверка на достоверное значение года выпуска\n");
isWriteFuel = false;
String message1 = " В поле 'fuel_flow' нельзя занести данное значение.\n"
+ " Спорткары c таким расходом топлива - не исправны";
String message2 = "Поля таблицы.";
JOptionPane.showMessageDialog(panel, message1, message2,
JOptionPane.INFORMATION_MESSAGE);
} else {
isWriteFuel = true;
}
} else {
isWriteFuel = false;
System.out.println("id - введены не цифры");
}
int idLastTable = 0;
if (tableCostDay.getRowCount() == 0) {
idLastTable = 0;
} else {
idLastTable = Integer
.valueOf((tableCostDay.getValueAt(table.getRowCount() - 1, 0).toString()));
}
if (!CheckEnterValues.checkIsDigit(costCheck, "id_cost_day")) {
int costCheckInt = Integer.valueOf(costCheck);
if (costCheckInt < 1 || costCheckInt > idLastTable) {
System.out.println("проверка на достоверное значение идентификатора таблицы\n");
isWriteIdCost = false;
String message1 = " В поле 'id_cost_day' нельзя занести данное значение.\n"
+ " Разрешены :\n" + " 1, 2, 3, 4";
String message2 = "Поля таблицы.";
JOptionPane.showMessageDialog(panel, message1, message2,
JOptionPane.INFORMATION_MESSAGE);
} else {
isWriteIdCost = true;
}
} else {
isWriteIdCost = false;
System.out.println("id - введены не цифры");
}
if (isWriteDataID && isWriteDataSpeed && isWriteYear && isWriteFuel && isWriteIdCost
&& isSelectedRow) {
Integer id = Integer.valueOf(idCheck);
String model = modelCheck;
System.out.println("model = " + model);
Integer speed = Integer.valueOf(speedCheck);
System.out.println("speed = " + speed);
String color = colorCheck;
System.out.println("color = " + color);
Integer year = Integer.valueOf(yearCheck);
System.out.println("year = " + year);
Integer fuel = Integer.valueOf(fuelCheck);
System.out.println("fuel = " + fuel);
Integer cost = Integer.valueOf(costCheck);
System.out.println("cost = " + cost);
DaoСars daoCars = new DaoСars(db);
Сars cars = new Сars(id, model, speed, color, year, fuel, cost);
daoCars.update(cars);
System.out.println("Изменения записаны");
}
}
} else if (idRole == 2) {
String message1 = " Вам запрещено редактировать данную таблицу.";
String message2 = "Уровень доступа.";
JOptionPane.showMessageDialog(panel, message1, message2, JOptionPane.WARNING_MESSAGE);
}
}
});
choose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (!GetAndProcessingOfDate.getValidate(idRole)) {
new TablesFrame(db);
dispose();
System.out.println("Переход в TablesFrame ");
}
}
});
orderRegistration.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
new ChooseOfProductsFrame(db, idUser, name, idRole);
dispose();
System.out.println("Переход в ChooseOfProductsFrame");
}
});
}
} |