Advertisement
PetarNeshkov5360

Stage class

Mar 10th, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. namespace FestivalManager.Entities
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6.  
  7. public class Stage
  8. {
  9. private const string CanNotBeNullMessage = "Can not be null!";
  10.  
  11. private readonly List<Song> Songs;
  12. private readonly List<Performer> performers;
  13.  
  14. public Stage()
  15. {
  16. this.Songs = new List<Song>();
  17. this.performers = new List<Performer>();
  18. }
  19.  
  20. public IReadOnlyCollection<Performer> Performers => this.performers.AsReadOnly();
  21.  
  22. public void AddPerformer(Performer performer)
  23. {
  24. this.ValidateNullValue(performer, nameof(performer), CanNotBeNullMessage);
  25.  
  26. if (performer.Age < 18)
  27. {
  28. throw new ArgumentException("You can only add performers that are at least 18.");
  29. }
  30.  
  31. this.performers.Add(performer);
  32. }
  33.  
  34. public void AddSong(Song song)
  35. {
  36. this.ValidateNullValue(song, nameof(song), CanNotBeNullMessage);
  37.  
  38. if (song.Duration.TotalMinutes < 1)
  39. {
  40. throw new ArgumentException("You can only add songs that are longer than 1 minute.");
  41. }
  42.  
  43. this.Songs.Add(song);
  44. }
  45.  
  46. public string AddSongToPerformer(string songName, string performerName)
  47. {
  48. this.ValidateNullValue(songName, nameof(songName), CanNotBeNullMessage);
  49. this.ValidateNullValue(performerName, nameof(performerName), CanNotBeNullMessage);
  50.  
  51. var perfomer = this.GetPerformer(performerName);
  52. var song = this.GetSong(songName);
  53.  
  54. perfomer.SongList.Add(song);
  55.  
  56. return $"{song} will be performed by {perfomer}";
  57. }
  58.  
  59. public string Play()
  60. {
  61. var songsCount = this.performers.Sum(p => p.SongList.Count());
  62.  
  63. return $"{this.performers.Count} performers played {songsCount} songs";
  64. }
  65.  
  66. private Performer GetPerformer(string performerName)
  67. {
  68. var performer = this.Performers.FirstOrDefault(p => p.FullName == performerName);
  69.  
  70. if (performer == null)
  71. {
  72. throw new ArgumentException("There is no performer with this name.");
  73. }
  74.  
  75. return performer;
  76. }
  77.  
  78. private Song GetSong(string songName)
  79. {
  80. var song = this.Songs.FirstOrDefault(p => p.Name == songName);
  81.  
  82. if (song==null)
  83. {
  84. throw new ArgumentException("There is no song with this name.");
  85. }
  86.  
  87. return song;
  88. }
  89.  
  90. private void ValidateNullValue(object variable, string variableName, string exceptionMessage)
  91. {
  92. if (variable == null)
  93. {
  94. throw new ArgumentNullException(variableName, exceptionMessage);
  95. }
  96. }
  97. }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement