forked from codehouseindia/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNameValidation_using_stack.java
More file actions
141 lines (128 loc) · 3.76 KB
/
NameValidation_using_stack.java
File metadata and controls
141 lines (128 loc) · 3.76 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
//conatct class
public class Contact{
static String fname,lname,dob,email,tel,mob;
Contact(String fname,String lname,String dob,String email,String tel,String mob){
this.fname=fname;
this.lname=lname;
this.dob=dob;
this.email=email;
this.tel=tel;
this.mob=mob;
}
public static void validate(String fname,String lname,String dob,String email,String tel,String mob) throws NotValidException{
if(fname.length()==0 || fname.length()==0 || fname.length()==0 || fname.length()==0){
throw new NotValidException("Not valid");
}
else if(tel.length()==0 && mob.length()==0){
throw new NotValidException("Either telephone or mobile number must exist");
}
else if(email != null){
String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\."+
"[a-zA-Z0-9_+&*-]+)*@" +
"(?:[a-zA-Z0-9-]+\\.)+[a-z" +
"A-Z]{2,7}$";
if(email.matches(emailRegex))
System.out.println("Email is valid");
else
throw new NotValidException("Not a valid email-id");
}
else{
System.out.println("Accepted");
}
}
public static void main(String[] args)throws NotValidException {
Contact c=new Contact("amit","mitra","12-12-1998","amit14mitra@gmail.com","03332561254","9854123674");
try{
validate(fname,lname,dob,email,tel,mob);
}
catch(NotValidException e){
e.printStackTrace();
//System.out.println("Not valid :"+e);
}
finally{
System.out.println("Finally block executed");
}
}
}
// name validation using stack program
public class Stack extends Contact{
int size;
String arr[];
int top;
Stack(String fname,String lname,String dob,String email,String tel,String mob,int size) {
super(fname,lname,dob,email,tel,mob);
this.size = size;
this.arr = new String[size];
this.top = -1;
}
private static void validate1(String pushElement) throws NotValidException {
String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\."+
"[a-zA-Z0-9_+&*-]+)*@" +
"(?:[a-zA-Z0-9-]+\\.)+[a-z" +
"A-Z]{2,7}$";
if(pushElement.length()==0)
throw new NotValidException("It is not valid!!!");
}
public void push(String pushedElement)throws NotValidException {
try{
validate1(pushedElement);
}
catch(NotValidException e){
e.printStackTrace();
}
if (top != size-1) {
top++;
arr[top] = pushedElement;
System.out.println("Pushed element:" + pushedElement);
} else {
try{
throw new overflowException("overflow occurs!!!");
}
catch(overflowException e){
e.printStackTrace();
}
}
}
public void pop() {
if (top >= 0) {
int returnedTop = top;
top--;
System.out.println("Popped element :" + arr[returnedTop]);
//return arr[returnedTop];
} else {
try{
throw new underflowException("underflow occurs!!!");
}
catch(underflowException e){
e.printStackTrace();
}
//return -1;
}
}
public static void main(String[] args) throws NotValidException {
Stack s = new Stack("amit","mitra","12-12-1998","amit14mitra@gmail.com","03332561254","9854123674",10);
Contact c=new Contact("amit","mitra","12-12-1998","amit14mitra@gmail.com","03332561254","9857463214");
//StackCustom.pop();
System.out.println("=================");
s.push(c.fname);s.push(c.lname);s.push(c.dob);s.push(c.email);s.push(c.tel);s.push(c.mob);
System.out.println("=================");
s.pop();s.pop();s.pop();s.pop();s.pop();s.pop();
//System.out.println(arr[top]);
}
}
// All exceptions underflow/overflow/notvalid exception
public class overflowException extends Exception {
public overflowException(String s){
super(s);
}
}
public class NotValidException extends Exception {
public NotValidException(String s) {
super(s);
}
}
public class underflowException extends Exception {
public underflowException(String s){
super(s);
}
}