-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccountCreator.java
241 lines (198 loc) · 7.17 KB
/
AccountCreator.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
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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class AccountCreator extends JFrame
{
private static String AdminAccess;
private JTextField firstNameField;
private JTextField lastNameField;
private JTextField username;
private JTextField password;
private JPanel jPanel;
private static File usersFile=new File("./Users");
private static final File dataBase=new File("./dataBase");
private ArrayList<Customer> CustomerArray;
public AccountCreator(Boolean adminAccess)
{
String Admin=Boolean.toString(adminAccess);
setAdminAccess(Admin);
setTitle("WAPA");
setSize(400,400);
JLabel customer=new JLabel("Create Account");
add(customer);
buildAccountPanel();
add(jPanel);
setVisible(true);
}
public static void setAdminAccess(String adminAccess)
{
AdminAccess = adminAccess;
}
public static String getAdminAccess()
{
return AdminAccess;
}
public void buildAccountPanel()
{
JLabel FirstName=new JLabel("First Name:");
firstNameField=new JTextField(10);
JLabel jLabel=new JLabel("Last Name:");
lastNameField=new JTextField(10);
JLabel jLabel1=new JLabel("Username:");
username=new JTextField(10);
JLabel jLabel2=new JLabel("Password:");
password=new JTextField(10);
JButton jButton=new JButton("Create Account");
JButton jButton1=new JButton("Back");
jButton1.addActionListener((new backListener()));
jButton.addActionListener(new createListener());
jPanel=new JPanel();
jPanel.setLayout(new GridLayout(5,2));
jPanel.add(FirstName);
jPanel.add(firstNameField);
jPanel.add(jLabel);
jPanel.add(lastNameField);
jPanel.add(jLabel1);
jPanel.add(username);
jPanel.add(jLabel2);
jPanel.add(password);
jPanel.add(jButton);
jPanel.add(jButton1);
}
private class backListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
Boolean adminAcess=Boolean.valueOf(getAdminAccess());
try
{
if (adminAcess)
{
setVisible(false);
AdminFrame b=new AdminFrame();
}
else
{
setVisible(false);
LoginFrame a=new LoginFrame();
}
}
catch (Exception ex)
{
System.out.println(ex+ "Caught");
}
}
}
public static void writeObjectToFile(Customer serObj,int lineNum)
{
try {
String FileName = String.valueOf(lineNum) + ".dat";
File objFile = new File(dataBase, FileName);
if (objFile.createNewFile())
{
FileOutputStream fileOut = new FileOutputStream(objFile);
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(serObj);
objectOut.close();
System.out.println("The Object was successfully written to a file");
}
else if (!objFile.createNewFile())
{
FileOutputStream fileOut = new FileOutputStream(objFile);
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(serObj);
objectOut.close();
System.out.println("The Object was successfully saved to an existing file");
}
else
{
System.out.println("File Creation Error");
}
}
catch (Exception ex)
{
System.out.println(ex);
}
}
public int fileNum(File file)throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(file));
int lines = 0;
while (reader.readLine() != null) lines++;
reader.close();
return lines;
}
public static void createUserOjects(ArrayList CustomerArray)throws IOException
{
Scanner dataReader=new Scanner(usersFile);
while (dataReader.hasNext())
{
String fName=dataReader.next();
String lName=dataReader.next();
boolean meter=Boolean.parseBoolean(dataReader.next());
String user=dataReader.next();
String pass=dataReader.next();
boolean adminCheck=Boolean.parseBoolean(dataReader.next());
int ID=Integer.parseInt(dataReader.next());
int meterNum=Integer.parseInt(dataReader.next());
int balance=Integer.parseInt(dataReader.next());
Customer one = new Customer(fName,lName,meter,user,pass,adminCheck,ID,meterNum,balance);
writeObjectToFile(one,ID);
try
{
CustomerArray.add(one);
}
catch (Exception ex)
{
System.out.println(ex);
}
}
}
public ArrayList<Customer> getCustomerArray()
{
return CustomerArray;
}
private class createListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
try
{
Random ranNum=new Random();
int num=ranNum.nextInt(500);
String user=username.getText();
String pass=password.getText();
String fName=firstNameField.getText();
String lName=lastNameField.getText();
PrintWriter loginWriter = new PrintWriter(new FileOutputStream(usersFile, true ));
loginWriter.println(fName+" " +lName+" "+ " false" +" "+ user+ " " +pass+ " " + AdminAccess + " " + fileNum(usersFile) + " " + num + " 200");
loginWriter.close();
boolean AdminMessage= Boolean.valueOf(AdminAccess);
if (AdminMessage)
{
setVisible(false);
JOptionPane.showMessageDialog(null, "Admin Account Created: Welcome Admin "+ fName);
AdminFrame main=new AdminFrame();
}
else
{
setVisible(false);
JOptionPane.showMessageDialog(null,"Account Created/Proceed to Login" + " Meter Status:Disconnected" + " Meter #" + num +" Start up Fee Due: 200");
LoginFrame main=new LoginFrame();
}
createUserOjects(CustomerArray);
}
catch (Exception ex)
{
System.out.println(ex+ "Caught");
}
}
}
}