Skip to content

Commit b729dd4

Browse files
authored
Merge pull request #19 from CSC207-2022F-UofT/4-feature-6-user-attribute-modification
User attribute modificatin pull request
2 parents 1c4d92d + 0dd15ec commit b729dd4

File tree

6 files changed

+121
-59
lines changed

6 files changed

+121
-59
lines changed
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: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package screens.profile_update_screen; /**
2+
* Provides the UI elements
3+
*/
4+
import interface_adapters.profile_modification_IA.ChangeController;
5+
6+
import javax.swing.*;
7+
import java.awt.*;
8+
import java.awt.event.ActionEvent;
9+
import java.awt.event.ActionListener;
10+
11+
public class UserModificationUI implements ChangeController {
12+
private JLabel label;
13+
public UserModificationUI() {
14+
final JFrame frame = new JFrame();
15+
frame.setSize(500, 300);
16+
17+
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
18+
frame.setLayout(new FlowLayout());
19+
20+
// Field for username
21+
final JTextField usernameField = new JTextField("Enter your current username");
22+
usernameField.setBounds(10, 25, 100, 25);
23+
24+
// Field for password
25+
final JTextField passwordField = new JTextField("Enter your current password");
26+
passwordField.setBounds(10, 25, 100, 25);
27+
28+
// to select feature that user wants to change
29+
JLabel lbl = new JLabel("<html>Select what feature<br>you wish to change</html>");
30+
lbl.setVisible(true);
31+
lbl.setBounds(10, 25, 100, 25);
32+
String[] choices = { "Username","Password", "Email"};
33+
final JComboBox<String> cb = new JComboBox<>(choices);
34+
35+
// new value of the feature
36+
final JTextField newFeatureField = new JTextField("Enter the new value for this feature and click OK");
37+
newFeatureField.setBounds(10, 25, 100, 25);
38+
39+
40+
// OK button to process
41+
JButton btn = new JButton("OK");
42+
43+
btn.addActionListener(new ActionListener() {
44+
@Override
45+
public void actionPerformed(ActionEvent e) {
46+
String username = (usernameField.getText());
47+
String password = (passwordField.getText());
48+
String item = cb.getSelectedItem().toString();
49+
String newFeature = (newFeatureField.getText());
50+
boolean success = reportChange(username, password, item, newFeature);
51+
if (success){
52+
label.setText("Your " + item + " was successfully changed.");
53+
}
54+
else{
55+
label.setText("<html>Change was not successful.<br>Please make sure your username and " +
56+
"<br>password match an existing account.</html>");
57+
}
58+
}
59+
});
60+
61+
label = new JLabel();
62+
63+
frame.add(usernameField);
64+
frame.add(passwordField);
65+
frame.add(cb);
66+
frame.add(lbl);
67+
frame.add(newFeatureField);
68+
frame.add(btn);
69+
frame.add(label);
70+
71+
72+
frame.setVisible(true);
73+
}
74+
75+
// profile_modification_IA.ChangeController makes UI implement reportChange to invert the use-case --> UI dependency
76+
@Override
77+
public boolean reportChange(String username, String password, String feature, String newFeature) {
78+
UserDatabase db = new UserDatabase();
79+
if (db.UserExists(username)){
80+
User user = db.getUser(username);
81+
if (user.PasswordMatch(password) & user.getUsername().equals(username)){
82+
user.changeFeature(feature, newFeature);
83+
// this serializes the change
84+
db.modifyUser(username, user);
85+
return true;
86+
}
87+
}
88+
return false;
89+
}
90+
91+
// for trying out the code:
92+
// public static void main(String[] args) {
93+
// new profile_update_screen.UserModificationUI();
94+
//
95+
// }
96+
97+
}

src/main/java/tutorial/HelloWorld.java

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package use_cases.user_attribute_modification_use_case;
2+
3+
public interface Changeable {
4+
public void changeFeature(String feature, String newFeature);
5+
}

src/test/java/TestUserChange.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import screens.profile_update_screen.UserModificationUI;
3+
4+
public class TestUserChange {
5+
public void correctChange() {
6+
UserDatabase db = new UserDatabase();
7+
db.createUser("parmism", "123", "[email protected]", "Basic");
8+
UserModificationUI ui = new UserModificationUI();
9+
ui.reportChange("parmism", "123", "Email", "[email protected]");
10+
UserDatabase db2 = new UserDatabase();
11+
String actual = db2.getUser("parmism").getEmail();
12+
Assertions.assertEquals("[email protected]", actual);
13+
}
14+
}

src/test/java/tutorial/HelloWorldTest.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)