-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.java
More file actions
317 lines (297 loc) · 6.69 KB
/
File.java
File metadata and controls
317 lines (297 loc) · 6.69 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
package hw5;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.io.*;
public class File extends JFrame implements ActionListener
{
private JTextField eid;
private JTextField en;
private JTextField es;
private JPanel panel4;
private JButton undo;
private JButton save;
private JLabel label;
private ArrayList<Employee> list=new ArrayList<Employee>();
private Employee employee;
private int number=0;
private int currentnumber=0;
public File()
{
setSize(1000,300);
setTitle("Employee Data");
Container content = getContentPane();//
content.setBackground(Color.LIGHT_GRAY);
content.setLayout(new BorderLayout());
label=new JLabel();
label.setForeground(Color.WHITE);
content.add(label, BorderLayout.CENTER);
JPanel panel1=new JPanel();
panel1.setBackground(Color.YELLOW);
content.add(panel1,BorderLayout.NORTH);
panel1.setLayout(new GridLayout(2,2));
JPanel panel11=new JPanel();
panel11.setBackground(Color.yellow);
panel11.setLayout(new FlowLayout());
panel1.add(panel11);
JLabel label1=new JLabel("Employee ID:");
panel11.add(label1);
eid=new JTextField(10);
panel11.add(eid);
JPanel panel12=new JPanel();
panel12.setBackground(Color.yellow);
panel12.setLayout(new FlowLayout());
panel1.add(panel12);
JLabel label2=new JLabel("Employee Name:");
panel12.add(label2);
en=new JTextField(30);
panel12.add(en);
JPanel panel13=new JPanel();
panel13.setBackground(Color.yellow);
panel13.setLayout(new FlowLayout());
panel1.add(panel13);
JLabel label3=new JLabel("Employee Salary:");
panel13.add(label3);
es=new JTextField(6);
panel13.add(es);
JPanel panel2=new JPanel();
content.add(panel2,BorderLayout.SOUTH);
panel2.setLayout(new BorderLayout());
JPanel panel3=new JPanel();
panel3.setBackground(Color.cyan);
panel2.add(panel3,BorderLayout.WEST);
JButton next=new JButton("Next");
next.addActionListener(this);
panel3.add(next);
JButton previous=new JButton("Previous");
previous.addActionListener(this);
panel3.add(previous);
panel4=new JPanel();
panel4.setBackground(Color.cyan);
panel2.add(panel4,BorderLayout.EAST);
undo=new JButton("Undo");
undo.addActionListener(this);
panel4.add(undo);
save=new JButton("Save");
save.addActionListener(this);
panel4.add(save);
JMenu file=new JMenu("File");
JMenuBar menubar=new JMenuBar();
menubar.add(file);
setJMenuBar(menubar);
JMenu modify=new JMenu("Modify");
file.add(modify);
JMenuItem edit=new JMenuItem("Edit");
edit.addActionListener(this);
modify.add(edit);
JMenu misc=new JMenu("Misc");
file.add(misc);
JMenuItem exit=new JMenuItem ("Exit");
exit.addActionListener(this);
misc.add(exit);
addWindowListener(new WindowDestroyer());
}
public void actionPerformed(ActionEvent e)
{
Container content = getContentPane();
String action=e.getActionCommand();
if (action.equals("Next"))
{
next();
}
else if (action.equals("Previous"))
{
previous();
}
else if (action.equals("Undo"))
{
editundo();
browse();
}
else if (action.equals("Save"))
{
editsave();
browse();
}
else if (action.equals("Edit"))
{
edit();
}
else if (action.equals("Exit"))
{
savedata();
System.exit(0);
}
else
{
System.out.println("error");
}
}
public void next()
{
if(list.isEmpty())
{
System.out.println("Empty");
}
else
{
if(currentnumber==(number-1))
{
label.setText("Already at last record");
}
else
{
currentnumber++;
label.setText(" ");
//1//write
//this.write();
//2//next
this.setText(employee);
}
}
}
public void previous()
{
if(currentnumber==0)
{
label.setText("Already at first record");
}
else
{
currentnumber--;
label.setText(" ");
this.setText(employee);
}
}
public void write()
{
edit();
int id=Integer.parseInt(eid.getText());
String name=en.getText();
double salary=Double.parseDouble(es.getText());
Employee saveemployee=new Employee(id,name,salary);
list.add(currentnumber,saveemployee);
System.out.println(list.get(currentnumber));
System.out.println(currentnumber);
}
public void edit()
{
eid.setEditable(true);
en.setEditable(true);
es.setEditable(true);
panel4.setVisible(true);
undo.setVisible(true);
save.setVisible(true);
//System.out.println("edit");
}
public void editsave()
{
if(list.isEmpty())
{
int id=Integer.parseInt(eid.getText());
String name=en.getText();
double salary=Double.parseDouble(es.getText());
Employee saveemployee=new Employee(id,name,salary);
list.add(currentnumber,saveemployee);
}
else
{
employee = ((Employee)list.get(currentnumber));
employee.setData(Integer.parseInt(eid.getText()), en.getText(), Double.parseDouble(es.getText()));
browse();
}
//System.out.println(list.get(currentnumber));
}
public void editundo()
{
setText(employee);
}
public void firststep()
{
readdata(list);
setText(employee);
browse();
}
public void readdata(ArrayList<Employee> read)
{
ObjectInputStream readData;
try
{
readData=new ObjectInputStream(new FileInputStream("employeedata.dat"));
try
{
while(true)
{
read.add((Employee)readData.readObject());
number++;
}
}
catch(EOFException e1){}
catch(Exception e2)
{
System.out.println(e2.getMessage()+"2");
}
System.out.println("Read " + number + " records");
readData.close();
}
catch(FileNotFoundException e3)
{
System.out.println(e3.getMessage()+"3");
}
catch(IOException e4)
{
System.out.println(e4.getMessage()+"4");
}
}
public void savedata()
{
ObjectOutputStream saveData=null;
try{
saveData=new ObjectOutputStream(new FileOutputStream("employeedata.dat", false));
for(Employee saveemployee:list)
{
saveData.writeObject(saveemployee);
}
//System.out.println("Wrote " + currentnumber + " records");
System.out.println("Wrote " + number + " records");
saveData.close();
}
catch(Exception e)
{
System.out.println(e.getMessage()+"save");
}
}
public void setText(Employee employeeText)
{
if (list.isEmpty())
{
System.out.println("No records in the Employee List.");
System.out.println("Please click\"Edit\"");
System.out.println("and then write information.");
}
else
{
employeeText=((Employee)list.get(currentnumber));
eid.setText(Integer.toString(employeeText.getEID()));
en.setText(employeeText.getEName());
es.setText(Double.toString(employeeText.getESalary()));
}
}
public void browse()
{
eid.setEditable(false);
en.setEditable(false);
es.setEditable(false);
panel4.setVisible(false);
undo.setVisible(false);
save.setVisible(false);
}
public static void main(String[] args)
{
File file=new File();
file.firststep();
file.setVisible(true);
}
}