-
Notifications
You must be signed in to change notification settings - Fork 0
/
Validity.java
66 lines (58 loc) · 2.08 KB
/
Validity.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
import java.util.Scanner;
import java.util.regex.Pattern;
public class Validity {
public static final String RED="\u001B[31m";
public static final String RESET="\u001B[0m";
// set validation from pattern class of util package
private static Pattern PublishYear_Pattern=Pattern.compile("^\\d{4}$");
private static Pattern AuthorTitle_Pattern=Pattern.compile("^[a-zA-Z ]+$");
private static Pattern ID_PATTERN = Pattern.compile("^\\d{1,4}$");
Scanner sc = new Scanner(System.in);
//check book id - only valid in digits
public String validateId() {
String bookid;
while (true) {
System.out.println("Enter Book ID ");
bookid = sc.nextLine();
if (!ID_PATTERN.matcher(bookid).matches()) {
System.out.println(RED+" SORRY ! PLEASE ENTER VALID BOOK ID "+RESET);
} else {
break;
}
}
return bookid;
}
// check book author & title - only valid in character capital/small
public String validateAuthorTitle(String input){
String result;
while (true){
if(input == "Title"){
System.out.println("Enter Title");
}else{
System.out.println("Enter Author");
}
result=sc.nextLine();
if(!AuthorTitle_Pattern.matcher(result).matches()){
System.out.println(RED+"Please Enter Valid AuthorTitle"+input+RESET);
} else {
break;
}
}
return result;
}
// check book publish year - only valid in 4 digits
public String validatePublishYear(){
String year;
while(true){
System.out.println("Enter Publish Year of Book ");
year=sc.nextLine();
if(!PublishYear_Pattern.matcher(year).matches()){
System.out.println(RED+"Enter valid Publish Year"+RESET);
}
else{
break;
}
}
return year;
}
}