Skip to content

Commit b7f24b2

Browse files
committed
Merge branch 'main' into 15-userdatabase-and-user
# Conflicts: # src/main/java/entities/userEntities/BasicUser.java # src/main/java/entities/userEntities/User.java # src/main/java/entities/userEntities/UserFactory.java # src/main/java/interface_adapters/User/Changeable.java # src/main/java/interface_adapters/User/IRetrieveList.java # src/main/java/interface_adapters/User/UserCreator.java # src/main/java/interface_adapters/User/UserExists.java # src/main/java/interface_adapters/User/UserRegPresenter.java # src/main/java/interface_adapters/User/UserRetriever.java # src/test/java/UserDatabaseTest.java
2 parents f1905b3 + 9b239a0 commit b7f24b2

33 files changed

+1168
-59
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package entities.message;
2+
3+
import java.time.LocalDateTime;
4+
5+
/**
6+
* Message (abstract class) containing the message content and associated metadata
7+
* The type of the message content is dependent on which child(ren) of Message the message is
8+
* The metadata is universal for all message types and includes the ID of the sender, timestamp, ID of message
9+
*/
10+
public abstract class Message {
11+
/**
12+
* ID of message sender (UUID)
13+
*/
14+
private final String senderID;
15+
/**
16+
* Time message was sent
17+
*/
18+
private final LocalDateTime timestamp;
19+
/**
20+
* ID of message (UUID)
21+
*/
22+
private final String msgID;
23+
24+
/**
25+
* Construct a new message
26+
* @param senderID ID of sender
27+
* @param timestamp time message was sent
28+
* @param msgID ID of message
29+
*/
30+
public Message(String senderID, LocalDateTime timestamp, String msgID) {
31+
this.senderID = senderID;
32+
this.timestamp = timestamp;
33+
this.msgID = msgID;
34+
}
35+
36+
/**
37+
* Gets ID of sender
38+
* @return ID of sender
39+
*/
40+
public String getSenderID() {
41+
return senderID;
42+
}
43+
44+
/**
45+
* Gets message timestamp
46+
* @return message timestamp
47+
*/
48+
public LocalDateTime getTimestamp() {
49+
return timestamp;
50+
}
51+
52+
/**
53+
* Gets ID of message
54+
* @return ID of message
55+
*/
56+
public String getMsgID() {
57+
return msgID;
58+
}
59+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package entities.message;
2+
3+
import java.time.LocalDateTime;
4+
import java.util.UUID;
5+
6+
public class MsgFactory {
7+
private final String msgType;
8+
9+
public MsgFactory(String msgType) {
10+
this.msgType = msgType;
11+
}
12+
13+
public Message createMsg(String senderID, Object msgContent) {
14+
if (msgType.equalsIgnoreCase("text")) {
15+
return new TextMessage(senderID, (String) msgContent, LocalDateTime.now(), UUID.randomUUID().toString());
16+
// ID of message's sender, message text/content, timestamp of message, ID of message
17+
} else {
18+
System.out.println("A " + msgType.toLowerCase() + " is an undefined message type for this program.");
19+
// TODO: implement exception
20+
return null;
21+
}
22+
}
23+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package entities.message;
2+
3+
import java.time.LocalDateTime;
4+
5+
/**
6+
* A message with text as its content (child of Message)
7+
*/
8+
public class TextMessage extends Message {
9+
/**
10+
* Text content of message
11+
*/
12+
private String msgContent;
13+
14+
/**
15+
* Construct a text message
16+
* @param senderID ID of sender
17+
* @param msgContent text content
18+
* @param timestamp message timestamp
19+
* @param msgID ID of message
20+
*/
21+
public TextMessage(String senderID, String msgContent, LocalDateTime timestamp, String msgID) {
22+
super(senderID, timestamp, msgID);
23+
this.msgContent = msgContent;
24+
}
25+
26+
/**
27+
* Gets text content of a message
28+
* @return message content
29+
*/
30+
public String getMsgContent() {
31+
return msgContent;
32+
}
33+
34+
/**
35+
* Sets text content of a message
36+
* @param msgContent updated message content
37+
*/
38+
public void setMsgContent(String msgContent) {
39+
this.msgContent = msgContent;
40+
}
41+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package interface_adapters.User_search_IA;
2+
3+
// UI implements this interface to invert the dependency of UI on the inner layers
4+
public interface UserPresenter {
5+
String showProfile(String username);
6+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package interface_adapters.app_screen_interface_adapters;
2+
3+
import use_cases.app_screen_use_case.AppScreenPresenter;
4+
import entities.*;
5+
import screens.app_screen.AppScreen;
6+
7+
import java.util.ArrayList;
8+
9+
public class AppScreenLoader implements AppScreenPresenter {
10+
11+
private final String username;
12+
private final ArrayList<Chat> chats;
13+
public AppScreen appScreen;
14+
15+
/**
16+
* Create the app screen loader (and store its user and chat information)
17+
* @param username The username of the current user
18+
*/
19+
public AppScreenLoader(String username, ArrayList<Chat> chats){
20+
this.username = username;
21+
this.chats = chats;
22+
try {
23+
openScreen();
24+
} catch (Exception e) {
25+
throw new RuntimeException("Unexpected Interruption: cannot load screen");
26+
}
27+
28+
}
29+
30+
/**
31+
* Create the screen to show to the user
32+
*/
33+
@Override
34+
public void openScreen() {
35+
this.appScreen = new AppScreen(this.username, this.chats);
36+
}
37+
38+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package interface_adapters.app_screen_interface_adapters;
2+
3+
import java.util.ArrayList;
4+
import interface_adapters.*;
5+
import entities.*;
6+
import data_access.UserDatabase;
7+
import java.util.ArrayList;
8+
9+
public class UserAppScreenGateway implements Login {
10+
11+
private final UserDatabase userDatabase;
12+
private final String username;
13+
private ArrayList<Chat> userChats;
14+
15+
/**
16+
* Create gateway between user and appscreen
17+
*/
18+
public UserAppScreenGateway(String username){
19+
this.username = username;
20+
this.userDatabase = new UserDatabase();
21+
login();
22+
}
23+
24+
/**
25+
* Log the user into the system
26+
*/
27+
public void login(){;
28+
this.userChats = this.userDatabase.getUserChats(this.username);
29+
AppScreenLoader appScreenLoader = new AppScreenLoader(this.username, this.userChats);
30+
}
31+
32+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package interface_adapters.profile_modification_IA;
2+
3+
public interface ChangeController {
4+
public boolean reportChange(String username, String password, String feature, String newFeature);
5+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package screens.Profile_screen; /**
2+
* Provides the UI elements
3+
*/
4+
import javax.swing.*;
5+
import java.awt.*;
6+
import java.awt.event.ActionEvent;
7+
import java.awt.event.ActionListener;
8+
import java.io.File;
9+
10+
11+
public class UserSearchUI implements UserPresenter {
12+
private JLabel label;
13+
14+
public UserSearchUI() {
15+
final JFrame frame = new JFrame();
16+
frame.setSize(300, 100);
17+
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
18+
frame.setLayout(new FlowLayout());
19+
20+
final JTextField field = new JTextField("Enter a username");
21+
22+
field.addActionListener(new ActionListener() {
23+
@Override
24+
public void actionPerformed(ActionEvent e) {
25+
label.setText(showProfile(field.getText()));
26+
}
27+
28+
});
29+
frame.add(field);
30+
label = new JLabel();
31+
frame.add(label);
32+
frame.setVisible(true);
33+
}
34+
35+
// User_search_IA.UserPresenter makes UI implement showProfile to invert the use-case --> UI dependency
36+
@Override
37+
public String showProfile(String username) {
38+
// setting up access to the database of users:
39+
UserDatabase db = new UserDatabase();
40+
if (db.UserExists(username)){
41+
User user = db.getUser(username);
42+
UserReader reader = new UserReader();
43+
String[] features = reader.UserReader(user);
44+
String email = features[1];
45+
return("<html>Username: " + username + "<br>Email: " + email + "</html>");
46+
}
47+
else{
48+
return("User with given username does not exist.");
49+
}
50+
}
51+
52+
// for trying out the code:
53+
// public static void main(String[] args) {
54+
// new Profile_screen.UserSearchUI();
55+
//
56+
// }
57+
58+
}
59+
60+
61+
62+
63+
64+

0 commit comments

Comments
 (0)