-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuyerSignUpWindow.java
194 lines (145 loc) · 5.81 KB
/
BuyerSignUpWindow.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
package C212Amazon;
import javax.swing.*;
import java.awt.*;
/**
* GUI for making a new buyer.
* @author Syed Turab Ali Jafri
* 4/26/2017
*/
public class BuyerSignUpWindow extends SignUpWindow {
private BuyerDataWriter buyerdatawriter = new BuyerDataWriter();
private BuyerDataReader buyerdatareader = new BuyerDataReader();
private JLabel firstNameLabel = new JLabel("First Name: ");
private JTextField firstNameTextField = new JTextField(20);
private JLabel lastNameLabel = new JLabel("Last Name: ");
private JTextField lastNameTextField = new JTextField(20);
private JLabel phoneLabel = new JLabel("Phone: ");
private JTextField phoneTextField = new JTextField(20);
public BuyerSignUpWindow() {
super("Buyer");
setPanelSizeAndLayout();
setLeftPanel();
setRightPanel();
setBottomPanel();
signUpButton.addActionListener(e -> setSignUpButton());
setLocationRelativeTo(null);
setVisible(true);
}
protected void setLeftPanel() {
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new GridLayout(9, 2));
leftPanel.add(new JPanel());
leftPanel.add(new JPanel());
firstNameLabel.setHorizontalAlignment(4);
leftPanel.add(new JPanel());
leftPanel.add(firstNameLabel);
lastNameLabel.setHorizontalAlignment(4);
leftPanel.add(new JPanel());
leftPanel.add(lastNameLabel);
usernameLabel.setHorizontalAlignment(4);
leftPanel.add(new JPanel());
leftPanel.add(usernameLabel);
passwordLabel.setHorizontalAlignment(4);
leftPanel.add(new JPanel());
leftPanel.add(passwordLabel);
confirmPasswordLabel.setHorizontalAlignment(4);
leftPanel.add(new JPanel());
leftPanel.add(confirmPasswordLabel);
phoneLabel.setHorizontalAlignment(4);
leftPanel.add(new JPanel());
leftPanel.add(phoneLabel);
emailAddressLabel.setHorizontalAlignment(4);
leftPanel.add(new JPanel());
leftPanel.add(emailAddressLabel);
leftPanel.add(new JPanel());
leftPanel.add(signUpButton);
mainPanel.add(leftPanel, BorderLayout.WEST);
}
protected void setRightPanel() {
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new GridLayout(9, 2));
rightPanel.add(new JPanel());
rightPanel.add(new JPanel());
rightPanel.add(firstNameTextField);
rightPanel.add(new JPanel());
rightPanel.add(lastNameTextField);
rightPanel.add(new JPanel());
rightPanel.add(usernameTextField);
rightPanel.add(new JPanel());
rightPanel.add(passwordTextField);
rightPanel.add(new JPanel());
rightPanel.add(confirmedPasswordTextField);
rightPanel.add(new JPanel());
rightPanel.add(phoneTextField);
rightPanel.add(new JPanel());
rightPanel.add(emailAddressTextField);
rightPanel.add(new JPanel());
mainPanel.add(rightPanel, BorderLayout.EAST);
}
private void setSignUpButton() {
JOptionPane jop = new JOptionPane();
int errors = 0;
String error = "";
boolean usernameExists = true;
if (buyerdatareader.verifyNewUsername("buyer", usernameTextField.getText())) {
usernameExists = false;
if (usernameTextField.getText().length() < 4) {
errors++;
error += "Invalid username (4)\n";
usernameTextField.setText("");
}
if (passwordTextField.getText().length() < 8) {
errors++;
error += "Invalid password (8)\n";
passwordTextField.setText("");
confirmedPasswordTextField.setText("");
}
else if(!confirmedPasswordTextField.getText().equals(passwordTextField.getText())) {
errors++;
error += "Passwords don't match.\n";
passwordTextField.setText("");
confirmedPasswordTextField.setText("");
}
if (firstNameTextField.getText().length() < 3) {
errors++;
error += "Invalid first name (3)\n";
firstNameTextField.setText("");
}
if (lastNameTextField.getText().length() < 3) {
errors++;
error += "Invalid last name (3)\n";
lastNameTextField.setText("");
}
if (phoneTextField.getText().length() < 10) {
errors++;
error += "Invalid phone number (10)\n";
phoneTextField.setText("");
}
if (emailAddressTextField.getText().length() < 5) {
errors++;
error += "Invalid email address (5)\n";
emailAddressTextField.setText("");
}
}
if(usernameExists) {
errors++;
error += "Username already exists!\n";
usernameTextField.setText("");
}
if(errors == 0) {
if(!buyerdatawriter.createBuyer(new Buyer(0,usernameTextField.getText(), passwordTextField.getText(),
firstNameTextField.getText(), lastNameTextField.getText(), phoneTextField.getText(),
emailAddressTextField.getText()))) {
jop.showMessageDialog(null, "Error while creating user. Please try again.",
"Writing Error", JOptionPane.ERROR_MESSAGE);
}
else {
jop.showMessageDialog(null, "Buyer created successfully!", "Success", JOptionPane.INFORMATION_MESSAGE);
dispose();
}
}
else {
jop.showMessageDialog(null, "Errors (" + errors + ")\n" + error, "Error", JOptionPane.ERROR_MESSAGE);
}
}
}