5
5
6
6
import java .io .*;
7
7
import java .util .*;
8
+ /**
9
+ * Common User Request Model.
10
+ * Application Business Rules
11
+ * @author Justin Li
12
+ */
8
13
9
14
public class CommonUserDataAccess implements IUserDataAccess {
10
15
private final File userCsvFile ;
@@ -13,6 +18,10 @@ public class CommonUserDataAccess implements IUserDataAccess {
13
18
14
19
private final Map <String , CommonUserDsRequestModel > accounts = new HashMap <>();
15
20
21
+ /**
22
+ * Creates a common user data access based on the following parameters.
23
+ * @param csvPath the csv file pathway to the database.
24
+ */
16
25
public CommonUserDataAccess (String csvPath ) throws IOException {
17
26
userCsvFile = new File (csvPath );
18
27
@@ -48,6 +57,9 @@ public CommonUserDataAccess(String csvPath) throws IOException {
48
57
reader .close ();
49
58
}
50
59
}
60
+ /**
61
+ * A private function that is called in the methods below which saves any changes made to a common user.
62
+ */
51
63
private void save () {
52
64
BufferedWriter writer ;
53
65
try {
@@ -73,44 +85,67 @@ private void save() {
73
85
}
74
86
75
87
}
88
+ /**
89
+ * Gets the user object with a given username.
90
+ * @param username the user's username.
91
+ * @return the user object with the given username.
92
+ */
76
93
@ Override
77
94
public CommonUserDsRequestModel getUser (String username ) {
78
95
return accounts .get (username );
79
-
80
96
}
81
-
97
+ /**
98
+ * Gets all the user objects in the database.
99
+ * @return all the user objects in the database.
100
+ */
82
101
@ Override
83
102
public Collection <CommonUserDsRequestModel > getAllUsers (){
84
103
return accounts .values ();
85
104
}
86
-
105
+ /**
106
+ * Returns true if a user object containing given username exists.
107
+ * @param username the user's username
108
+ * @return true if a user object containing given username exists.
109
+ */
87
110
@ Override
88
111
public boolean existsByName (String username ) {
89
112
return accounts .containsKey (username );
90
113
91
114
}
115
+ /**
116
+ * Saves a new flashcard set id into the list of current flashcard set ids that the user has created.
117
+ * @param username the user's username
118
+ * @param flashcardSetId the id of the flashcard set that the user created.
119
+ */
92
120
@ Override
93
- public void saveFlashcardSetID (String username , int FlashcardSetID ) {
121
+ public void saveFlashcardSetID (String username , int flashcardSetId ) {
94
122
CommonUserDsRequestModel oldUser = accounts .get (username );
95
123
List <Integer > newFlashcardSet = new ArrayList <>(oldUser .getFlashcardSetIds ());
96
- newFlashcardSet .add (FlashcardSetID );
124
+ newFlashcardSet .add (flashcardSetId );
97
125
CommonUserDsRequestModel newUser = new CommonUserDsRequestModel (oldUser .getUsername (), oldUser .getPassword (), oldUser .getIsAdmin (), newFlashcardSet );
98
126
99
127
accounts .put (username , newUser );
100
128
save ();
101
129
}
102
-
130
+ /**
131
+ * Deletes a flashcard set created by a user.
132
+ * @param username the user's username
133
+ * @param flashcardSetId the id of the flashcard set that will be deleted
134
+ */
103
135
@ Override
104
- public void deleteFlashcardSetID (String username , int FlashcardSetID ) {
136
+ public void deleteFlashcardSetID (String username , int flashcardSetId ) {
105
137
CommonUserDsRequestModel oldUser = accounts .get (username );
106
138
List <Integer > newFlashcardSet = new ArrayList <>(oldUser .getFlashcardSetIds ());
107
- newFlashcardSet .remove ((Object ) FlashcardSetID );
139
+ newFlashcardSet .remove ((Object ) flashcardSetId );
108
140
CommonUserDsRequestModel newUser = new CommonUserDsRequestModel (oldUser .getUsername (), oldUser .getPassword (), oldUser .getIsAdmin (), newFlashcardSet );
109
141
110
142
accounts .put (username , newUser );
111
143
save ();
112
144
}
113
-
145
+ /**
146
+ * Saves a newly created user to the database
147
+ * @param user the user's username
148
+ */
114
149
@ Override
115
150
public void saveUser (CommonUserDsRequestModel user ) {
116
151
accounts .put (user .getUsername (), user );
0 commit comments