deleriumbg

Untitled

Dec 12th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.95 KB | None | 0 0
  1. namespace FestivalManager.Core.Controllers
  2. {
  3.     using System;
  4.     using System.Globalization;
  5.     using System.Linq;
  6.     using System.Text;
  7.     using Contracts;
  8.     using Entities.Contracts;
  9.  
  10.     public class FestivalController : IFestivalController
  11.     {
  12.         private const string TimeFormat = "mm\\:ss";
  13.         private const string TimeFormatLong = "{0:2D}:{1:2D}";
  14.         private const string TimeFormatThreeDimensional = "{0:3D}:{1:3D}";
  15.  
  16.         private readonly IStage stage;
  17.  
  18.         public FestivalController(IStage stage)
  19.         {
  20.             this.stage = stage;
  21.         }
  22.  
  23.         public string Report()
  24.         {
  25.             var result = string.Empty;
  26.  
  27.             var totalFestivalLength = new TimeSpan(this.stage.Sets.Sum(s => s.ActualDuration.Ticks));
  28.  
  29.             result += ($"Festival length: {FormatTime(totalFestivalLength)}") + "\n";
  30.  
  31.             foreach (var set in this.stage.Sets)
  32.             {
  33.                 result += ($"--{set.Name} ({FormatTime(set.ActualDuration)}):") + "\n";
  34.  
  35.                 var performersOrderedDescendingByAge = set.Performers.OrderByDescending(p => p.Age);
  36.                 foreach (var performer in performersOrderedDescendingByAge)
  37.                 {
  38.                     var instruments = string.Join(", ", performer.Instruments
  39.                         .OrderByDescending(i => i.Wear));
  40.  
  41.                     result += ($"---{performer.Name} ({instruments})") + "\n";
  42.                 }
  43.  
  44.                 if (!set.Songs.Any())
  45.                     result += ("--No songs played") + "\n";
  46.                 else
  47.                 {
  48.                     result += ("--Songs played:") + "\n";
  49.                     foreach (var song in set.Songs)
  50.                     {
  51.                         result += ($"----{song.Name} ({song.Duration.ToString(TimeFormat)})") + "\n";
  52.                     }
  53.                 }
  54.             }
  55.  
  56.             return result.ToString();
  57.         }
  58.  
  59.         public string RegisterSet(string[] args)
  60.         {
  61.             throw new NotImplementedException(); // оф...
  62.         }
  63.  
  64.         public string SignUpPerformer(string[] args)
  65.         {
  66.             var name = args[0];
  67.             var age = int.Prase(args[1]);
  68.  
  69.             var instrumenti = args.Skip(2).ToArray();
  70.  
  71.             var instrumenti2 = instrumenti
  72.                 .Select(i => this.instrumentFactory.CreateInstrument(i))
  73.                 .ToArray();
  74.  
  75.             var performer = this.performerFactory.CreatePerformer(name, age);
  76.  
  77.             foreach (var instrument in instrumenti2)
  78.             {
  79.                 performer.AddInstrument(instrument);
  80.             }
  81.  
  82.             this.stage.AddPerformer(performer);
  83.  
  84.             return $"Registered performer {performer.Name}";
  85.         }
  86.  
  87.         public string RegisterSong(string[] args)
  88.         {
  89.             // TODO: scheduled for next month
  90.  
  91.             return $"Registered song {song}";
  92.         }
  93.  
  94.         public string SongRegistration(string[] args)
  95.         {
  96.             var songName = args[0];
  97.             var setName = args[1];
  98.  
  99.             if (!this.stage.HasSet(setName))
  100.             {
  101.                 throw new InvalidOperationException("Invalid set provided");
  102.             }
  103.  
  104.             if (!this.stage.HasSong(songName))
  105.             {
  106.                 throw new InvalidOperationException("Invalid song provided");
  107.             }
  108.  
  109.             var set = this.stage.GetSet(setName);
  110.             var song = this.stage.GetSong(songName);
  111.  
  112.             set.AddSong(song);
  113.  
  114.             return $"Added {song} to {set.Name}";
  115.         }
  116.  
  117.         // Временно!!! Чтобы работало делаем срез на конец месяца
  118.         public string AddPerformerToSet(string[] args)
  119.         {
  120.             return PerformerRegistration(args);
  121.         }
  122.  
  123.         public string PerformerRegistration(string[] args)
  124.         {
  125.             var performerName = args[0];
  126.             var setName = args[1];
  127.  
  128.             if (!this.stage.HasPerformer(performerName))
  129.             {
  130.                 throw new InvalidOperationException("Invalid performer provided");
  131.             }
  132.  
  133.             if (!this.stage.HasSet(setName))
  134.             {
  135.                 throw new InvalidOperationException("Invalid set provided");
  136.             }
  137.  
  138.             AddPerformerToSet(args);
  139.  
  140.             var performer = this.stage.GetPerformer(performerName);
  141.             var set = this.stage.GetSet(setName);
  142.  
  143.             set.AddPerformer(performer);
  144.  
  145.             return $"Added {performer.Name} to {set.Name}";
  146.         }
  147.  
  148.         public string RepairInstruments(string[] args)
  149.         {
  150.             var instrumentsToRepair = this.stage.Performers
  151.                 .SelectMany(p => p.Instruments)
  152.                 .Where(i => i.Wear <= 100)
  153.                 .ToArray();
  154.  
  155.             foreach (var instrument in instrumentsToRepair)
  156.             {
  157.                 instrument.Repair();
  158.             }
  159.  
  160.             return $"Repaired {instrumentsToRepair.Length} instruments";
  161.         }
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment