Skip to content

Commit f24cc10

Browse files
committed
Implemented UserModificationGateway for serializing changes to User attributes.
1 parent 9d92543 commit f24cc10

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/main/java/UserDatabase.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import java.io.*;
22
import java.util.ArrayList;
33
import java.util.List;
4-
public class UserDatabase implements UserExists, UserRetriever, UserCreator, IRetrieveList{
4+
public class UserDatabase implements UserExists, UserRetriever, UserCreator, IRetrieveList, UserModificationGateway{
55
File accounts;
66
List<User> accountList;
77
public UserDatabase(){
@@ -94,4 +94,24 @@ public List<User> getList() {
9494
throw new RuntimeException(ex);
9595
}
9696
}
97+
98+
@Override
99+
public void modifyUser(String oldUsername, User modified){
100+
// swap in modified user to accountList
101+
this.accountList.remove(this.getUser(oldUsername));
102+
this.accountList.add(modified);
103+
104+
// overwrite the serialized file
105+
try(FileOutputStream fileOut = new FileOutputStream(accounts)){
106+
try(ObjectOutputStream out = new ObjectOutputStream(fileOut)){
107+
out.writeObject(this.accountList);
108+
out.close();
109+
fileOut.close();
110+
}catch(Exception e){
111+
System.out.println("Error");
112+
}
113+
}catch(Exception e){
114+
System.out.println("Error");
115+
}
116+
}
97117
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// allows for the serialized UserDatabase to update when changes occur to user attributes.
2+
public interface UserModificationGateway {
3+
public void modifyUser(String oldUsername, User modified);
4+
}
5+

0 commit comments

Comments
 (0)