From 4b0563be614341b87fbbdb5458a5b72d89b051f6 Mon Sep 17 00:00:00 2001 From: Alisa Tsarova Date: Wed, 11 Oct 2017 22:42:21 +0300 Subject: [PATCH 1/2] Exam --- Exam/Airplane.cs | 25 ++++ Exam/AirplaneIsCrashedException.cs | 19 +++ Exam/CityEnum.cs | 21 ++++ Exam/Dispatcher.cs | 78 ++++++++++++ Exam/Flight.cs | 185 +++++++++++++++++++++++++++++ Exam/FlightEvent.cs | 102 ++++++++++++++++ Exam/Pilot.cs | 25 ++++ Exam/Program.cs | 24 ++++ Exam/UnfitForFlightException.cs | 19 +++ 9 files changed, 498 insertions(+) create mode 100644 Exam/Airplane.cs create mode 100644 Exam/AirplaneIsCrashedException.cs create mode 100644 Exam/CityEnum.cs create mode 100644 Exam/Dispatcher.cs create mode 100644 Exam/Flight.cs create mode 100644 Exam/FlightEvent.cs create mode 100644 Exam/Pilot.cs create mode 100644 Exam/Program.cs create mode 100644 Exam/UnfitForFlightException.cs diff --git a/Exam/Airplane.cs b/Exam/Airplane.cs new file mode 100644 index 0000000..f521336 --- /dev/null +++ b/Exam/Airplane.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace _2017._10._07_exam +{ + public class Airplane + { + public const int maxSpeed = 1000; + + public int Speed { get; set; } + public int Height { get; set; } + + public Airplane() + { + } + + public static void StartFlight() + { + } + + } +} diff --git a/Exam/AirplaneIsCrashedException.cs b/Exam/AirplaneIsCrashedException.cs new file mode 100644 index 0000000..827a75f --- /dev/null +++ b/Exam/AirplaneIsCrashedException.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace _2017._10._07_exam +{ + [System.Serializable] + public class AirplaneIsCrashedException : Exception + { + public AirplaneIsCrashedException() { } + public AirplaneIsCrashedException(string message) : base(message) { } + public AirplaneIsCrashedException(string message, Exception inner) : base(message, inner) { } + protected AirplaneIsCrashedException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + } +} diff --git a/Exam/CityEnum.cs b/Exam/CityEnum.cs new file mode 100644 index 0000000..3c53d83 --- /dev/null +++ b/Exam/CityEnum.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace _2017._10._07_exam +{ + public enum CityEnum + { + Minsk, + Moscow, + Kiev, + Warsaw, + SaintPetersburg, + Vilnius, + Riga, + Tallinn, + Praga + }; +} diff --git a/Exam/Dispatcher.cs b/Exam/Dispatcher.cs new file mode 100644 index 0000000..250d9f6 --- /dev/null +++ b/Exam/Dispatcher.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace _2017._10._07_exam +{ + public class Dispatcher + { + public string Name { get; private set; } + public bool Active { get; set; } + public string Id { get; private set; } + public CityEnum city { get; set; } + public int AdjustmenWeatherConditions { get; set; } + public int FirstMove { get; set; } + public int RecommendedHeightFlight { get; set; } + public int PenaltyPoints { get; set; } + public bool ControlStart { get; set; } + + public Dispatcher(string name) + { + Random rand = new Random(); + Name = name; + Id = GenerateID(); + Active = true; + AdjustmenWeatherConditions = rand.Next(-200, 200); + city = (CityEnum)rand.Next(1, 9); + FirstMove = 0; + PenaltyPoints = 0; + ControlStart = false; + } + + private string GenerateID() + { + DateTime date = DateTime.Now; + return date.Second.ToString() + Name; + } + + public void CheckAirplane(int speed, int height) + { + ++FirstMove; + RecommendedHeightFlight = 7 * speed - AdjustmenWeatherConditions; + Console.WriteLine($"Dispatcher {Name} recommended height of flight: {RecommendedHeightFlight}"); + + int difference = 0; + + difference = Math.Abs(height - RecommendedHeightFlight); + + if (difference >= 300 && difference < 600) + { + PenaltyPoints += 25; + } + else if (difference >= 600 && difference < 1000) + { + PenaltyPoints += 50; + } + else if (difference >= 1000 || ((speed == 0 && height == 0) && FirstMove < 2)) + { + throw new AirplaneIsCrashedException(); + } + else if (speed < 50) + { + throw new AirplaneIsCrashedException(); + } + else if(speed > 1000) + { + PenaltyPoints += 100; + Console.WriteLine("Please, reduce speed."); + } + + if (PenaltyPoints > 1000) + { + throw new UnfitForFlightException(); + } + } + } +} diff --git a/Exam/Flight.cs b/Exam/Flight.cs new file mode 100644 index 0000000..4ae7fa1 --- /dev/null +++ b/Exam/Flight.cs @@ -0,0 +1,185 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace _2017._10._07_exam +{ + public class Flight + { + public Airplane airplane = new Airplane(); + public Pilot Pilot { get; set; } + public List allDispatcher = new List(); + public int CountActiveDispatchers { get; set; } + + public Flight(string pilotName) + { + Pilot = new Pilot(pilotName); + CountActiveDispatchers = 0; + } + + public void AddDispatcher() + { + Console.WriteLine("You must have at least 2 dispatcher:"); + Console.WriteLine($"Now you have a {CountActiveDispatchers} dispatchers."); + string name = Pilot.AddDispetcher(); + Dispatcher dispatcher = new Dispatcher(name); + allDispatcher.Add(dispatcher); + CountActiveDispatchers++; + } + + public void DeleteDispatcher() + { + Console.WriteLine("Enter a number of dispatcher, which you want to change: "); + int index = 0; + Dispatcher disp; + foreach (var dispet in allDispatcher) + { + if (dispet.Active) + { + Console.WriteLine($"{index} - {dispet.Name}"); + } + index++; + } + int answerInt; + do + { + string answerStr = Console.ReadLine(); + answerInt = int.Parse(answerStr); + + if (answerInt == 3) { return; } + + } while (answerInt < 0 && answerInt >= allDispatcher.Count); + + disp = allDispatcher[answerInt]; + + allDispatcher.Remove(disp); + CountActiveDispatchers--; + + if (CountActiveDispatchers < 2) + { + AddDispatcher(); + } + } + + public void StartFlight() + { + + if(CountActiveDispatchers >= 2) + { + StartFlight fly = new StartFlight(); + + fly.Flight += (o, e) => { airplane.Speed += e.Speed; + Console.WriteLine($"Speed: {airplane.Speed}"); + }; + + fly.Flight += (o, e) => { + airplane.Height += e.Height; + Console.WriteLine($"Height {airplane.Height}"); + }; + + fly.Flight += (o, e) => + { + foreach (var d in allDispatcher) + { + if (d.Active) + { + if(airplane.Speed >= 50 && d.FirstMove == 0) + { + d.ControlStart = true; + } + } + if (d.ControlStart && d.Active) + { + if (airplane.Speed == 0 && airplane.Height == 0 && d.FirstMove >= 1) + { + Console.WriteLine("you have successfully landed the airplane"); + GetAllPenaltyPoint(); + e.IsFinished = true; + } + else + { + try + { + d.CheckAirplane(airplane.Speed, airplane.Height); + } + catch (AirplaneIsCrashedException ex) + { + Console.WriteLine("The airplane is crashed"); + GetAllPenaltyPoint(); + e.IsFinished = true; + } + catch (UnfitForFlightException ex) + { + Console.WriteLine("You are unfit to flight."); + GetAllPenaltyPoint(); + e.IsFinished = true; + } + } + } + else if (!d.ControlStart) + { + Console.WriteLine("For flight you need increase speed more the less 50 km/h."); + airplane.Height = 0; + } + } + }; + + fly.Flight += (o, e) => + { + if(e.GoToMenu == true) + { + if(Menu()) + { + e.GoToMenu = false; + } + } + }; + + fly.Start(); + } + else + { + Console.WriteLine("you can't begin to control the plane, as you have less than 2 dispatchers!"); + } + } + + public bool Menu() + { + int answerInt = 0; + do + { + Console.WriteLine("Menu:\nEnter what you want to do:"); + Console.WriteLine("1 - Add dispatcher\n2 - Delete dispatcher\n3 - StartFly\n4 - Exit"); + string answerStr = Console.ReadLine(); + answerInt = int.Parse(answerStr); + if (answerInt == 4) { return false; } + } while (answerInt <= 0 && answerInt >= 4); + + switch(answerInt) + { + case 1: + AddDispatcher(); + return true; + case 2: + DeleteDispatcher(); + return true; + case 3: + StartFlight(); + return true; + } + return false; + } + + public void GetAllPenaltyPoint() + { + int allPenaltyPoint = 0; + foreach(var dispatcher in allDispatcher) + { + allPenaltyPoint += dispatcher.PenaltyPoints; + } + Console.WriteLine($"Your penalty point {allPenaltyPoint}"); + } + } +} diff --git a/Exam/FlightEvent.cs b/Exam/FlightEvent.cs new file mode 100644 index 0000000..2e42fa2 --- /dev/null +++ b/Exam/FlightEvent.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace _2017._10._07_exam +{ + public class FlightEvent : EventArgs + { + public int Speed { get; set; } + public int Height { get; set; } + public bool IsFinished { get; set; } + public bool GoToMenu { get; set; } + + public FlightEvent(int speed, int height, bool isFinished, bool goToMenu) + { + Speed = speed; + Height = height; + IsFinished = isFinished; + GoToMenu = goToMenu; + } + } + + public class StartFlight + { + public event EventHandler Flight; + + public void Start() + { + Console.WriteLine("Speed — modified arrow keys Left and Right\n" + + "(Right: +50 km/h, Left: –50 km/h, Shift-Right: +150 km/h, Shift - Left: –150 km/h)"); + Console.WriteLine("Height — changes the arrow keys Up and Down:\n" + + "(Up: +250 m, Down: –250 m, Shift-Up: +500 m, Shift-Down: –500 m)"); + Console.WriteLine("Go to menu: M"); + + FlightEvent events; + do + { + var key = Console.ReadKey(); + Func changeSpeed = null; + Func changeHeignt = null; + Func changeFinished = null; + Func changeGoToMenu = null; + + var speedMul = key.Modifiers.HasFlag(ConsoleModifiers.Shift) ? 3 : 1; + var heightMul = key.Modifiers.HasFlag(ConsoleModifiers.Shift) ? 5 : 1; + switch (key.Key) + { + case ConsoleKey.LeftArrow: + changeSpeed = (x) => x - 50 * speedMul; + changeHeignt = (y) => y; + changeFinished = (z) => z; + changeGoToMenu = (m) => m; + break; + case ConsoleKey.RightArrow: + changeSpeed = (x) => x + 50 * speedMul; + changeHeignt = (y) => y; + changeFinished = (z) => z; + changeGoToMenu = (m) => m; + break; + case ConsoleKey.DownArrow: + changeSpeed = (x) => x; + changeHeignt = (y) => y - 50 * heightMul; + changeFinished = (z) => z; + changeGoToMenu = (m) => m; + break; + case ConsoleKey.UpArrow: + changeSpeed = (x) => x; + changeHeignt = (y) => y + 50 * heightMul; + changeFinished = (z) => z; + changeGoToMenu = (m) => m; + break; + case ConsoleKey.M: + changeSpeed = (x) => x; + changeHeignt = (y) => y; + changeFinished = (z) => z; + changeGoToMenu = (m) => true; + break; + } + + var resultSpeed = changeSpeed(0); + var resultHeight = changeHeignt(0); + var reusulFinished = changeFinished(false); + var reusulGoToMenu = changeGoToMenu(false); + + events = new FlightEvent(resultSpeed, resultHeight, reusulFinished, reusulGoToMenu); + OnStartFly(events); + } while (!events.IsFinished); + } + + private void OnStartFly(FlightEvent events) + { + var handler = Flight;//handler указатель на функцию + + if (handler != null) + { + handler(this, events); + } + } + } +} diff --git a/Exam/Pilot.cs b/Exam/Pilot.cs new file mode 100644 index 0000000..81fc830 --- /dev/null +++ b/Exam/Pilot.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace _2017._10._07_exam +{ + public class Pilot + { + public string Name { get; private set; } + + public Pilot(string name) + { + Name = name; + } + + public string AddDispetcher() + { + Console.WriteLine("Enter dispatcher name: "); + return Console.ReadLine(); + } + + } +} diff --git a/Exam/Program.cs b/Exam/Program.cs new file mode 100644 index 0000000..b4b17d2 --- /dev/null +++ b/Exam/Program.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace _2017._10._07_exam +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello, pilot! Enter your name please: "); + string pilotName = Console.ReadLine(); + Flight flight = new Flight(pilotName); + + Console.WriteLine("{0}, you control the airplane Tu-154.", pilotName); + + while(flight.Menu()) + { + } + } + } +} diff --git a/Exam/UnfitForFlightException.cs b/Exam/UnfitForFlightException.cs new file mode 100644 index 0000000..1a6a7fe --- /dev/null +++ b/Exam/UnfitForFlightException.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace _2017._10._07_exam +{ + [System.Serializable] + public class UnfitForFlightException : Exception + { + public UnfitForFlightException() { } + public UnfitForFlightException(string message) : base(message) { } + public UnfitForFlightException(string message, Exception inner) : base(message, inner) { } + protected UnfitForFlightException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + } +} From 09191691a54a1d7a420d546815c398bd94655aab Mon Sep 17 00:00:00 2001 From: Alisa Tsarova Date: Thu, 12 Oct 2017 09:26:09 +0300 Subject: [PATCH 2/2] Update Flight.cs --- Exam/Flight.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Exam/Flight.cs b/Exam/Flight.cs index 4ae7fa1..b5c77cb 100644 --- a/Exam/Flight.cs +++ b/Exam/Flight.cs @@ -33,7 +33,7 @@ public void DeleteDispatcher() { Console.WriteLine("Enter a number of dispatcher, which you want to change: "); int index = 0; - Dispatcher disp; + foreach (var dispet in allDispatcher) { if (dispet.Active) @@ -52,9 +52,8 @@ public void DeleteDispatcher() } while (answerInt < 0 && answerInt >= allDispatcher.Count); - disp = allDispatcher[answerInt]; - - allDispatcher.Remove(disp); + allDispatcher[answerInt].Active = false; + CountActiveDispatchers--; if (CountActiveDispatchers < 2)