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