-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
167 lines (141 loc) · 5.45 KB
/
Copy pathGUI.java
File metadata and controls
167 lines (141 loc) · 5.45 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
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.io.*;
class GUI extends Frame {
private final TextField
nameField = new TextField(),
addressField = new TextField(),
telephoneField = new TextField(),
emailField = new TextField();
private final List customerNames = new List(CustomerList.LINE_COUNT);
private final Button
homeButton = new Button("Home"),
addButton = new Button("Add"),
removeButton = new Button("Remove"),
showButton = new Button("Show"),
exportButton = new Button("Export");
// define an export method so that listener object can call this method when used by the exportedButton.
static final String SEPARATOR = "\t";
static void writeToCSV(Vector<Customer> customers){
try{
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("/Users/yifan/Desktop/Output.csv"), "UTF-8"));
//bw.newLine();
for (Customer customer : customers){
StringBuffer oneLine = new StringBuffer();
oneLine.append(customer.getName().trim().length() == 0 ? "" : customer.getName());
oneLine.append(SEPARATOR);
oneLine.append(customer.getAddress().trim().length() == 0 ? "" : customer.getAddress());
oneLine.append(SEPARATOR);
oneLine.append(customer.getTelephone().trim().length() == 0 ? "" : customer.getTelephone());
oneLine.append(SEPARATOR);
oneLine.append(customer.getEmail().trim().length() == 0 ? "" : customer.getEmail());
oneLine.append(SEPARATOR);
bw.write(oneLine.toString());
bw.newLine();
}
bw.flush();
bw.close();
}
catch (UnsupportedEncodingException e) {System.out.println(e.getMessage());}
catch (FileNotFoundException e){System.out.println(e.getMessage());}
catch (IOException e){System.out.println(e.getMessage());}
}// end of writeCSV
GUI(int lines){
Panel panel = new Panel(new GridLayout(4,2));// HAVE TO USE the GirdLayout to create 4 rows and 2 columns for the panel
Label nameLabel = new Label("Name:");
Label addressLabel = new Label("Address:");
Label telephoneLabel = new Label("Telephone:");
Label emailLabel = new Label("Email:");
panel.add(nameLabel);
panel.add(nameField);
panel.add(addressLabel);
panel.add(addressField);
panel.add(telephoneLabel);
panel.add(telephoneField);
panel.add(emailLabel);
panel.add(emailField);
add(panel,"North"); // PLACE THE PANEL TO THE GUI North division
panel = new Panel();
Label namesLabel = new Label("Names:");
panel.add(namesLabel);
customerNames.setSize(100,50);
panel.add(customerNames);
add(panel,"Center"); // PLACE THE PANEL TO THE GUI Center division
panel = new Panel();
panel.add(homeButton);
panel.add(addButton);
panel.add(removeButton);
panel.add(showButton);
panel.add(exportButton);
add(panel,"South"); // PLACE THE PANEL TO THE GUI South division
// add the logic to the buttons by delegating listener class/interface object
addButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Customer customer = new Customer();
customer.name = nameField.getText();
customer.address = addressField.getText();
customer.telephone = telephoneField.getText();
customer.email = emailField.getText();
CustomerList.customers.addElement(customer);
customerNames.addItem(customer.name);
// when added, empty the TextFields!
nameField.setText("");
addressField.setText("");
telephoneField.setText("");
emailField.setText("");
}
});
removeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int index = customerNames.getSelectedIndex();
Customer customer = (Customer) CustomerList.customers.elementAt(index);
if(index<0) return;
//before removing, set the TextField to the values of the current Customer object so that we can create it again once mis-deleted
nameField.setText(customer.name);
addressField.setText(customer.address);
telephoneField.setText(customer.telephone);
emailField.setText(customer.email);
CustomerList.customers.removeElementAt(index);
customerNames.remove(index);
}
});
showButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int index = customerNames.getSelectedIndex();
Customer customer = (Customer) CustomerList.customers.elementAt(index);
if(index<0) return;
nameField.setText(customer.name);
nameField.show();
addressField.setText(customer.address);
addressField.show();
telephoneField.setText(customer.telephone);
telephoneField.show();
emailField.setText(customer.email);
emailField.show();
}
});
homeButton.addActionListener(new ActionListener(){ // home button essentially acting like a cleaner
public void actionPerformed(ActionEvent e){
nameField.setText("");
nameField.show();
addressField.setText("");
addressField.show();
telephoneField.setText("");
telephoneField.show();
emailField.setText("");
emailField.show();
}
});
exportButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//Vector<Customer> customers = CustomerList.customers;
GUI.writeToCSV(CustomerList.customers);
}
});
}
GUI(String title, int lines){
this(lines);
setTitle(title);
}
}