Skip to content

Commit 5444109

Browse files
authored
NameValidation using stack in java
1 parent 75e8996 commit 5444109

1 file changed

Lines changed: 141 additions & 0 deletions

File tree

NameValidation_using_stack.java

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
//conatct class
2+
public class Contact{
3+
4+
static String fname,lname,dob,email,tel,mob;
5+
Contact(String fname,String lname,String dob,String email,String tel,String mob){
6+
this.fname=fname;
7+
this.lname=lname;
8+
this.dob=dob;
9+
this.email=email;
10+
this.tel=tel;
11+
this.mob=mob;
12+
}
13+
public static void validate(String fname,String lname,String dob,String email,String tel,String mob) throws NotValidException{
14+
if(fname.length()==0 || fname.length()==0 || fname.length()==0 || fname.length()==0){
15+
throw new NotValidException("Not valid");
16+
}
17+
else if(tel.length()==0 && mob.length()==0){
18+
throw new NotValidException("Either telephone or mobile number must exist");
19+
}
20+
else if(email != null){
21+
String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\."+
22+
"[a-zA-Z0-9_+&*-]+)*@" +
23+
"(?:[a-zA-Z0-9-]+\\.)+[a-z" +
24+
"A-Z]{2,7}$";
25+
if(email.matches(emailRegex))
26+
System.out.println("Email is valid");
27+
else
28+
throw new NotValidException("Not a valid email-id");
29+
}
30+
else{
31+
System.out.println("Accepted");
32+
}
33+
}
34+
public static void main(String[] args)throws NotValidException {
35+
Contact c=new Contact("amit","mitra","12-12-1998","[email protected]","03332561254","9854123674");
36+
try{
37+
validate(fname,lname,dob,email,tel,mob);
38+
}
39+
catch(NotValidException e){
40+
e.printStackTrace();
41+
//System.out.println("Not valid :"+e);
42+
}
43+
finally{
44+
System.out.println("Finally block executed");
45+
}
46+
}
47+
48+
}
49+
50+
// name validation using stack program
51+
52+
public class Stack extends Contact{
53+
int size;
54+
String arr[];
55+
int top;
56+
57+
Stack(String fname,String lname,String dob,String email,String tel,String mob,int size) {
58+
super(fname,lname,dob,email,tel,mob);
59+
this.size = size;
60+
this.arr = new String[size];
61+
this.top = -1;
62+
}
63+
private static void validate1(String pushElement) throws NotValidException {
64+
String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\."+
65+
"[a-zA-Z0-9_+&*-]+)*@" +
66+
"(?:[a-zA-Z0-9-]+\\.)+[a-z" +
67+
"A-Z]{2,7}$";
68+
if(pushElement.length()==0)
69+
throw new NotValidException("It is not valid!!!");
70+
}
71+
public void push(String pushedElement)throws NotValidException {
72+
try{
73+
validate1(pushedElement);
74+
}
75+
catch(NotValidException e){
76+
e.printStackTrace();
77+
}
78+
if (top != size-1) {
79+
top++;
80+
arr[top] = pushedElement;
81+
System.out.println("Pushed element:" + pushedElement);
82+
} else {
83+
try{
84+
throw new overflowException("overflow occurs!!!");
85+
}
86+
catch(overflowException e){
87+
e.printStackTrace();
88+
}
89+
}
90+
}
91+
92+
public void pop() {
93+
if (top >= 0) {
94+
int returnedTop = top;
95+
top--;
96+
System.out.println("Popped element :" + arr[returnedTop]);
97+
//return arr[returnedTop];
98+
99+
} else {
100+
try{
101+
throw new underflowException("underflow occurs!!!");
102+
}
103+
catch(underflowException e){
104+
e.printStackTrace();
105+
}
106+
//return -1;
107+
}
108+
}
109+
110+
public static void main(String[] args) throws NotValidException {
111+
Stack s = new Stack("amit","mitra","12-12-1998","[email protected]","03332561254","9854123674",10);
112+
Contact c=new Contact("amit","mitra","12-12-1998","[email protected]","03332561254","9857463214");
113+
//StackCustom.pop();
114+
System.out.println("=================");
115+
s.push(c.fname);s.push(c.lname);s.push(c.dob);s.push(c.email);s.push(c.tel);s.push(c.mob);
116+
System.out.println("=================");
117+
s.pop();s.pop();s.pop();s.pop();s.pop();s.pop();
118+
//System.out.println(arr[top]);
119+
}
120+
}
121+
122+
// All exceptions underflow/overflow/notvalid exception
123+
124+
public class overflowException extends Exception {
125+
public overflowException(String s){
126+
super(s);
127+
}
128+
}
129+
130+
public class NotValidException extends Exception {
131+
132+
public NotValidException(String s) {
133+
super(s);
134+
}
135+
}
136+
137+
public class underflowException extends Exception {
138+
public underflowException(String s){
139+
super(s);
140+
}
141+
}

0 commit comments

Comments
 (0)