Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.11 KB | None | 0 0
  1. namespace SpaceStation.Core
  2. {
  3.     using SpaceStation.Core.Contracts;
  4.     using SpaceStation.Models.Astronauts.Contracts;
  5.     using SpaceStation.Models.Astronauts.Models;
  6.     using SpaceStation.Models.Mission;
  7.     using SpaceStation.Models.Planets;
  8.     using SpaceStation.Repositories.Models;
  9.     using SpaceStation.Utilities.Messages;
  10.     using System;
  11.     using System.Collections.Generic;
  12.     using System.Linq;
  13.     using System.Text;
  14.  
  15.     public class Controller : IController
  16.     {
  17.         private readonly AstronautRepository astronautRepository;
  18.         private readonly PlanetRepository planetRepository;
  19.         private readonly IMission mission;
  20.         private int exploredPlanetsCount;
  21.  
  22.         public Controller()
  23.         {
  24.             this.astronautRepository = new AstronautRepository();
  25.             this.planetRepository = new PlanetRepository();
  26.             this.mission = new Mission();
  27.             this.exploredPlanetsCount = 0;
  28.         }
  29.  
  30.         public string AddAstronaut(string type, string astronautName)
  31.         {
  32.             IAstronaut astronaut;
  33.  
  34.             switch (type)
  35.             {
  36.                 case "Biologist":
  37.                     astronaut = new Biologist(astronautName);
  38.                     break;
  39.  
  40.                 case "Geodesist":
  41.                     astronaut = new Geodesist(astronautName);
  42.                     break;
  43.  
  44.                 case "Meteorologist":
  45.                     astronaut = new Meteorologist(astronautName);
  46.                     break;
  47.  
  48.                 default:
  49.                     throw new InvalidOperationException(ExceptionMessages.InvalidAstronautType);
  50.             }
  51.  
  52.             this.astronautRepository.Add(astronaut);
  53.             return string.Format(OutputMessages.AstronautAdded,
  54.                 astronaut.GetType().Name,
  55.                 astronaut.Name);
  56.         }
  57.  
  58.         public string AddPlanet(string planetName, params string[] items)
  59.         {
  60.             Planet planet = new Planet(planetName);
  61.  
  62.             planet.AddItems(items);
  63.  
  64.             this.planetRepository.Add(planet);
  65.             return string.Format(OutputMessages.PlanetAdded, planet.Name);
  66.         }
  67.  
  68.         public string ExplorePlanet(string planetName)
  69.         {
  70.             List<IAstronaut> astronauts = this.astronautRepository
  71.                 .Models
  72.                 .Where(a => a.Oxygen > 60)
  73.                 .ToList();
  74.  
  75.             if (astronauts.Count == 0)
  76.             {
  77.                 throw new InvalidOperationException(ExceptionMessages.InvalidAstronautCount);
  78.             }
  79.  
  80.             IPlanet planet = this.planetRepository.FindByName(planetName);
  81.             this.mission.Explore(planet, astronauts);
  82.             this.exploredPlanetsCount++;
  83.  
  84.             return string.Format(OutputMessages.PlanetExplored,
  85.                 planet.Name,
  86.                 astronauts.Where(a => a.CanBreath == false).Count());
  87.         }
  88.  
  89.         public string Report()
  90.         {
  91.             StringBuilder sb = new StringBuilder();
  92.  
  93.             sb.AppendLine($"{this.exploredPlanetsCount} planets were explored!");
  94.             sb.AppendLine("Astronauts info:");
  95.  
  96.             foreach (IAstronaut astronaut in this.astronautRepository.Models)
  97.             {
  98.                 sb.AppendLine($"Name: { astronaut.Name}");
  99.                 sb.AppendLine($"Oxygen: {astronaut.Oxygen}");
  100.                 sb.AppendLine($"Bag items: {(astronaut.backpack.Items.Count == 0 ? "none" : string.Join(", ", astronaut.backpack.Items))} ");
  101.             }
  102.  
  103.             return sb.ToString().TrimEnd();
  104.         }
  105.  
  106.         public string RetireAstronaut(string astronautName)
  107.         {
  108.             if (this.astronautRepository.FindByName(astronautName) == null)
  109.             {
  110.                 throw new InvalidOperationException(
  111.                     string.Format(ExceptionMessages.InvalidRetiredAstronaut, astronautName));
  112.             }
  113.  
  114.             IAstronaut astronaut = this.astronautRepository.FindByName(astronautName);
  115.             this.astronautRepository.Remove(astronaut);
  116.  
  117.             return string.Format(OutputMessages.AstronautRetired, astronautName);
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement