-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainFrame.java
More file actions
470 lines (443 loc) · 18.1 KB
/
Copy pathMainFrame.java
File metadata and controls
470 lines (443 loc) · 18.1 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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.util.Scanner;
import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
public class MainFrame extends JFrame implements ActionListener {
private static final int WIDTH = 1000;
private static final int HEIGHT = 500;
private int ROW;
private static final int COLUMN = 4;
// 파일 이름
private String personalFileName;
private String familyFileName;
// 개인과 가족별 ROW count
private int cnt;
// 테이블 관련
DefaultTableModel personalAccountTableModel;
JTable personalAccountTable;
JScrollPane personalScrollPane;
DefaultTableModel familyAccountTableModel;
JTable familyAccountTable;
JScrollPane familyScrollPane;
// 개인과 가족별 수입 지출 총합 텍스트필드
JTextField personalIncome;
JTextField personalConsumption;
JTextField personalTotal;
JTextField familyIncome;
JTextField familyConsumption;
JTextField familyTotal;
// 데이터 삽입에 필요한 텍스트필드
JTextField dateField;
JTextField majorClassificationField;
JTextField minorClassificationField;
JTextField amountField;
// 콤보박스 생성
DefaultComboBoxModel selectComboModel;
JComboBox selectCombo;
public MainFrame(int FID, String ID, String Name, String Relation) {
// 프레임 설정
super("Property Management System");
setSize(400, 400);
setLocation(960, 500);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// 입출력에 필요한 변수 초기화
cnt = 0;
// 파일 이름 설정
familyFileName = ("./Data/Account/" + intToString(FID) + ".txt");
personalFileName = ("./Data/Account/" + intToString(FID) + " " + Name + ".txt");
// 테이블을 붙일 panel 생성
JPanel tablePanel = new JPanel();
tablePanel.setLayout(new GridLayout(1, 2));
// 테이블 만들기 - 헤더와 데이터 불러올 배열 만들기
String tableHeader[] = { "날짜", "대분류", "소분류", "금액" };
String personalContents[][] = null;
String familyContents[][] = null;
// 개인 계좌 데이터 로드
Scanner inputStream = null;
try {
inputStream = new Scanner(new FileInputStream(personalFileName));
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, personalFileName + "파일이 존재하지 않습니다. 프로그램 에러로 프로그램을 종료합니다.");
System.exit(0);
}
// 배열의 행의 개수 읽어오기.
try {
ROW = inputStream.nextInt();
personalContents = new String[ROW][COLUMN];
} catch (InputMismatchException e) {
JOptionPane.showMessageDialog(null, "파일 로딩에 에러가 발생했습니다. 프로그램을 종료합니다.");
System.exit(0);
}
inputStream.nextLine();
// 배열에 값 저장하기
while (inputStream.hasNext()) {
try {
personalContents[cnt][0] = intToString(inputStream.nextInt());
personalContents[cnt][1] = inputStream.next();
personalContents[cnt][2] = inputStream.next();
personalContents[cnt++][3] = intToString(inputStream.nextInt());
} catch (InputMismatchException e) {
JOptionPane.showMessageDialog(null, "파일 로딩에 에러가 발생했습니다. 프로그램을 종료합니다.");
System.exit(0);
}
}
inputStream.close();
// 테이블 만들기
personalAccountTableModel = new DefaultTableModel(personalContents, tableHeader);
personalAccountTable = new JTable(personalAccountTableModel);
personalScrollPane = new JScrollPane(personalAccountTable);
tablePanel.add(personalScrollPane);
cnt = 0;
// 가족 계좌 데이터 로드
try {
inputStream = new Scanner(new FileInputStream(familyFileName));
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, familyFileName + "파일이 존재하지 않습니다. 프로그램 에러로 프로그램을 종료합니다.");
System.exit(0);
}
// 배열의 행의 개수 읽어오기.
try {
ROW = inputStream.nextInt();
familyContents = new String[ROW][COLUMN];
} catch (InputMismatchException e) {
JOptionPane.showMessageDialog(null, "파일 로딩에 에러가 발생했습니다. 프로그램을 종료합니다.");
System.exit(0);
}
inputStream.nextLine();
// 배열에 값 저장하기
while (inputStream.hasNext()) {
try {
familyContents[cnt][0] = intToString(inputStream.nextInt());
familyContents[cnt][1] = inputStream.next();
familyContents[cnt][2] = inputStream.next();
familyContents[cnt++][3] = intToString(inputStream.nextInt());
} catch (InputMismatchException e) {
JOptionPane.showMessageDialog(null, "파일 로딩에 에러가 발생했습니다. 프로그램을 종료합니다.");
System.exit(0);
}
}
inputStream.close();
// 테이블 만들기
familyAccountTableModel = new DefaultTableModel(familyContents, tableHeader);
familyAccountTable = new JTable(familyAccountTableModel);
familyScrollPane = new JScrollPane(familyAccountTable);
tablePanel.add(familyScrollPane);
add(tablePanel, BorderLayout.CENTER);
// JTable에서 컬럼명을 클릭했을때, 데이터 값 정렬
personalAccountTable.setAutoCreateRowSorter(true);
TableRowSorter personalAccountTableSorter = new TableRowSorter(personalAccountTableModel);
personalAccountTable.setRowSorter(personalAccountTableSorter);
familyAccountTable.setAutoCreateRowSorter(true);
TableRowSorter familyAccountTableSorter = new TableRowSorter(familyAccountTableModel);
familyAccountTable.setRowSorter(familyAccountTableSorter);
// Table의 셀 크기 조절
personalAccountTable.getColumnModel().getColumn(0).setPreferredWidth(118);
familyAccountTable.getColumnModel().getColumn(0).setPreferredWidth(118);
personalAccountTable.getColumnModel().getColumn(3).setPreferredWidth(100);
familyAccountTable.getColumnModel().getColumn(3).setPreferredWidth(100);
// 수입 지출 총합을 출력할 때 쓸 패널 생성.
JPanel sumPanel = new JPanel();
sumPanel.setLayout(new GridLayout(2, 2));
JPanel personalSumPanel = new JPanel();
personalSumPanel.setLayout(new BoxLayout(personalSumPanel, BoxLayout.X_AXIS));
JPanel familySumPanel = new JPanel();
familySumPanel.setLayout(new BoxLayout(familySumPanel, BoxLayout.X_AXIS));
JPanel personalSumLabelPanel = new JPanel();
personalSumLabelPanel.setLayout(new BoxLayout(personalSumLabelPanel, BoxLayout.X_AXIS));
JPanel familySumLabelPanel = new JPanel();
familySumLabelPanel.setLayout(new BoxLayout(familySumLabelPanel, BoxLayout.X_AXIS));
// 라벨 생성 및 sumPanel에 붙이기
JLabel personalIncomeLabel = new JLabel(" 개인수입 ");
personalSumLabelPanel.add(personalIncomeLabel);
JLabel personalConsumptionLabel = new JLabel(" 개인지출 ");
personalSumLabelPanel.add(personalConsumptionLabel);
JLabel personalTotalLabel = new JLabel(" 개인총합 ");
personalSumLabelPanel.add(personalTotalLabel);
JLabel familyIncomeLabel = new JLabel(" 가족수입 ");
familySumLabelPanel.add(familyIncomeLabel);
JLabel familyConsumptionLabel = new JLabel(" 가족지출 ");
familySumLabelPanel.add(familyConsumptionLabel);
JLabel familyTotalLabel = new JLabel(" 가족총합 ");
familySumLabelPanel.add(familyTotalLabel);
sumPanel.add(personalSumLabelPanel);
sumPanel.add(familySumLabelPanel);
// 각 텍스트필드 생성과 수정 불가하게 초기화, 각 패널에 붙이기
personalIncome = new JTextField("");
personalIncome.setEditable(false);
personalSumPanel.add(personalIncome);
personalConsumption = new JTextField("");
personalConsumption.setEditable(false);
personalSumPanel.add(personalConsumption);
personalTotal = new JTextField("");
personalTotal.setEditable(false);
personalSumPanel.add(personalTotal);
familyIncome = new JTextField("");
familyIncome.setEditable(false);
familySumPanel.add(familyIncome);
familyConsumption = new JTextField("");
familyConsumption.setEditable(false);
familySumPanel.add(familyConsumption);
familyTotal = new JTextField("");
familyTotal.setEditable(false);
familySumPanel.add(familyTotal);
sumPanel.add(personalSumPanel);
sumPanel.add(familySumPanel);
add(sumPanel, BorderLayout.NORTH);
totalCalculation();
// 데이터 삽입 삭제 저장
// 패널 생성
JPanel dataManagementPanel = new JPanel();
dataManagementPanel.setLayout(new GridLayout(3, 4));
// 데이터 삽입에 필요한 Text Field와 안내문구를 넣을 라벨 생성.
JLabel dateLabel = new JLabel("날짜");
dataManagementPanel.add(dateLabel);
dateField = new JTextField("(yyyymmdd)");
dataManagementPanel.add(dateField);
JLabel majorClassificationLabel = new JLabel("대분류");
dataManagementPanel.add(majorClassificationLabel);
majorClassificationField = new JTextField("(수입/지출)");
dataManagementPanel.add(majorClassificationField);
JLabel minorClassificationLabel = new JLabel("소분류");
dataManagementPanel.add(minorClassificationLabel);
minorClassificationField = new JTextField("(띄어쓰기 금지)");
dataManagementPanel.add(minorClassificationField);
JLabel amountLabel = new JLabel("금액");
dataManagementPanel.add(amountLabel);
amountField = new JTextField("정수입력");
dataManagementPanel.add(amountField);
// 콤보박스 추가
String[] selectPORF = new String[] { "가족", "개인" };
selectCombo = new JComboBox();
selectComboModel = new DefaultComboBoxModel(selectPORF);
selectCombo.setModel(selectComboModel);
dataManagementPanel.add(selectCombo);
// 버튼 추가 - 추가 삭제
JButton insert = new JButton("추가");
insert.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String event = e.getActionCommand();
if (event == "추가") {
// 필드에 입력한 값 받기
String inputString[] = new String[4];
inputString[0] = dateField.getText();
inputString[1] = majorClassificationField.getText();
inputString[2] = minorClassificationField.getText();
inputString[3] = amountField.getText();
String select = (String) selectComboModel.getSelectedItem();
// 필드 값 입력
if (select == "가족") {
familyAccountTableModel.addRow(inputString);
} else if (select == "개인") {
personalAccountTableModel.addRow(inputString);
} else {
JOptionPane.showMessageDialog(null, "예상치 못한 에러 발생. 관리자에게 문의하세요.");
}
totalCalculation();
// 필드 초기화
dateField.setText("");
majorClassificationField.setText("");
minorClassificationField.setText("");
amountField.setText("");
} else {
JOptionPane.showMessageDialog(null, "예상치 못한 에러 발생. 관리자에게 문의하세요.");
}
}
});
dataManagementPanel.add(insert);
JButton delete = new JButton("삭제");
delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String event = e.getActionCommand();
if (event == "삭제") {
if (personalAccountTable.getSelectedRow() != -1) {
personalAccountTableModel.removeRow(personalAccountTable.getSelectedRow());
} else if (familyAccountTable.getSelectedRow() != -1) {
familyAccountTableModel.removeRow(familyAccountTable.getSelectedRow());
} else {
return;
}
} else {
JOptionPane.showMessageDialog(null, "예상치 못한 에러 발생. 관리자에게 문의하세요.");
}
}
});
dataManagementPanel.add(delete);
JButton modify = new JButton("수정");
modify.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String event = e.getActionCommand();
if (event == "수정") {
String select = (String) selectComboModel.getSelectedItem();
String modifyString = null;
if (select == "개인" && personalAccountTable.getSelectedRow() != -1
&& personalAccountTable.getSelectedColumn() != -1) {
// 선택한 열에 따라 수정할 값을 입력한 필드의 값을 modifyString 변수에 넣음
if (personalAccountTable.getSelectedColumn() == 0) {
modifyString = dateField.getText();
} else if (personalAccountTable.getSelectedColumn() == 1) {
modifyString = majorClassificationField.getText();
} else if (personalAccountTable.getSelectedColumn() == 2) {
modifyString = minorClassificationField.getText();
} else if (personalAccountTable.getSelectedColumn() == 3) {
modifyString = amountField.getText();
} else {
return;
}
// 선택한 행과 열의 데이터 수정
personalAccountTableModel.setValueAt(modifyString, personalAccountTable.getSelectedRow(),
personalAccountTable.getSelectedColumn());
} else if (select == "가족" && familyAccountTable.getSelectedRow() != -1
&& familyAccountTable.getSelectedColumn() != -1) {
// 가족 부분 수정코드. 위와 동일
if (familyAccountTable.getSelectedColumn() == 0) {
modifyString = dateField.getText();
} else if (familyAccountTable.getSelectedColumn() == 1) {
modifyString = majorClassificationField.getText();
} else if (familyAccountTable.getSelectedColumn() == 2) {
modifyString = minorClassificationField.getText();
} else if (familyAccountTable.getSelectedColumn() == 3) {
modifyString = amountField.getText();
} else {
return;
}
familyAccountTable.setValueAt(modifyString, familyAccountTable.getSelectedRow(),
familyAccountTable.getSelectedColumn());
} else {
return;
}
} else {
JOptionPane.showMessageDialog(null, "예상치 못한 에러 발생. 관리자에게 문의하세요.");
}
totalCalculation();
}
});
dataManagementPanel.add(modify);
// dataMangementPanel 메인프레임 SOUTH에 추가
add(dataManagementPanel, BorderLayout.SOUTH);
// 메뉴바 추가
JMenu systemMenu = new JMenu("메뉴");
// 파일로 저장
JMenuItem saveMenuItem = new JMenuItem("저장");
systemMenu.add(saveMenuItem);
saveMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String event = e.getActionCommand();
if (event.equals("저장")) {
// 입출력에 필요한 변수 초기화
cnt = 0;
PrintWriter outputStream = null;
try { // 개인 데이터 저장
outputStream = new PrintWriter(new FileOutputStream(personalFileName));
cnt = personalAccountTable.getRowCount();
outputStream.println(cnt + "");
for (int i = 0; i < cnt; i++) {
outputStream.println(
personalAccountTable.getValueAt(i, 0) + " " + personalAccountTable.getValueAt(i, 1)
+ " " + personalAccountTable.getValueAt(i, 2) + " "
+ personalAccountTable.getValueAt(i, 3));
}
outputStream.close();
outputStream = new PrintWriter(new FileOutputStream(familyFileName));
cnt = familyAccountTable.getRowCount();
outputStream.println(cnt + "");
for (int i = 0; i < cnt; i++) {
outputStream.println(familyAccountTable.getValueAt(i, 0) + " "
+ familyAccountTable.getValueAt(i, 1) + " " + familyAccountTable.getValueAt(i, 2)
+ " " + familyAccountTable.getValueAt(i, 3));
}
outputStream.close();
} catch (FileNotFoundException er) {
JOptionPane.showMessageDialog(null, "파일을 여는데 문제가 생겼습니다. 프로그램을 종료합니다.");
System.exit(0);
} catch (IOException er) {
JOptionPane.showMessageDialog(null, "파일을 여는데 문제가 생겼습니다. 프로그램을 종료합니다.");
System.exit(0);
}
} else {
JOptionPane.showMessageDialog(null, "예상치 못한 에러 발생. 관리자에게 문의하세요.");
}
}
});
// 도움말
JMenuItem helpMenuItem = new JMenuItem("도움말");
systemMenu.add(helpMenuItem);
helpMenuItem.addActionListener(this);
JMenuBar menuBar = new JMenuBar();
menuBar.add(systemMenu);
setJMenuBar(menuBar);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String event = e.getActionCommand();
if (event == "도움말") {
} else {
JOptionPane.showMessageDialog(null, "예상치 못한 에러 발생. 관리자에게 문의하세요.");
}
}
public void totalCalculation() {
ROW = personalAccountTable.getRowCount();
int income = 0, consumption = 0, total = 0;
// 개인 합계 구하기
for (int i = 0; i < ROW; i++) {
if (((String) personalAccountTable.getValueAt(i, 1)).equals("수입") == true) {
income = income + stringToInt((String) personalAccountTable.getValueAt(i, 3));
} else if (((String) personalAccountTable.getValueAt(i, 1)).equals("지출") == true) {
consumption = consumption + stringToInt((String) personalAccountTable.getValueAt(i, 3));
} else {
JOptionPane.showMessageDialog(null, "수입 또는 지출 이외의 값이 개인테이블 안에 들어있습니다. 수정해주세요.");
}
}
total = income - consumption;
personalIncome.setText(intToString(income));
personalConsumption.setText(intToString(consumption));
personalTotal.setText(intToString(total));
// 변수 초기화
income = consumption = total = 0;
// 가족 개인 합계 구하기
ROW = familyAccountTable.getRowCount();
for (int i = 0; i < ROW; i++) {
if (((String) familyAccountTable.getValueAt(i, 1)).equals("수입") == true) {
income = income + stringToInt((String) familyAccountTable.getValueAt(i, 3));
} else if (((String) familyAccountTable.getValueAt(i, 1)).equals("지출") == true) {
consumption = consumption + stringToInt((String) familyAccountTable.getValueAt(i, 3));
} else {
JOptionPane.showMessageDialog(null, "수입 또는 지출 이외의 값이 가족테이블 안에 들어있습니다. 수정해주세요.");
}
}
total = income - consumption;
familyIncome.setText(intToString(income));
familyConsumption.setText(intToString(consumption));
familyTotal.setText(intToString(total));
}
public int stringToInt(String string) {
return Integer.parseInt(string);
}
public String intToString(int integer) {
return String.valueOf(integer);
}
}