-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
526 additions
and
0 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
04-collections-clean-code/lab/solution/src/bg/sofia/uni/fmi/mjt/gym/Gym.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package bg.sofia.uni.fmi.mjt.gym; | ||
|
||
import bg.sofia.uni.fmi.mjt.gym.member.Address; | ||
import bg.sofia.uni.fmi.mjt.gym.member.GymMember; | ||
import bg.sofia.uni.fmi.mjt.gym.member.MembersByNameComparator; | ||
import bg.sofia.uni.fmi.mjt.gym.member.MembersByPersonalIdComparator; | ||
import bg.sofia.uni.fmi.mjt.gym.member.MembersByProximityToGymComparator; | ||
import bg.sofia.uni.fmi.mjt.gym.workout.Workout; | ||
|
||
import java.time.DayOfWeek; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.SortedSet; | ||
import java.util.TreeSet; | ||
|
||
public class Gym implements GymAPI { | ||
private final int capacity; | ||
private SortedSet<GymMember> members; | ||
private final Address address; | ||
|
||
public Gym(int capacity, Address address) { | ||
this.address = address; | ||
this.capacity = capacity; | ||
this.members = new TreeSet<>(new MembersByPersonalIdComparator()); | ||
} | ||
|
||
@Override | ||
public SortedSet<GymMember> getMembers() { | ||
return Collections.unmodifiableSortedSet(members); | ||
} | ||
|
||
@Override | ||
public SortedSet<GymMember> getMembersSortedByName() { | ||
return getUnmodifiableSortedMembers(new MembersByNameComparator()); | ||
} | ||
|
||
@Override | ||
public SortedSet<GymMember> getMembersSortedByProximityToGym() { | ||
return getUnmodifiableSortedMembers(new MembersByProximityToGymComparator(this.address)); | ||
} | ||
|
||
@Override | ||
public void addMember(GymMember member) throws GymCapacityExceededException { | ||
if (member == null) { | ||
throw new IllegalArgumentException("Member should not be null"); | ||
} | ||
if (members.size() == capacity) { | ||
throw new GymCapacityExceededException("Cannot add member to the gym. Gym is full."); | ||
} | ||
|
||
members.add(member); | ||
} | ||
|
||
@Override | ||
public void addMembers(Collection<GymMember> members) throws GymCapacityExceededException { | ||
if (members == null || members.isEmpty()) { | ||
throw new IllegalArgumentException("Members should not be null or empty"); | ||
} | ||
if (this.members.size() + members.size() > capacity) { | ||
throw new GymCapacityExceededException("Cannot add all of the provided members. Gym is full."); | ||
} | ||
|
||
this.members.addAll(members); | ||
} | ||
|
||
@Override | ||
public boolean isMember(GymMember member) { | ||
if (member == null) { | ||
throw new IllegalArgumentException("Member should not be null"); | ||
} | ||
|
||
return this.members.contains(member); | ||
} | ||
|
||
@Override | ||
public boolean isExerciseTrainedOnDay(String exerciseName, DayOfWeek day) { | ||
if (exerciseName == null || exerciseName.isEmpty()) { | ||
throw new IllegalArgumentException("ExerciseName should not be null or empty"); | ||
} | ||
if (day == null) { | ||
throw new IllegalArgumentException("Day should not be null"); | ||
} | ||
|
||
for (GymMember member : this.members) { | ||
Map<DayOfWeek, Workout> memberProgram = member.getTrainingProgram(); | ||
Workout memberWorkout = memberProgram.get(day); | ||
if (memberWorkout == null) { | ||
continue; | ||
} | ||
if (memberWorkout.containsExercise(exerciseName)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public Map<DayOfWeek, List<String>> getDailyListOfMembersForExercise(String exerciseName) { | ||
if (exerciseName == null || exerciseName.isEmpty()) { | ||
throw new IllegalArgumentException("ExerciseName should not be null or empty"); | ||
} | ||
|
||
Map<DayOfWeek, List<String>> result = new HashMap<>(); | ||
for (GymMember member : this.members) { | ||
Map<DayOfWeek, Workout> memberProgram = member.getTrainingProgram(); | ||
for (Map.Entry<DayOfWeek, Workout> entry : memberProgram.entrySet()) { | ||
if (entry.getValue().containsExercise(exerciseName)) { | ||
List<String> membersForExercise = result.get(entry.getKey()); | ||
if (membersForExercise == null) { | ||
membersForExercise = new ArrayList<>(); | ||
result.put(entry.getKey(), membersForExercise); | ||
} | ||
|
||
membersForExercise.add(member.getName()); | ||
} | ||
} | ||
} | ||
|
||
return Collections.unmodifiableMap(result); | ||
} | ||
|
||
private SortedSet<GymMember> getUnmodifiableSortedMembers(Comparator<GymMember> comparator) { | ||
SortedSet<GymMember> sortedMembers = new TreeSet<>(comparator); | ||
sortedMembers.addAll(this.members); | ||
return Collections.unmodifiableSortedSet(sortedMembers); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
04-collections-clean-code/lab/solution/src/bg/sofia/uni/fmi/mjt/gym/GymAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package bg.sofia.uni.fmi.mjt.gym; | ||
|
||
import bg.sofia.uni.fmi.mjt.gym.member.GymMember; | ||
|
||
import java.util.Collection; | ||
import java.time.DayOfWeek; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.SortedSet; | ||
|
||
public interface GymAPI { | ||
|
||
/** | ||
* Returns an unmodifiable copy of all members of the gym. | ||
* If there are no members, return an empty collection. | ||
*/ | ||
SortedSet<GymMember> getMembers(); | ||
|
||
/** | ||
* Returns an unmodifiable copy of all members of the gym sorted by their name in lexicographic order. | ||
* If there are no members, return an empty collection. | ||
*/ | ||
SortedSet<GymMember> getMembersSortedByName(); | ||
|
||
/** | ||
* Returns an unmodifiable copy of all members of the gym sorted by their proximity to the gym in increasing order. | ||
* If there are no members, return an empty collection. | ||
*/ | ||
SortedSet<GymMember> getMembersSortedByProximityToGym(); | ||
|
||
/** | ||
* Adds a single member to the gym. | ||
* | ||
* @param member the member to add | ||
* @throws GymCapacityExceededException - if the gym is full | ||
* @throws IllegalArgumentException if member is null | ||
*/ | ||
void addMember(GymMember member) throws GymCapacityExceededException; | ||
|
||
/** | ||
* Adds a group of members to the gym. If the gym does not have the capacity to accept all the | ||
* new members then no members are added | ||
* | ||
* @param members the members to add | ||
* @throws GymCapacityExceededException if the gym is full | ||
* @throws IllegalArgumentException if members is null or empty | ||
*/ | ||
void addMembers(Collection<GymMember> members) throws GymCapacityExceededException; | ||
|
||
/** | ||
* Checks if a given member is member of the gym. | ||
* | ||
* @param member - the member | ||
* @throws IllegalArgumentException if member is null | ||
*/ | ||
boolean isMember(GymMember member); | ||
|
||
/** | ||
* Checks if an Exercise is trained on a given day. | ||
* | ||
* @param exerciseName - the name of the Exercise | ||
* @param day - the day for which the check is done | ||
* @throws IllegalArgumentException if day is null or if exerciseName is null or empty | ||
*/ | ||
boolean isExerciseTrainedOnDay(String exerciseName, DayOfWeek day); | ||
|
||
/** | ||
* Returns an unmodifiable Map representing each day and the names of the members that do this exercise on it. | ||
* | ||
* @param exerciseName - the name of the exercise being done | ||
* @throws IllegalArgumentException if exerciseName is null or empty | ||
*/ | ||
Map<DayOfWeek, List<String>> getDailyListOfMembersForExercise(String exerciseName); | ||
} |
11 changes: 11 additions & 0 deletions
11
...ns-clean-code/lab/solution/src/bg/sofia/uni/fmi/mjt/gym/GymCapacityExceededException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package bg.sofia.uni.fmi.mjt.gym; | ||
|
||
public class GymCapacityExceededException extends Exception { | ||
public GymCapacityExceededException(String message) { | ||
super(message); | ||
} | ||
|
||
public GymCapacityExceededException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
04-collections-clean-code/lab/solution/src/bg/sofia/uni/fmi/mjt/gym/member/Address.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package bg.sofia.uni.fmi.mjt.gym.member; | ||
|
||
public record Address(double longitude, double latitude) { | ||
public double getDistanceTo(Address other) { | ||
return Math.sqrt( | ||
Math.pow((other.longitude - this.longitude), 2) + Math.pow((other.latitude - this.latitude), 2)); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...lections-clean-code/lab/solution/src/bg/sofia/uni/fmi/mjt/gym/member/DayOffException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package bg.sofia.uni.fmi.mjt.gym.member; | ||
|
||
public class DayOffException extends RuntimeException { | ||
public DayOffException(String message) { | ||
super(message); | ||
} | ||
|
||
public DayOffException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
04-collections-clean-code/lab/solution/src/bg/sofia/uni/fmi/mjt/gym/member/Gender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package bg.sofia.uni.fmi.mjt.gym.member; | ||
|
||
public enum Gender { | ||
MALE, FEMALE, OTHER | ||
} |
82 changes: 82 additions & 0 deletions
82
04-collections-clean-code/lab/solution/src/bg/sofia/uni/fmi/mjt/gym/member/GymMember.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package bg.sofia.uni.fmi.mjt.gym.member; | ||
|
||
import bg.sofia.uni.fmi.mjt.gym.workout.Exercise; | ||
import bg.sofia.uni.fmi.mjt.gym.workout.Workout; | ||
|
||
import java.time.DayOfWeek; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public interface GymMember { | ||
|
||
/** | ||
* Returns the member's name. | ||
*/ | ||
String getName(); | ||
|
||
/** | ||
* Returns the member's age. | ||
*/ | ||
int getAge(); | ||
|
||
/** | ||
* Returns the member's id number. | ||
*/ | ||
String getPersonalIdNumber(); | ||
|
||
/** | ||
* Returns the member's gender. | ||
*/ | ||
Gender getGender(); | ||
|
||
/** | ||
* Returns the member's address. | ||
*/ | ||
Address getAddress(); | ||
|
||
/** | ||
* Returns an immutable Map representing the workout a member does on the DayOfWeek. | ||
*/ | ||
Map<DayOfWeek, Workout> getTrainingProgram(); | ||
|
||
/** | ||
* Sets the workout for a specific day. | ||
* | ||
* @param day - DayOfWeek on which the workout will be trained | ||
* @param workout - the workout to be trained | ||
* @throws IllegalArgumentException if day or workout is null. | ||
*/ | ||
void setWorkout(DayOfWeek day, Workout workout); | ||
|
||
/** | ||
* Returns a collection of days in undefined order on which the workout finishes with a specific exercise. | ||
* | ||
* @param exerciseName - the name of the exercise. | ||
* @throws IllegalArgumentException if exerciseName is null or empty. | ||
*/ | ||
Collection<DayOfWeek> getDaysFinishingWith(String exerciseName); | ||
|
||
/** | ||
* Adds an Exercise to the Workout trained on the given day. If there is no workout set for the day, the day is | ||
* considered a day off and no exercise can be added. | ||
* | ||
* @param day - DayOfWeek to train the exercise. | ||
* @param exercise - the trained Exercise. | ||
* @throws DayOffException if the Workout on this day is null. | ||
* @throws IllegalArgumentException if day or exercise is null | ||
*/ | ||
void addExercise(DayOfWeek day, Exercise exercise); | ||
|
||
/** | ||
* Adds Exercises to the Workout trained on the given day. If there is no workout set for the day, the day is | ||
* considered a day off and no exercise can be added. | ||
* | ||
* @param day - DayOfWeek to train the exercise. | ||
* @param exercises - list of the trained Exercises | ||
* @throws DayOffException if the Workout on this day is null or the exercises list is empty. | ||
* @throws IllegalArgumentException if day is null or exercises is null or empty | ||
*/ | ||
void addExercises(DayOfWeek day, List<Exercise> exercises); | ||
|
||
} |
Oops, something went wrong.