public string AddAthlete(string gymName, string athleteType, string athleteName, string motivation, int numberOfMedals) { IAthlete athlete; switch (athleteType) { case nameof(Boxer): athlete = new Boxer(athleteName, motivation, numberOfMedals); break; case nameof(Weightlifter): athlete = new Weightlifter(athleteName, motivation, numberOfMedals); break; default: throw new InvalidOperationException(ExceptionMessages.InvalidAthleteType); } IGym gym = this.gyms.First(g => g.Name == gymName); if (athlete.GetType().Name == nameof(Boxer) && gym.GetType().Name == nameof(BoxingGym)) { gym.AddAthlete(athlete); } else if (athlete.GetType().Name == nameof(Weightlifter) && gym.GetType().Name == nameof(WeightliftingGym)) { gym.AddAthlete(athlete); } else { return(OutputMessages.InappropriateGym); } return(string.Format(OutputMessages.EntityAddedToGym, athleteType, gymName)); }
public string AddAthlete(string gymName, string athleteType, string athleteName, string motivation, int numberOfMedals) { IAthlete athlete; IGym gym = this.gyms.First(x => x.Name == gymName); if (athleteType == nameof(Boxer)) { athlete = new Boxer(athleteName, motivation, numberOfMedals); } else if (athleteType == nameof(Weightlifter)) { athlete = new Weightlifter(athleteName, motivation, numberOfMedals); } else { throw new InvalidOperationException(InvalidAthleteType); } var notAppropriateForBoxer = !(gym.GetType().Name == nameof(BoxingGym) && athleteType == nameof(Boxer)); var notAppropriateForWeightlifter = !(gym.GetType().Name == nameof(WeightliftingGym) && athleteType == nameof(Weightlifter)); if (notAppropriateForBoxer && notAppropriateForWeightlifter) { return(InappropriateGym); } gym.AddAthlete(athlete); return(string.Format(EntityAddedToGym, athleteType, gymName)); }