-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPerson.java
More file actions
52 lines (44 loc) · 1.2 KB
/
Person.java
File metadata and controls
52 lines (44 loc) · 1.2 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
package inheritanceTutorial;
import java.util.regex.Pattern;
public class Person {
private String name;
private int age;
private String email;
private final String emailRegex = "^(.+)@(.+).com$";
private final Pattern myPattern = Pattern.compile(emailRegex);
public Person (String name, int age, String email) {
if(!myPattern.matcher(email).matches()) {
throw new IllegalArgumentException("INVALID EMAIL!");
}
this.name = name;
this.age = age;
this.email = email;
}
public String getName() {
if(name.length() > 20)
return "Name is too long";
else
return name;
}
public void setName(String name) {
if (name != null && name.length() > 2)
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
System.out.println(email);
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString(){
return "Name: " + name + " " + "Age: " + age + "Email: " + email;
}
}