Skip to content

Commit

Permalink
update[Customer]: update customer class to better suit the application
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredrik Marjoni committed Mar 3, 2026
1 parent c4b2eac commit 6b313cc
Showing 1 changed file with 49 additions and 42 deletions.
91 changes: 49 additions & 42 deletions src/main/java/edu/group5/app/model/user/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,64 @@
public class Customer extends User {
private List<Integer> 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<Integer> 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));
}
}
}

0 comments on commit 6b313cc

Please sign in to comment.