-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.java
More file actions
264 lines (219 loc) · 8.9 KB
/
Database.java
File metadata and controls
264 lines (219 loc) · 8.9 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import java.util.*;
/* Database.java
* a database that contains a list of all the profiles created
*
* @author Alexis Luo
*/
public class Database {
private HashedDictionary<String, Profile> profilesList = new HashedDictionary<>();
Scanner scan = new Scanner(System.in);
public void Database(){
}
public HashedDictionary<String, Profile> getProfilesList(){
return profilesList;
}
//creates a new profile and adds it to the database, profilesList
public void createProfile(){
boolean exist = true;
while (exist){
System.out.println("What would you like your username to be?");
String userName = scan.nextLine();
if (profilesList.contains(userName)){
System.out.println("The username already exists.");
}
else{
Profile newUser = new Profile();
newUser.setName(userName);
System.out.print("Enter your first name: ");
String first = scan.nextLine();
newUser.setFirstName(first);
System.out.print("Enter your last name: ");
String last = scan.nextLine();
newUser.setLastName(last);
profilesList.add(userName, newUser);
System.out.println("Succesfully created a new account!");
System.out.println();
exist = false;
}
}
}
//checks if a user's friends are still in the database
//removes friends if they are no longer in the database
public void checkFriends(Profile user){
for (int i=0; i<user.getFriendList().size(); i++){
Profile friendUser = user.getFriendList().get(i);
if (!profilesList.contains(friendUser.getName())){
user.removeFriend(friendUser);
}
}
}
//provides a user options to change their account username/friends
public void modifyProfile(Profile user){
System.out.println("Please select what you would like to modify:");
System.out.println("(1) Change username");
System.out.println("(2) Update friends");
String modify = scan.nextLine();
System.out.println();
if (modify.equals("1")){ //renaming username
boolean exist = true;
while (exist){
System.out.println("What would you like your new username to be?");
String newName = scan.nextLine();
if (profilesList.contains(newName)){ //username already exists
System.out.println("The username already exists.");
}
else{ //username not already used
//first remove original profile with old name
String oldName = user.getName();
profilesList.remove(oldName);
//now add in the updated user with new name for the key
// it is inconvenient, but not sure how else to make convenient
user.setName(newName);
profilesList.add(newName, user);
System.out.println("Successfully changed username to " + newName);
exist = false;
}
}
}
else if (modify.equals("2")){
update(user);
}
else{
System.out.println("Bad input.");
}
System.out.println();
}
//logins a user and gives them options to access their account
public void loginToProfile(Profile user){
System.out.println(" ------- Welcome, " + user.getFirstName() + "! ------- ");
checkFriends(user);
boolean nextOpt = true;
while (nextOpt){
System.out.println("Select an option:");
System.out.println("(1) View profile ");
System.out.println("(2) Edit profile ");
System.out.println("(3) Search for other profiles");
System.out.println("(4) Delete profile ");
System.out.println("(5) Log out and return to home screen ");
String modOpt = scan.nextLine();
System.out.println();
if (modOpt.equals("1")){
user.displayProfile();
}
else if (modOpt.equals("2")){
modifyProfile(user);
}
else if (modOpt.equals("3")){ //reading profiles
//print out list of profiles
displayProfileList();
System.out.println();
boolean cont = true;
while (cont){
System.out.println();
System.out.println("Which profile would you like to look at?");
String readProfile = scan.nextLine();
Profile friendProfile = read(readProfile);
if (friendProfile != null){
System.out.println();
friendProfile.displayProfile();
System.out.println();
update(user);
}
System.out.println("Would you like to continue looking at profiles? (Y/N)");
String contLook = scan.nextLine().toLowerCase();
if (contLook.equals("n")){
cont = false;
}
}
System.out.println();
}
else if (modOpt.equals("4")){
deleteProfile(user);
nextOpt = false;
}
else if (modOpt.equals("5")){
nextOpt = false;
System.out.println("Logged off!");
}
else{
System.out.println("Invalid option.");
}
}
}
//deletes a profile and removes it from the database
public void deleteProfile(Profile user){
System.out.println("Are you sure you want to delete your profile? (Y/N)");
String deleteProfile = scan.nextLine().toLowerCase();
if (deleteProfile.equals("y")){
profilesList.remove(user.getName());
System.out.println("Successfully deleted profile from database.");
}
else if (deleteProfile.equals("n")){
System.out.println("Profile was not deleted.");
}
else{
System.out.println("Invalid input.");
}
}
//finds and returns the user being searched for, if in the database
public Profile read(String username) {
if (profilesList.contains(username)) {
return profilesList.getValue(username);
} else {
System.out.println("Profile not found");
return null;
}
}
//asks user if they want to add/remove friends
public void update(Profile user) {
if (user == null) {
System.out.println("Profile for " + user.getName() + " not found.");
return;
}
Scanner scanner = new Scanner(System.in);
System.out.println("Would you like to: ");
System.out.println("(1) Add Friend ");
System.out.println("(2) Remove Friend ");
int choice = scanner.nextInt();
scanner.nextLine();
if ((choice == 1) || (choice == 2)){
System.out.print("Enter the friend's username: ");
String friendName = scanner.nextLine();
if (friendName.equals(user.getName())){
System.out.println("You cannot add yourself as a friend.");
return;
}
else if (profilesList.contains(friendName)){
Profile aFriend = profilesList.getValue(friendName);
if (choice == 1){
if (user.addFriend(aFriend))
System.out.println("Friend added successfully.");
else
System.out.println(friendName + " is already a friend.");
}
else if (choice == 2){
if (user.removeFriend(aFriend))
System.out.println("Friend removed successfully.");
else
System.out.println(friendName + " is not on the friend's list");
}
}
else{
System.out.println("The profile does not exist."); }
}
else{
System.out.println("Invalid option. Please choose 1 or 2.");
}
}
//prints out a list of profiles
public void displayProfileList() {
System.out.println("Here are a list of profiles in the database: ");
Iterator<String> keyIterator = profilesList.getKeyIterator();
System.out.println("---");
while (keyIterator.hasNext()) {
String username = keyIterator.next();
System.out.println("| • " + username);
}
System.out.println("-----");
}
}