Skip to content
This repository was archived by the owner on Jun 17, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 73 additions & 42 deletions src/main/java/MakeItFit/users/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* @version (11052024)
*/
public abstract class User implements UserInterface, Serializable, Comparable<User> {
// CHANGED: removed index and setIndex: now calculated dynamically

private final UUID code;
private String name;
Expand All @@ -26,7 +27,6 @@ public abstract class User implements UserInterface, Serializable, Comparable<Us
private String address;
private String phone;
private String email;
private float index;
public List<Activity> activities;

/**
Expand All @@ -53,37 +53,40 @@ public User(String name,
String address,
String phone,
String email) {
this.code = UUID.randomUUID();
this.name = name;
this.age = age;
this.gender = gender;
this.weight = weight;
this.height = height;
this.bpm = bpm;
this.level = level;

// CHANGED: Added argument validation
this.code = UUID.randomUUID();
this.name = name;
this.setAge(age);
this.gender = gender;
this.setWeight(weight);
this.setHeight(height);
this.setBpm(bpm);
this.setLevel(level);
this.address = address;
this.phone = phone;
this.email = email;
this.index = calculateIndex(weight, height, bpm);
this.activities = new ArrayList<>();
}

/**
* Constructs a new user with the default details.
*/
public User() {
// NOTE: unreachable code
// CHANGED: made values for weight, height, and bpm valid

this.code = UUID.randomUUID();
this.name = "";
this.age = 0;
this.gender = Gender.Other;
this.weight = 0;
this.height = 0;
this.bpm = 0;
this.weight = 1.0f;
this.height = 1;
this.bpm = 1;
this.level = 0;
this.address = "";
this.phone = "";
this.email = "";
this.index = 0;
this.activities = new ArrayList<>();
}

Expand All @@ -104,7 +107,6 @@ public User(User u) {
this.address = u.getAddress();
this.phone = u.getPhone();
this.email = u.getEmail();
this.index = u.getIndex();
this.activities = u.getListActivities();
}

Expand Down Expand Up @@ -213,7 +215,7 @@ public String getEmail() {
* @return The index of the user.
*/
public float getIndex() {
return this.index;
return this.calculateIndex(this.weight, this.height, this.bpm);
}

/**
Expand All @@ -240,6 +242,11 @@ public void setName(String name) {
* @param age The new age of the user.
*/
public void setAge(int age) {
// CHANGED: Added argument validation
if (age < 0) {
throw new IllegalArgumentException("Invalid user age");
}

this.age = age;
}

Expand All @@ -258,6 +265,11 @@ public void setGender(Gender gender) {
* @param weight The new weight of the user.
*/
public void setWeight(float weight) {
// CHANGED: Added argument validation
if (weight < 0.0f) {
throw new IllegalArgumentException("Invalid user weight");
}

this.weight = weight;
}

Expand All @@ -267,6 +279,11 @@ public void setWeight(float weight) {
* @param height The new height of the user.
*/
public void setHeight(int height) {
// CHANGED: Added argument validation
if (height < 0) {
throw new IllegalArgumentException("Invalid user age");
}

this.height = height;
}

Expand All @@ -276,6 +293,11 @@ public void setHeight(int height) {
* @param bpm The new bpm of the user.
*/
public void setBpm(int bpm) {
// CHANGED: Added argument validation
if (bpm < 0) {
throw new IllegalArgumentException("Invalid user bpm");
}

this.bpm = bpm;
}

Expand All @@ -285,6 +307,11 @@ public void setBpm(int bpm) {
* @param level The new level of the user.
*/
public void setLevel(int level) {
// CHANGED: Added argument validation
if (level < 0) {
throw new IllegalArgumentException("Invalid user level");
}

this.level = level;
}

Expand Down Expand Up @@ -315,15 +342,6 @@ public void setEmail(String email) {
this.email = email;
}

/**
* Sets the index of the user.
*
* @param index The new index of the user.
*/
public void setIndex(float index) {
this.index = index;
}

/**
* Adds an activity to the user's list of activities.
*
Expand All @@ -334,7 +352,10 @@ public void addActivity(Activity activity) {
}

public void addActivities(List<Activity> activities) {
this.activities.addAll(activities);
// CHANGED: fix composition
for (Activity activity : activities) {
this.activities.add(activity.clone());
}
}

/**
Expand All @@ -351,7 +372,7 @@ public void removeActivity(UUID activityCode) {
*/
public void updateActivities() {
for (Activity activity : this.activities) {
activity.updateActivity(this.index);
activity.updateActivity(this.getIndex());
}
}

Expand All @@ -364,6 +385,11 @@ public void updateActivities() {
* @return The index of the user.
*/
public float calculateIndex(float weight, int height, int bpm) {
// CHANGED: added argument validation
if (weight <= 0.0f || height <= 0 || bpm <= 0) {
throw new IllegalArgumentException("Invalid calculateIndex arguments");
}

return (weight / (((float) height / 100) * ((float) height / 100)) + (float) bpm / 40);
}

Expand All @@ -382,19 +408,19 @@ public float calculateIndex(float weight, int height, int bpm) {
@Override
public String toString() {
// clang-format off
return String.format(" == (User details) ==\n" +
" Code: %s\n" +
" Name: %s\n" +
" Age: %d\n" +
" Gender: %s\n" +
" Weight: %.2f kg\n" +
" Height: %d cm\n" +
" Bpm: %d\n" +
" Level: %s\n" +
" Address: %s\n" +
" Phone: %s\n" +
" Email: %s\n" +
" Activities: %s\n",
return String.format("== (User details) ==\n" +
"Code: %s\n" +
"Name: %s\n" +
"Age: %d\n" +
"Gender: %s\n" +
"Weight: %.2f kg\n" +
"Height: %d cm\n" +
"Bpm: %d\n" +
"Level: %s\n" +
"Address: %s\n" +
"Phone: %s\n" +
"Email: %s\n" +
"Activities: %s\n",
this.code,
this.name,
this.age,
Expand All @@ -418,12 +444,17 @@ public String toString() {
*/
@Override
public boolean equals(Object o) {
// CHANGED: check for null, class, and gender

if (o == this)
return true;
if (!(o instanceof User))
if (o == null)
return false;
if (this.getClass() != o.getClass())
return false;

User u = (User) o;
return (this.index == u.index && this.name.equals(u.name) && this.age == u.age &&
return (this.name.equals(u.name) && this.age == u.age && this.gender == u.gender &&
this.height == u.height && this.weight == u.weight && this.bpm == u.bpm &&
this.level == u.level && this.address.equals(u.address) &&
this.phone.equals(u.phone) && this.email.equals(u.email) &&
Expand Down
37 changes: 34 additions & 3 deletions src/main/java/MakeItFit/users/UserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ public void insertUser(User user) throws ExistingEntityConflictException {
" already exists.");
}

// CHANGED: this check was missing
if (this.usersByCode.containsKey(user.getCode())) {
throw new ExistingEntityConflictException("User with code " + user.getCode() +
" already exists.");
}

this.usersByCode.put(user.getCode(), user);
this.usersByEmail.put(email, user);
}
Expand Down Expand Up @@ -147,9 +153,11 @@ public void removeUserByCode(UUID code) throws EntityDoesNotExistException {
* @throws EntityDoesNotExistException if the user does not exist
*/
public void removeUserByEmail(String email) throws EntityDoesNotExistException {
// CHANGED: original authors forgot conversion to lower case before comparison
email = email.toLowerCase();

if (!this.usersByEmail.containsKey(email)) {
throw new EntityDoesNotExistException("User with code " + email + " does not exist.");
throw new EntityDoesNotExistException("User with email " + email + " does not exist.");
}

User user = this.usersByEmail.get(email);
Expand Down Expand Up @@ -241,6 +249,9 @@ public List<User> getAllUsers() {
* @return a list of all activities from the user
*/
public List<Activity> getActivitiesFromUser(String email) throws EntityDoesNotExistException {
// CHANGED: original authors forgot conversion to lower case before comparison
email = email.toLowerCase();

if (!this.usersByEmail.containsKey(email)) {
throw new EntityDoesNotExistException(email);
}
Expand All @@ -253,7 +264,14 @@ public List<Activity> getActivitiesFromUser(String email) throws EntityDoesNotEx
* @param email the user's email
* @param activity the activity to be added
*/
public void addActivityToUser(String email, Activity activity) {
public void addActivityToUser(String email, Activity activity)
throws EntityDoesNotExistException {
// CHANGED: added email conversion to lower case and exception
email = email.toLowerCase();

if (!this.usersByEmail.containsKey(email)) {
throw new EntityDoesNotExistException(email);
}
usersByEmail.get(email).addActivity(activity);
}

Expand All @@ -264,6 +282,13 @@ public void addActivityToUser(String email, Activity activity) {
* @param activityCode the activity code
*/
public void removeActivityFromUser(String email, UUID activityCode) {
// CHANGED: added email conversion to lower case and exception
email = email.toLowerCase();

if (!this.usersByEmail.containsKey(email)) {
throw new EntityDoesNotExistException(email);
}

usersByEmail.get(email).removeActivity(activityCode);
}

Expand All @@ -273,7 +298,13 @@ public void removeActivityFromUser(String email, UUID activityCode) {
* @param userCode the user's code
* @param activities the activities to be added
*/
public void addActivitiesToUser(UUID userCode, List<Activity> activities) {
public void addActivitiesToUser(UUID userCode, List<Activity> activities)
throws EntityDoesNotExistException {
// CHANGED: add exception
if (!this.usersByCode.containsKey(userCode)) {
throw new EntityDoesNotExistException("User with code " + userCode + " does not exist");
}

usersByCode.get(userCode).addActivities(activities);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/MakeItFit/users/types/Amateur.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ public Amateur clone() {
* @return A string representation of this Amateur user.
*/
public String toString() {
return super.toString() + " ====================\n";
return super.toString() + "====================\n";
}
}
24 changes: 22 additions & 2 deletions src/main/java/MakeItFit/users/types/Occasional.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ public void setFrequency(int frequency) {
this.frequency = frequency;
}

// CHANGED: added equals method

/**
* Checks if the user is equal to another object.
*
* @param o The object to compare.
* @return True if the user is equal to the object, false otherwise.
*/
@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (o == null)
return false;
if (this.getClass() != o.getClass())
return false;

Occasional u = (Occasional) o;
return (super.equals(o) && this.frequency == u.frequency);
}

/**
* Creates and returns a copy of this Occasional user.
*
Expand All @@ -91,7 +112,6 @@ public Occasional clone() {
* @return a string containing the details of the Occasional user
*/
public String toString() {
return super.toString() + " Frequency: " + frequency + "\n"
+ " ====================\n";
return super.toString() + "Frequency: " + frequency + "\n====================\n";
}
}
Loading