Advertisement
desislava_topuzakova

Program.cs -> Fitness

May 10th, 2020
1,138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.66 KB | None | 0 0
  1. using Exam_Preparation_2_Fitness;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5.  
  6. namespace Exam_Preparation_2
  7. {
  8.     class Program
  9.     {
  10.         static Dictionary<string, Coach> coaches = new Dictionary<string, Coach>();
  11.         static Dictionary<string, Athlete> athletes = new Dictionary<string, Athlete>();
  12.  
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             string command;
  17.  
  18.             while ((command = Console.ReadLine()) != "End")
  19.             {
  20.                 var commandArgs = command.Split(' ').ToArray();
  21.  
  22.                 switch (commandArgs[0])
  23.                 {
  24.                     case "AddAthlete":
  25.                         AddAthlete(commandArgs.Skip(1).ToArray());
  26.                         break;
  27.                     case "PrintAthleteInfoByName":
  28.                         PrintAthleteInfoByName(commandArgs.Skip(1).ToArray());
  29.                         break;
  30.                     case "AddCoach":
  31.                         AddCoach(commandArgs.Skip(1).ToArray());
  32.                         break;
  33.                     case "PrintCoachInfoByName":
  34.                         PrintCoachInfoByName(commandArgs.Skip(1).ToArray());
  35.                         break;
  36.                     case "AddAthleteToSpecificCoach":
  37.                         AddAthleteToSpecificCoach(commandArgs.Skip(1).ToArray());
  38.                         break;
  39.                     case "RemoveAthleteFromSpecificCoach":
  40.                         RemoveAthleteFromSpecificCoach(commandArgs.Skip(1).ToArray());
  41.                         break;
  42.                     case "GetClientsCount":
  43.                         GetClientsCount(commandArgs.Skip(1).ToArray());
  44.                         break;
  45.                     case "GetAllClientsTougherThan":
  46.                         GetAllClientsTougherThan(commandArgs.Skip(1).ToArray());
  47.                         break;
  48.                     case "SpendMoneyOnSupplements":
  49.                         SpendMoneyOnSupplements(commandArgs.Skip(1).ToArray());
  50.                         break;
  51.                     case "TrainHard":
  52.                         TrainHard(commandArgs.Skip(1).ToArray());
  53.                         break;
  54.                     case "TrainAthlete":
  55.                         TrainAthlete(commandArgs.Skip(1).ToArray());
  56.                         break;
  57.                     case "RemoveAthletesLeakerThan":
  58.                         RemoveAthletesLeakerThan(commandArgs.Skip(1).ToArray());
  59.                         break;
  60.                     case "GetToughestAthlete":
  61.                         GetToughestAthlete(commandArgs.Skip(1).ToArray());
  62.                         break;
  63.                     default:
  64.                         Console.WriteLine("Invalid command!");
  65.                         break;
  66.                 }
  67.             }
  68.         }
  69.  
  70.         private static void AddAthlete(string[] athleteInfo)
  71.         {
  72.             string name = athleteInfo[0];
  73.             int age = int.Parse(athleteInfo[1]);
  74.             int strentgh = int.Parse(athleteInfo[2]);
  75.  
  76.             if (athletes.ContainsKey(name))
  77.             {
  78.                 Console.WriteLine("Cannot add athlete! Athlete already exists!");
  79.                 return;
  80.             }
  81.  
  82.             try
  83.             {
  84.                 Athlete athlete = new Athlete(name, age, strentgh);
  85.                 athletes.Add(name, athlete);
  86.             }
  87.             catch (ArgumentException ex)
  88.             {
  89.                 Console.WriteLine(ex.Message);
  90.             }
  91.         }
  92.  
  93.         private static void PrintAthleteInfoByName(string[] athleteInfo)
  94.         {
  95.             string athleteName = athleteInfo[0];
  96.  
  97.             if (!athletes.ContainsKey(athleteName))
  98.             {
  99.                 Console.WriteLine($"Invalid athlete {athleteName}");
  100.                 return;
  101.             }
  102.  
  103.             Console.WriteLine(athletes[athleteName].ToString());
  104.         }
  105.  
  106.         private static void AddCoach(string[] coachInfo)
  107.         {
  108.             string name = coachInfo[0];
  109.             int experience = int.Parse(coachInfo[1]);
  110.  
  111.             if (coaches.ContainsKey(name))
  112.             {
  113.                 Console.WriteLine("Cannot add coach! Coach already exists!");
  114.                 return;
  115.             }
  116.  
  117.             Coach coach;
  118.  
  119.             try
  120.             {
  121.                 if (coachInfo.Length == 2)
  122.                 {
  123.                     coach = new Coach(name, experience);
  124.                 }
  125.                 else
  126.                 {
  127.                     double money = double.Parse(coachInfo[2]);
  128.                     coach = new Coach(name, experience, money);
  129.                 }
  130.                 coaches.Add(name, coach);
  131.             }
  132.             catch (ArgumentException ex)
  133.             {
  134.                 Console.WriteLine(ex.Message);
  135.             }
  136.         }
  137.  
  138.         private static void PrintCoachInfoByName(string[] coachInfo)
  139.         {
  140.             string name = coachInfo[0];
  141.  
  142.             if (!coaches.ContainsKey(name))
  143.             {
  144.                 Console.WriteLine($"Invalid coach {name}");
  145.                 return;
  146.             }
  147.  
  148.             Console.WriteLine(coaches[name].ToString());
  149.         }
  150.  
  151.         private static void AddAthleteToSpecificCoach(string[] info)
  152.         {
  153.             string athleteName = info[0];
  154.             string coachName = info[1];
  155.  
  156.             if (!coaches.ContainsKey(coachName))
  157.             {
  158.                 Console.WriteLine($"Invalid coach {coachName}");
  159.                 return;
  160.             }
  161.  
  162.             if (!athletes.ContainsKey(athleteName))
  163.             {
  164.                 Console.WriteLine($"Invalid athlete {athleteName}");
  165.                 return;
  166.             }
  167.  
  168.             Coach coach = coaches[coachName];
  169.             Athlete athlete = athletes[athleteName];
  170.  
  171.             coach.AddNewClient(athlete);
  172.         }
  173.  
  174.         private static void RemoveAthleteFromSpecificCoach(string[] info)
  175.         {
  176.             string athleteName = info[0];
  177.             string coachName = info[1];
  178.  
  179.             if (!coaches.ContainsKey(coachName))
  180.             {
  181.                 Console.WriteLine($"Invalid coach {coachName}");
  182.                 return;
  183.             }
  184.  
  185.             if (!athletes.ContainsKey(athleteName))
  186.             {
  187.                 Console.WriteLine($"Invalid athlete {athleteName}");
  188.                 return;
  189.             }
  190.  
  191.             Coach coach = coaches[coachName];
  192.             Athlete athlete = athletes[athleteName];
  193.  
  194.             bool removed = coach.LooseClient(athlete.Name);
  195.  
  196.             if (removed)
  197.             {
  198.                 Console.WriteLine($"Successfully removed athlete: {athlete.Name} from coach {coach.Name}!");
  199.             }
  200.             else
  201.             {
  202.                 Console.WriteLine($"Athlete removal failed.");
  203.             }
  204.         }
  205.  
  206.         private static void GetClientsCount(string[] info)
  207.         {
  208.             string coachName = info[0];
  209.  
  210.             if (!coaches.ContainsKey(coachName))
  211.             {
  212.                 Console.WriteLine($"Invalid coach {coachName}");
  213.  
  214.                 return;
  215.             }
  216.  
  217.             Coach coach = coaches[coachName];
  218.  
  219.             int count = coach.GetClientsCount();
  220.  
  221.             Console.WriteLine($"Coach {coach.Name} has total count of clients: {count}.");
  222.         }
  223.  
  224.         private static void GetAllClientsTougherThan(string[] info)
  225.         {
  226.             string coachName = info[0];
  227.             int strength = int.Parse(info[1]);
  228.  
  229.             if (!coaches.ContainsKey(coachName))
  230.             {
  231.                 Console.WriteLine($"Invalid coach {coachName}");
  232.  
  233.                 return;
  234.             }
  235.  
  236.             Coach coach = coaches[coachName];
  237.  
  238.             List<Athlete> toughGuys = coach.GetAllClientsTougherThan(strength);
  239.  
  240.             Console.WriteLine("Tough Guys:");
  241.  
  242.             for (int i = 0; i < toughGuys.Count; i++)
  243.             {
  244.                 Console.WriteLine($"{i}: {toughGuys[i]}");
  245.             }
  246.         }
  247.  
  248.         private static void SpendMoneyOnSupplements(string[] info)
  249.         {
  250.             string coachName = info[0];
  251.             double money = double.Parse(info[1]);
  252.  
  253.             if (!coaches.ContainsKey(coachName))
  254.             {
  255.                 Console.WriteLine($"Invalid coach {coachName}");
  256.                 return;
  257.             }
  258.  
  259.             Coach coach = coaches[coachName];
  260.             try
  261.             {
  262.                 coach.SpendMoneyOnSupplements(money);
  263.                 Console.WriteLine($"Coach {coachName} spent on supplements {money:F2}");
  264.             }
  265.             catch (ArgumentException ex)
  266.             {
  267.                 Console.WriteLine(ex.Message);
  268.             }
  269.         }
  270.  
  271.         private static void TrainHard(string[] info)
  272.         {
  273.             string coachName = info[0];
  274.  
  275.             if (!coaches.ContainsKey(coachName))
  276.             {
  277.                 Console.WriteLine($"Invalid coach {coachName}");
  278.  
  279.                 return;
  280.             }
  281.  
  282.             Coach coach = coaches[coachName];
  283.  
  284.             coach.TrainHard();
  285.         }
  286.  
  287.         private static void TrainAthlete(string[] info)
  288.         {
  289.             string athleteName = info[0];
  290.             string coachName = info[1];
  291.  
  292.             if (!coaches.ContainsKey(coachName))
  293.             {
  294.                 Console.WriteLine($"Invalid coach {coachName}");
  295.  
  296.                 return;
  297.             }
  298.  
  299.             if (!athletes.ContainsKey(athleteName))
  300.             {
  301.                 Console.WriteLine($"Invalid athlete {athleteName}");
  302.  
  303.                 return;
  304.             }
  305.  
  306.             Coach coach = coaches[coachName];
  307.             Athlete athlete = athletes[athleteName];
  308.  
  309.             coach.TrainClient(athlete);
  310.         }
  311.  
  312.         private static void RemoveAthletesLeakerThan(string[] info)
  313.         {
  314.             string coachName = info[0];
  315.             int strength = int.Parse(info[1]);
  316.  
  317.             if (!coaches.ContainsKey(coachName))
  318.             {
  319.                 Console.WriteLine($"Invalid coach {coachName}");
  320.  
  321.                 return;
  322.             }
  323.  
  324.             Coach coach = coaches[coachName];
  325.  
  326.             int loosers = coach.RemoveAllClientsLeakerThan(strength);
  327.  
  328.             Console.WriteLine($"Coach {coach.Name} removed {loosers} loosers.");
  329.         }
  330.  
  331.         private static void GetToughestAthlete(string[] info)
  332.         {
  333.             string coachName = info[0];
  334.  
  335.             if (!coaches.ContainsKey(coachName))
  336.             {
  337.                 Console.WriteLine($"Invalid coach {coachName}");
  338.  
  339.                 return;
  340.             }
  341.  
  342.             Coach coach = coaches[coachName];
  343.  
  344.             Athlete toughest = coach.GetToughestClient();
  345.  
  346.             Console.WriteLine($"Toughest client of coach {coach.Name} is {toughest.ToString()}");
  347.         }
  348.     }
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement