Skip to content
Open

Exam #22

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions ExamCS_PilotSimulator/CitiesData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;

namespace ExamCS_PilotSimulator
{
public static class CitiseData
{
private static Random random = new Random();
private static City minsk = new City("Минск", new List<Dispatcher> { new Dispatcher("Михаил"), new Dispatcher("Андрей"), new Dispatcher("Павел") }, random.Next(-200, 200));
private static City mosсow = new City("Москва", new List<Dispatcher> { new Dispatcher("Александр"), new Dispatcher("Генадий"), new Dispatcher("Максим") }, random.Next(-200, 200));
private static City kiev = new City("Киев", new List<Dispatcher> { new Dispatcher("Виктор"), new Dispatcher("Вова"), new Dispatcher("Артур") }, random.Next(-200, 200));
private static City paris = new City("Париж", new List<Dispatcher> { new Dispatcher("Даниэль") }, random.Next(-200, 200));
private static City sanFrancisco = new City("Лос-Анджелес", new List<Dispatcher> { new Dispatcher("Вильям"), new Dispatcher("Дэвид") }, random.Next(-200, 200));
private static List<City> cities = new List<City>() { minsk, mosсow, kiev, paris, sanFrancisco };

public static List<City> GetData()
{
return cities;
}
}
}
20 changes: 20 additions & 0 deletions ExamCS_PilotSimulator/City.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;

namespace ExamCS_PilotSimulator
{
public class City
{
public string Name { get; set; }

public int HeightAdjustment { get; set; }

public List<Dispatcher> Dispatchers { get; set; }

public City(string name, List<Dispatcher> dispatchers, int heightAdjustment)
{
HeightAdjustment = heightAdjustment;
Name = name;
Dispatchers = dispatchers;
}
}
}
52 changes: 52 additions & 0 deletions ExamCS_PilotSimulator/Dispatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;

namespace ExamCS_PilotSimulator
{
public class Dispatcher
{
public string Name { get; set; }
public int PenaltyPoints { get; set; }
public int ReccomentedHeight { get; set; }
public bool IsActive { get; set; } = false;
public bool IsPilotMissionCompleted { get; set; } = false;

public Dispatcher(string name)
{
Name = name;
}

public void AddPenaltyPoints(Action<int, string> printPoints, int height, int speed, int maxSpeed)
{
int points = 0;
string message = "";
if (speed > maxSpeed)
{
message = "Немедленно снизте скорость!";
points += 100;
}
if (Math.Abs(height - ReccomentedHeight) > 600)
points += 50;
else if (Math.Abs(height - ReccomentedHeight) > 300)
points += 25;
PenaltyPoints += points;
if(speed > 50)
printPoints(points, message);
}

public bool CheckPlaneParams(int speed, int height, int maxSpeed, int N)
{
ReccomentedHeight = 7 * speed + N;
if(height < 0)
throw new FlightException("Земля близко! Самолет разбился.");
if (speed == 0 && height != 0)
throw new FlightException("Нулевая скорость! Штопор! Самолет разбился.");
if(Math.Abs(height - ReccomentedHeight) > 1000)
throw new FlightException("Слишком большое отклонение от рекомендуемой высоты (больше 1000 м). Вы непригодны.");
if(PenaltyPoints >= 1000)
throw new FlightException("Вы набрали 1000 штрафных очков от одного диспетчера. Вы непригодны.");
if (speed > maxSpeed || Math.Abs(height - ReccomentedHeight) > 300)
return false;
return true;
}
}
}
137 changes: 137 additions & 0 deletions ExamCS_PilotSimulator/Flight.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
using System;

namespace ExamCS_PilotSimulator
{
public class Flight
{
public string PilotName { get; set; }
public Plane Plane { get; set; }
public City StartedCity { get; set; }
public City FinishedCity { get; set; }
public int MaxSpeed { get; set; } = 1000;
public int MyProperty { get; set; }
public bool MissionCompleted { get; set; } = false;

Action printMissionCompleted = Program.PrintMissionCompleted;
Action<int> printResultPenaltyPoints = Program.PrintResult;
Action<int, string> printAddedPenaltyPoints = Program.PrintAddedPenaltyPoints;
Action<string, string, int> printReccomendation = Program.PrintReccomendation;

public event EventHandler<ChangeDispatcherArgs> ChangeDispatcher;
public event EventHandler<ChangePlaneParamsArgs> ChangePlane;

public Flight(Plane plane, string pilotName, City startedCity, City finishedCity)
{
PilotName = pilotName;
Plane = plane;
StartedCity = startedCity;
FinishedCity = finishedCity;
}

public void Start()
{
while (CheckFlightFinish() == false)
{
ChangePlane(this, new ChangePlaneParamsArgs(Plane));
PrintReccomendationAndPealtyPoints(StartedCity, printReccomendation);
PrintReccomendationAndPealtyPoints(FinishedCity, printReccomendation);
CheckMissionCimpleted();

var key = Console.ReadKey();
var speedMultiplier = key.Modifiers.HasFlag(ConsoleModifiers.Shift) ? 3 : 1;
var heightMultiplier = key.Modifiers.HasFlag(ConsoleModifiers.Shift) ? 2 : 1;

switch (key.Key)
{
case ConsoleKey.RightArrow:
Plane.Speed += 50 * speedMultiplier;
break;
case ConsoleKey.LeftArrow:
Plane.Speed -= 50 * speedMultiplier;
if (Plane.Speed < 0)
Plane.Speed = 0;
break;
case ConsoleKey.UpArrow:
if (Plane.Speed > 0)
Plane.Height += 250 * heightMultiplier;
break;
case ConsoleKey.DownArrow:
if (Plane.Speed > 0)
Plane.Height -= 250 * heightMultiplier;
break;
case ConsoleKey.D:
ChangeDispatcher(this, new ChangeDispatcherArgs(StartedCity, FinishedCity));
break;
default:
break;
}
}
PrintResult();
}

private void CheckMissionCimpleted()
{
if (Plane.Speed >= MaxSpeed)
MissionCompleted = true;
if(MissionCompleted)
printMissionCompleted();
}

private bool CheckFlightFinish()
{
if (MissionCompleted == true)
if (Plane.Height == 0 && Plane.Speed == 0)
return true;
return false;
}

private void PrintResult()
{
int points = 0;
foreach (var dispatcher in StartedCity.Dispatchers)
points += dispatcher.PenaltyPoints;
foreach (var dispatcher in FinishedCity.Dispatchers)
points += dispatcher.PenaltyPoints;
printResultPenaltyPoints(points);
}

private void PrintReccomendationAndPealtyPoints(City city, Action<string, string, int> printReccomendation)
{

foreach (var dispatcher in city.Dispatchers)
{
if (dispatcher.IsActive)
{
var result = dispatcher.CheckPlaneParams(Plane.Speed, Plane.Height, MaxSpeed, city.HeightAdjustment);
if(Plane.Speed > 50)
printReccomendation(dispatcher.Name, city.Name, dispatcher.ReccomentedHeight);
if (result == false)
dispatcher.AddPenaltyPoints(printAddedPenaltyPoints, Plane.Height, Plane.Speed, MaxSpeed);
break;
}
}
}
}

public class ChangeDispatcherArgs : EventArgs
{
public City StatrCity { get; set; }
public City FinishedCity { get; set; }

public ChangeDispatcherArgs(City statrCity, City finishedCity)
{
StatrCity = statrCity;
FinishedCity = finishedCity;
}
}

public class ChangePlaneParamsArgs : EventArgs
{
public Plane Plane { get; set; }

public ChangePlaneParamsArgs(Plane plane)
{
Plane = plane;
}
}
}
16 changes: 16 additions & 0 deletions ExamCS_PilotSimulator/FlightException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace ExamCS_PilotSimulator
{

[Serializable]
public class FlightException : Exception
{
public FlightException() { }
public FlightException(string message) : base(message) { }
public FlightException(string message, Exception inner) : base(message, inner) { }
protected FlightException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
}
17 changes: 17 additions & 0 deletions ExamCS_PilotSimulator/Plane.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

namespace ExamCS_PilotSimulator
{
public class Plane
{
public PlaneTypes PlaneType { get; set; }

public int Speed { get; set; }

public int Height { get; set; }

public Plane(PlaneTypes planeType)
{
PlaneType = planeType;
}
}
}
15 changes: 15 additions & 0 deletions ExamCS_PilotSimulator/PlaneTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

namespace ExamCS_PilotSimulator
{
public enum PlaneTypes
{
Boeing747,
Boeing777,
AirbusA320,
AirbusA380,
ИЛ96,
ТУ154,

EndingNoUsed
}
}
Loading