-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomerDriver.java
More file actions
201 lines (154 loc) · 6.71 KB
/
Copy pathCustomerDriver.java
File metadata and controls
201 lines (154 loc) · 6.71 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
import java.util.*;
import javax.swing.JOptionPane;
public class CustomerDriver {
private static final Map<Integer, Customer> customers = new HashMap<>(); // User storage - <User ID - User Object>
Customer customer = null;
int number;
ArrayList<String> securityQuestions = new ArrayList<>() { {
add("1) In what city were you born?");
add("2) What is the name of your favorite pet?");
add("3) What is the name of your first school?");
}
};
Scanner sc = new Scanner(System.in);
public void printitem()
{
System.out.println(customers);
}
private final Scanner scan = new Scanner(System.in);
public void addCustomer(Customer customer) {
customers.putIfAbsent(customer.getId(), customer);
}
public Customer getCustomer(int id) {
return customers.get(id);
}
public void create() { //Create account
int id = 0;
String password = null;
String name;
String address;
long creditCardNumber = 0;
String securityQuestion;
String answer;
JOptionPane.showMessageDialog(null,"Create new account:");
// ID handling
id = enterID();
int length = String.valueOf(id).length();
while (!(length == 4)){
JOptionPane.showMessageDialog(null,"Invalid length of ID. Please try again.");
id = enterID();
length = String.valueOf(id).length();
}
while (checkID(id) || id == 0) {
JOptionPane.showMessageDialog(null,"ID value is already taken");
id = enterID();
}
// Password handling
while (Checker.checkPassword(password)) {
password = enterPassword();
if(Checker.checkPassword(password)){
JOptionPane.showMessageDialog(null,"Invalid Password!");
}
}
name = enterName();
address = enterAddress();
while (Checker.checkCard(creditCardNumber)){ //Assuming the cardnumber is 8digit
creditCardNumber = enterCreditCardNumber();
if (Checker.checkCard(creditCardNumber)){
JOptionPane.showMessageDialog(null,"Invalid Cardnumber.");
}
}
securityQuestion = selectSecurityQuestion();
answer = enterSecurityQuestionAnswer();
customer = new Customer(id, password, securityQuestion, answer, name, address, creditCardNumber);
// Confirmation
String input_conf = JOptionPane.showInputDialog("\n***************************************************************************************"
+customer.toString() +
"\n***************************************************************************************\n"+
"\nPlease confirm the above information to Create the account" +
"\nTo confirm press (y/n):");
//char confirmation = scan.next().toLowerCase().charAt(0);
if (input_conf.equals("y")){
addCustomer(customer);
JOptionPane.showMessageDialog(null,"--------------------Account successfully created-----------------------");
return;
}
//create();
}
public Customer login() { //login
int id;
String password;
JOptionPane.showMessageDialog(null,"\nLog in with credentials:");
id = enterID();
// User ID handling
if (getCustomer(id) == null || !customers.containsKey(id)) {
JOptionPane.showMessageDialog(null,"ID does not exist in the system.\nTry again.");
login();
}
password = enterPassword();
int attempts = 1; // Number of attempts user has taken to log in
// User password handling
while (!password.equals(getCustomer(id).getPassword())) {
if (attempts >= 3) {
JOptionPane.showMessageDialog(null,"Maximum attempts exceeded. Terminating program.Try again later.");
System.exit(0);
}
JOptionPane.showMessageDialog(null,"Incorrect password. Attempt " + attempts + "/3");
attempts++;
password = enterPassword();
}
//Security question handling
String reply = JOptionPane.showInputDialog(getCustomer(id).getQuestion() + "\nAnswer:");
if (reply.equals(getCustomer(id).getAnswer())){
JOptionPane.showMessageDialog(null,"\n------------------------------------------------Login successful---------------------------------------------" +
"\n--------------------------------------Welcome to Customer Order System!-----------------------------------------");
customer = getCustomer(id);
return customer;
}else{
JOptionPane.showMessageDialog(null,"Incorrect Answer, Try again later.\n Thank you.");
System.exit(0);
}
return customer;
}
public static void logout(){
JOptionPane.showMessageDialog(null,"Logged out successfully!");
}
private String selectSecurityQuestion() {
String selection = JOptionPane.showInputDialog("Select a security question: "
+ "\n1) In what city were you born?" + "\n2) What is the name of your favorite pet?"
+ "\n3) What is the name of your first school?");
//
// for (String securityQuestion : securityQuestions) {
// System.out.println(securityQuestion);
// }
int selection_int = Integer.parseInt(selection);
return securityQuestions.get(selection_int -1); //for index starting at 0
}
private int enterID() {
String input_id = JOptionPane.showInputDialog("\nEnter the 4 digit Id: ");
return Integer.parseInt(input_id);
}
private String enterPassword() {
String input_pass = JOptionPane.showInputDialog("Enter a password: \n(Password must be at least a digit, a special character( @, #, $, %, &, and *) and upper case Alphabet)");
return input_pass;
}
private String enterName() {
String input_name = JOptionPane.showInputDialog("Enter your name: ");
return input_name;
}
private String enterAddress() {
String input_add = JOptionPane.showInputDialog("Enter your address: ");
return input_add;
}
private long enterCreditCardNumber() {
String input_card = JOptionPane.showInputDialog("Enter your credit card number(8 digit): ");
return Long.parseLong(input_card);
}
private String enterSecurityQuestionAnswer() {
String input_ans = JOptionPane.showInputDialog("Enter the answer: ");
return input_ans;
}
private boolean checkID(int id) {
return customers.containsKey(id);
}
}