-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidation.java
More file actions
427 lines (406 loc) · 14.3 KB
/
Validation.java
File metadata and controls
427 lines (406 loc) · 14.3 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import java.util.regex.*;
/**
* Write a description of class Validation here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Validation
{
/**
* A default constructor for objects of class Validation
*/
public Validation()
{
}
/**
* Create a validateAmount method to validate Amount.
*/
public boolean validateAmount(String amount)
{
amount = amount.trim();
if (amount.length() == 0)
{
System.out.println("No amount was entered. Please try again(Enter \"X\" to cancel):");
return false;
}
else if (amount.matches("^[0-9]+$") || amount.matches("^[0-9]+.[0-9]*$"))
{
if (Double.valueOf(amount) > 0)
return true;
else
{
System.out.println("Amount should be a number larger than 0. Please try again(Enter \"X\" to cancel):");
return false;
}
}
else
{
System.out.println("Amount should be a number larger than 0. Please try again(Enter \"X\" to cancel):");
return false;
}
}
/**
* Create a validateInteger method to check whether the inputted value is an integer.
*/
public boolean validateInteger(String value)
{
value = value.trim();
if (value.matches("^[1-9]+[0-9]*$") || value.equals("x"))
return true;
else
{
System.out.println("You can only input digits! Please try again");
return false;
}
}
/**
* Create a validatePassword method to validate Password.
*/
public boolean validatePassword(String password)
{
password = password.trim();
if (password.length() < 8 || password.length() > 20)
{
System.out.println("The length of password should be between 8 to 20. Please try again(Enter \"X\" to cancel):");
return false;
}
int index = 0;
if (password.matches(".*[a-z]{1,}.*"))
index++;
else
{
System.out.println("There should be at least one lowercase in the password. Please try again(Enter \"X\" to cancel):");
return false;
}
if (password.matches(".*[A-Z]{1,}.*"))
index++;
else
{
System.out.println("There should be at least one uppercase in the password. Please try again(Enter \"X\" to cancel):");
return false;
}
if (password.matches(".*\\d{1,}.*"))
index++;
else
{
System.out.println("There should be at least one digit in the password. Please try again(Enter \"X\" to cancel):");
return false;
}
if (password.matches(".*[_\\W]{1,}.*"))
index++;
else
{
System.out.println("There should be at least one special character in the password. Please try again(Enter \"X\" to cancel):");
return false;
}
if (index == 4)
return true;
else
{
System.out.println("There should be at least one special character, one digit, one uppercase and one lowercase in the password. Please try again(Enter \"X\" to cancel):");
return false;
}
}
/**
* Create a validateProductAmount method to validate product amount.
*/
public boolean validateProductAmount(String amount)
{
if (amount.length() == 0)
{
System.out.println("No amount was entered. Please try again(Enter \"X\" to cancel):");
return false;
}
else if (amount.matches("^[0-9]+$") || amount.matches("^[0-9]*\\.[0-9]*$"))
{
if (Double.valueOf(amount) > 0)
return true;
else
{
System.out.println("Amount should be a number larger than 0. Please try again(Enter \"X\" to cancel):");
return false;
}
}
else
{
System.out.println("Amount should be a number larger than 0. Please try again(Enter \"X\" to cancel):");
return false;
}
}
/**
* Create a validateProductCategory method to validate Product Category.
*/
public boolean validateProductCategory(String category)
{
category = category.trim();
if(category.toLowerCase().equals("fruit") || category.toLowerCase().equals("vegetable"))
return true;
else
{
System.out.println("Please enter fruit or vegetable(Enter \"X\" to cancel):");
return false;
}
}
/**
* Create a validateProductDiscount method to validate product Discount.
*/
public boolean validateProductDiscount(String productDiscount)
{
int a = 0;
productDiscount = productDiscount.trim();
if (productDiscount.length() == 0)
{
System.out.println("No discount was entered. Please try again(Enter \"X\" to cancel):");
return false;
}
for (int i=0; i < productDiscount.length(); i++)
{
if(!Character.isDigit(productDiscount.charAt(i)))
{
if((productDiscount.charAt(i)) != ('.'))
{
System.out.println("Please input a valid product discount in digits(Enter \"X\" to cancel):");
return false;
}
else if ((productDiscount.charAt(i)) == ('.'))
a++;
}
}
if (a > 1)
{
System.out.println("There should be only one decimal point. Please try again(Enter \"X\" to cancel):");
return false;
}
double d = Double.parseDouble(productDiscount);
if (d <= 0 || d > 1)
{
System.out.println("Please input a valid product discount which is between 0(exclude) to 1(include)(Enter \"X\" to cancel):");
return false;
}
else
return true;
}
/**
* Create a validateProductName method to validate Product Name.
*/
public boolean validateProductName(String productName)
{
productName = productName.trim();
if(productName.length() == 0)
{
System.out.println("No product name was entered. Please try again(Enter \"X\" to cancel):");
return false;
}
else if (productName.length() >= 15)
{
System.out.println("The length of product name should between 1 to 15. Please try again(Enter \"X\" to cancel):");
return false;
}
else if (productName.matches(".*[a-zA-Z]{2,}.*") == false)
{
System.out.println("There should be at least two adjacent letters in product name. Please try again(Enter \"X\" to cancel):");
return false;
}
return true;
}
/**
* Create a validateProductOrigin method to validate product Origin.
*/
public boolean validateProductOrigin(String productOrigin)
{
productOrigin = productOrigin.trim();
if (productOrigin.length() < 2 ||
productOrigin.length() > 20)
{
System.out.println("The length of product origin should be between 2 to 20. Please try again(Enter \"X\" to cancel):");
return false;
}
else if (productOrigin.matches(".*[a-zA-Z]{2,}.*")==false)
{
System.out.println("There should be at least two adjacent letters in the product origin. Please try again(Enter \"X\" to cancel):");
return false;
}
else
return true;
}
/**
* Create a validateProductPrice method to validate Product Price.
*/
public boolean validateProductPrice(String price)
{
price = price.trim();
if (price.length() == 0)
{
System.out.println("No price was entered. Please try again(Enter \"X\" to cancel):");
return false;
}
else if (price.matches("^[0-9]+$") || price.matches("^[0-9]+.[0-9]*$"))
{
if (Double.valueOf(price) > 0)
return true;
else
{
System.out.println("Price should be a number larger than 0. Please try again(Enter \"X\" to cancel):");
return false;
}
}
else
{
System.out.println("Price should be a number larger than 0. Please try again(Enter \"X\" to cancel):");
return false;
}
}
/**
* Create a validateProductShelfLife method to validate product Shelf Life.
*/
public boolean validateProductShelfLife(String productShelfLife)
{
productShelfLife = productShelfLife.trim();
if (productShelfLife.length() == 0)
{
System.out.println("No shelf life was entered. Please try again(Enter \"X\" to cancel):");
return false;
}
for (int i = 0; i < productShelfLife.length(); i++)
{
if (!Character.isDigit(productShelfLife.charAt(i)))
{
System.out.println("Please input a valid product shelf life in digits(Enter \"X\" to cancel):");
return false;
}
else
return true;
}
int i = Integer.parseInt(productShelfLife);
if (i <= 0 )
{
System.out.println("Please input a valid product shelf life which is higer than 0(Enter \"X\" to cancel):");
return false;
}
else
return true;
}
/**
* Create a validateUserAddress method to validate User Address.
*/
public boolean validateUserAddress(String address)
{
address = address.trim();
if(address.length() == 0)
{
System.out.println("No address was entered. Please try again(Enter \"X\" to cancel):");
return false;
}
else if (address.length() >= 50)
{
System.out.println("The length of address should less than 50. Please try again(Enter \"X\" to cancel):");
return false;
}
else if (address.matches(".*[a-zA-Z]{2,}.*") == false)
{
System.out.println("There should be at least two adjacent letters in address. Please try again(Enter \"X\" to cancel):");
return false;
}
return true;
}
/**
* Create a validateUserEmail method to validate User Email.
*/
public boolean validateUserEmail(String email)
{
email = email.trim();
if (email.length() == 0)
{
System.out.println("No email was entered. Please try again(Enter \"X\" to cancel):");
return false;
}
String regex = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(email);
if (!m.matches())
{
System.out.println("Please enter the correct email format(Example: someone@somesite.com)(Enter \"X\" to cancel):");
return false;
}
return true;
}
/**
* Create a validateUserName method to validate User Name.
*/
public boolean validateUserName(String userName)
{
int hyphenNumber = 0;//The number of hyphen in the player name.
int alphabetNumber = 0;//The number of alphabet in the player name.
for (int i = 0;i < userName.length();i++)
{
if (userName.toUpperCase().charAt(i) >= 'A' && userName.toUpperCase().charAt(i) <= 'Z')
alphabetNumber++;
if (userName.charAt(i) == '-')
hyphenNumber++;
}
if (alphabetNumber + hyphenNumber != userName.length())
{
System.out.println("Error!Player name can contain only alphabetical characters and hyphen. Please try again(Enter \"X\" to cancel):");
return false;
}
if (alphabetNumber < 2)
{
System.out.println("Error!At least two alphabetical characters should be used. Please try again(Enter \"X\" to cancel):");
return false;
}
if (alphabetNumber + hyphenNumber > 20)
{
System.out.println("Error!Name length can't be longer than 20. Please try again(Enter \"X\" to cancel):");
return false;
}
if (hyphenNumber > 1 || userName.charAt(0) == '-' || userName.charAt(userName.length() - 1) == '-')
{
System.out.println("Error!Hyphen can't be at begin or end of the name!At most one hyphen can be used. Please try again(Enter \"X\" to cancel):");
return false;
}
return true;
}
/**
* Create a validateUserPhone method to validate User Phone.
*/
public boolean validateUserPhone(String phone)
{
phone = phone.trim();
if (phone.length() == 0)
{
System.out.println("No phone was entered. Please try again(Enter \"X\" to cancel):");
return false;
}
else if (phone.length() >= 20)
{
System.out.println("The length of phone should less than 20. Please try again(Enter \"X\" to cancel):");
return false;
}
else if(phone.replaceAll("[_\\W]","").length() != phone.length())
{
System.out.println("The phone number cannot contain special characters. Please try again(Enter \"X\" to cancel):");
return false;
}
else if (phone.matches(".*[a-zA-Z]{1,}.*"))
{
System.out.println("The phone number cannot contain letters. Please try again(Enter \"X\" to cancel):");
return false;
}
return true;
}
/**
* Create a validateYN method to validate input choice (yes or no).
*/
public boolean validateYN(String option)
{
option = option.toUpperCase().trim();
if (option.equals("Y") || option.equals("N"))
return true;
else
{
System.out.println("You can only choose \"y\" or \"n\". Please try again:");
return false;
}
}
}