|
| 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 | +} |
0 commit comments