From 6b313cc4ef9b9a4f7c7a264d6dd5e51b93df0d2f Mon Sep 17 00:00:00 2001 From: Fredrik Marjoni Date: Tue, 3 Mar 2026 13:44:02 +0100 Subject: [PATCH] update[Customer]: update customer class to better suit the application --- .../edu/group5/app/model/user/Customer.java | 91 ++++++++++--------- 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/src/main/java/edu/group5/app/model/user/Customer.java b/src/main/java/edu/group5/app/model/user/Customer.java index 6ae6109..2c221b0 100644 --- a/src/main/java/edu/group5/app/model/user/Customer.java +++ b/src/main/java/edu/group5/app/model/user/Customer.java @@ -14,57 +14,64 @@ public class Customer extends User { private List preferences; - /** - * Constructs a Customer object. - * Calls the {@link User} superclass constructor to initialize common user fields - * and initializes the preferences list. - * @param userId the unique identifier for the user, must be a positive integer - * @param firstName the first name of the customer - * @param lastName the last name of the customer - * @param email the email address of the customer - * @param passwordHash the hashed password of the customer - * @throws IllegalArgumentException if any required field is invalid - */ - public Customer(int userId, String firstName, String lastName, - String email, String passwordHash) { - +/** + * Constructs a new Customer object for new registrations. + * The role is automatically set to "Customer" and the preferences list is initialized empty. + * + * @param userId the unique identifier for the user, must be positive + * @param firstName the first name of the customer + * @param lastName the last name of the customer + * @param email the email address of the customer + * @param passwordHash the hashed password of the customer + * @throws IllegalArgumentException if any parameter is invalid (null, empty, or userId ≤ 0) + */ +public Customer(int userId, String firstName, String lastName, + String email, String passwordHash) { super(userId, "Customer", firstName, lastName, email, passwordHash); this.preferences = new ArrayList<>(); - } +} + +/** + * Constructs a Customer object for existing users loaded from the database + * or for cases where the role is already defined. + * The role should normally be "Customer", but this constructor allows flexibility + * (e.g., for testing or future user types). Preferences list is initialized empty. + * + * @param userId the unique identifier for the user, must be positive + * @param role the role of the user (should be "Customer" for customer instances) + * @param firstName the first name of the customer + * @param lastName the last name of the customer + * @param email the email address of the customer + * @param passwordHash the hashed password of the customer + * @throws IllegalArgumentException if any parameter is invalid (null, empty, or userId ≤ 0) + */ +public Customer(int userId, String role, String firstName, String lastName, + String email, String passwordHash) { + super(userId, role, firstName, lastName, email, passwordHash); + this.preferences = new ArrayList<>(); +} public List getPreferences() { return preferences; } - public boolean addPreference(int orgNumber) { - try { - if (orgNumber <= 0) { - throw new IllegalArgumentException("Organization number must be a positive integer"); - } - if (preferences.contains(orgNumber)) { - throw new IllegalStateException("Organization number already in preferences"); - } - preferences.add(orgNumber); - return true; - } catch (Exception e) { - System.out.println("Add preference failed: " + e.getMessage()); - return false; + public void addPreference(int orgNumber) { + if (orgNumber <= 0) { + throw new IllegalArgumentException("Organization number must be a positive integer"); } + if (preferences.contains(orgNumber)) { + throw new IllegalArgumentException("Organization number already in preferences"); + } + preferences.add(orgNumber); } - public boolean removePreference(int orgNumber) { - try { - if (orgNumber <= 0) { - throw new IllegalArgumentException("Organization number must be a positive integer"); - } - if (!preferences.contains(orgNumber)) { - throw new IllegalStateException("Organization number not found in preferences"); - } - preferences.remove(Integer.valueOf(orgNumber)); - return true; - } catch (Exception e) { - System.out.println("Remove preference failed: " + e.getMessage()); - return false; + public void removePreference(int orgNumber) { + if (orgNumber <= 0) { + throw new IllegalArgumentException("Organization number must be a positive integer"); + } + if (!preferences.contains(orgNumber)) { + throw new IllegalArgumentException("Organization number not found in preferences"); } + preferences.remove(Integer.valueOf(orgNumber)); } -} +} \ No newline at end of file