Skip to content

Commit 67e3d83

Browse files
authored
Merge pull request #20 from CSC207-2022F-UofT/5-feature-5-user-profile-display
User profile display pull request
2 parents 848c3b1 + a14e18d commit 67e3d83

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package 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+

src/main/java/User.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.io.Serializable;
2+
public abstract class User implements Serializable, Changeable{
3+
protected String username;
4+
protected String password;
5+
protected String email;
6+
boolean verified = false;
7+
boolean online = false;
8+
public User(String username, String password, String email){
9+
this.username = username;
10+
this.password = password;
11+
this.email = email;
12+
}
13+
public String getEmail(){
14+
return this.email;
15+
}
16+
public String getUsername(){
17+
return this.username;
18+
}
19+
private String getPassword(){
20+
return this.password;
21+
}
22+
23+
public Boolean PasswordMatch(String attempt){
24+
return (this.getPassword().equals(attempt));
25+
}
26+
27+
@Override
28+
// from Changeable
29+
public void changeFeature(String feature, String newFeature){
30+
if (feature == "Username"){
31+
this.username = newFeature;
32+
} else if (feature == "Password"){
33+
this.password = newFeature;
34+
} else if (feature == "Email"){
35+
this.email = newFeature;
36+
}
37+
}
38+
39+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package 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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package user_profile_display_use_case;
2+
3+
import java.io.File;
4+
5+
public class UserReader {
6+
public String[] UserReader(User user) {
7+
String email = user.getEmail();
8+
String username = user.getUsername();
9+
String[] out = new String[] {username, email};
10+
return out;
11+
}
12+
13+
}

src/test/java/TestUserSearch.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Profile_screen.UserSearchUI;
2+
import org.junit.jupiter.api.Assertions;
3+
import org.junit.jupiter.api.Test;
4+
5+
public class TestUserSearch {
6+
@Test
7+
public void TestShowUser() {
8+
UserDatabase db = new UserDatabase();
9+
UserSearchUI ui = new UserSearchUI();
10+
db.createUser("parmism", "123", "[email protected]", "Basic");
11+
String output = "<html>Username: parmism<br>Email: [email protected]</html>";
12+
Assertions.assertEquals((ui.showProfile("parmism")), (output));
13+
}
14+
}

0 commit comments

Comments
 (0)