-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplateMethod.java
142 lines (114 loc) · 3.16 KB
/
templateMethod.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
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
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
interface LoginHandler {
void login(String email, String passwd);
}
class User {
private String email;
private String passwd;
public User(String email, String passwd) {
this.email = email;
this.passwd = passwd;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
}
class Database {
private List<String> authorizedUserDB = new LinkedList<>();
private List<User> userDB = new ArrayList<>();
public void addUser(User user){
userDB.add(user);
}
public void rmUser(User user){
userDB.remove(user);
}
public boolean findUser(User user){
for(User u: userDB){
if(u.getEmail().equals(user.getEmail()))
return true;
}
return false;
}
public void addAuthorizedUser(String email){
authorizedUserDB.add(email);
}
public void rmAuthorizedUser(String email){
authorizedUserDB.remove(email);
}
public boolean findAuthorizedUser(String email){
for(String e: authorizedUserDB){
if(e.equals(email))
return true;
}
return false;
}
}
abstract class AbstractLoginHandler implements LoginHandler {
private Database db;
AbstractLoginHandler(Database db){
this.db = db;
}
abstract boolean validate(String email);
boolean authenticate(String email, String passwd){
return db.findUser(new User(email, passwd));
}
boolean authorizate(String email){
return db.findAuthorizedUser(email);
}
@Override
public void login(String email, String passwd){
if(!validate(email)){
System.out.println("validation failed!");
return;
}
if(!authenticate(email, passwd)){
System.out.println("Email or password wrong! Please try again");
return;
}
if(!authorizate(email)){
System.out.println("You are not authorized");
return;
}
System.out.println("Welcome Back!");
}
}
class EmailLoginHandler extends AbstractLoginHandler {
EmailLoginHandler(Database db) {
super(db);
}
@Override
boolean validate(String email) {
return email.contains("@");
}
}
class FacebookLoginHandler extends AbstractLoginHandler {
FacebookLoginHandler(Database db) {
super(db);
}
@Override
boolean validate(String email) {
return email.contains("fb");
}
}
public class Main {
public static void main(String[] args) {
Database db = new Database();
db.addUser(new User("[email protected]", "123456"));
db.addAuthorizedUser("[email protected]");
db.addUser(new User("[email protected]", "123"));
db.addAuthorizedUser("[email protected]");
LoginHandler loginHandler = new FacebookLoginHandler(db);
loginHandler.login("[email protected]", "123");
}
}