-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathMySystemController.java
More file actions
122 lines (106 loc) · 2.88 KB
/
MySystemController.java
File metadata and controls
122 lines (106 loc) · 2.88 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package hello;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author roberta
*/
public class MySystemController {
private List<User> userList = new ArrayList<User>();
private static MySystemController system;
public static MySystemController getInstance() {
if (system == null) {
system = new MySystemController();
}
return system;
}
private MySystemController() {
}
/**
* Creates a new user,if the user already exists displays an exception
*
* @param userName
* @param email
* @param password
* @param realName
* @throws UserAlreadyExistsException
*/
public void createUser(String userName, String email, String password, String realName) throws UserAlreadyExistsException {
User user = new User(userName, email, password, realName);
if (!userList.contains(user)) {
userList.add(user);
} else {
throw new UserAlreadyExistsException("User already exists!");
}
}
/**
* Eliminate a user if exists and matches with the email inserted
*
* @param email
*
*/
public void deleteUser(String email) {
Integer indexOf = null;
for (User u : userList) {
if (u.getEmail().equals(email)) {
indexOf = userList.indexOf(u);
break;
}
}
if (indexOf != null) {
userList.remove(userList.get(indexOf));
}
}
/**
* Search the user by userName and return a list with the values.
*
* @param userName
* @return
*/
public List<User> readUser(String userName) {
List<User> resultList = new ArrayList<User>();
for (User u : userList) {
if (u.getUserName().contains(userName)) {
resultList.add(u);
}
}
return resultList;
}
/**
* Find a user by mail
*
* @param mail
* @return
*/
public User retrieveUser(String mail) {
for (User u : userList) {
if (u.getEmail().equals(mail)) {
return u;
}
}
return null;
}
/**
* Search for the email, and update userName | password | realName
*
* @param userName
* @param password
* @param email
* @param realName
*/
public void updateUser(String userName, String password, String email, String realName) {
for (User u : userList) {
if (u.getEmail().equals(email)) {
u.setPassword(password);
u.setRealName(realName);
u.setUserName(userName);
break;
}
}
}
}