From 44a7ac5fff17ef7242f4e7ef1f8bb4cb637f0106 Mon Sep 17 00:00:00 2001 From: Gv1don Date: Tue, 13 Sep 2022 20:40:39 +0300 Subject: [PATCH 1/9] Add solution for first task --- CourseApp.Tests/DemoTest.cs | 13 ------ CourseApp.Tests/HydroplaneMethodsTests.cs | 47 +++++++++++++++++++ CourseApp/Class/Hydroplane.cs | 56 +++++++++++++++++++++++ CourseApp/Class/ILanding.cs | 9 ++++ CourseApp/Class/ITakeoff.cs | 9 ++++ CourseApp/Class/Plane.cs | 22 +++++++++ CourseApp/Class/Program.cs | 22 +++++++++ CourseApp/Program.cs | 12 ----- 8 files changed, 165 insertions(+), 25 deletions(-) delete mode 100644 CourseApp.Tests/DemoTest.cs create mode 100644 CourseApp.Tests/HydroplaneMethodsTests.cs create mode 100644 CourseApp/Class/Hydroplane.cs create mode 100644 CourseApp/Class/ILanding.cs create mode 100644 CourseApp/Class/ITakeoff.cs create mode 100644 CourseApp/Class/Plane.cs create mode 100644 CourseApp/Class/Program.cs delete mode 100644 CourseApp/Program.cs diff --git a/CourseApp.Tests/DemoTest.cs b/CourseApp.Tests/DemoTest.cs deleted file mode 100644 index cf7cbb1..0000000 --- a/CourseApp.Tests/DemoTest.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace CourseApp.Tests -{ - using Xunit; - - public class DemoTest - { - [Fact] - public void Test1() - { - Assert.True(true); - } - } -} diff --git a/CourseApp.Tests/HydroplaneMethodsTests.cs b/CourseApp.Tests/HydroplaneMethodsTests.cs new file mode 100644 index 0000000..bfcd57c --- /dev/null +++ b/CourseApp.Tests/HydroplaneMethodsTests.cs @@ -0,0 +1,47 @@ +namespace CourseApp.Tests +{ + using CourseApp.Class; + using Xunit; + + public class HydroplaneMethodsTests + { + [Fact] + public void SpeedUp_SetSampleDate_ReturnsCorrectSpeed() + { + Hydroplane hydroplane = new Hydroplane(450, 9, 1361, 80, 90, 8, 2, "DHC-2 Beaver", 255); + var result = hydroplane.SpeedUp(); + + Assert.Equal(hydroplane.GetSpeed(), result); + } + + [Fact] + public void SpeedUpDouble_SetSampleDate_ReturnCorrectSpeed() + { + var hydroplane = new Hydroplane(450, 9, 1361, 80, 90, 8, 2, "DHC-2 Beaver", 255); + hydroplane.SpeedUp(); + var result = hydroplane.SpeedUp(); + + Assert.Equal(hydroplane.GetSpeed(), result); + } + + [Fact] + public void SpeedDown_SetSampleDate_ReturnCorrectSpeed() + { + var hydroplane = new Hydroplane(450, 9, 1361, 80, 90, 8, 2, "DHC-2 Beaver", 255); + hydroplane.SpeedUp(); + hydroplane.SpeedDown(); + + Assert.Equal(80, hydroplane.GetSpeed()); + } + + [Fact] + public void Braking_SetSampleDate_ReturnsCorrectSpeed() + { + var hydroplane = new Hydroplane(450, 9, 1361, 80, 90, 8, 2, "DHC-2 Beaver", 255); + hydroplane.SpeedUp(); + hydroplane.Braking(); + + Assert.Equal(0, hydroplane.GetSpeed()); + } + } +} diff --git a/CourseApp/Class/Hydroplane.cs b/CourseApp/Class/Hydroplane.cs new file mode 100644 index 0000000..740e649 --- /dev/null +++ b/CourseApp/Class/Hydroplane.cs @@ -0,0 +1,56 @@ +namespace CourseApp.Class +{ + using System; + + public class Hydroplane : Plane, ILanding, ITakeoff + { + private int lengthFloat; + private int speed; + private int power; + private int length; + private int weight; + private int landingSpeed; + private int takeoffSpeed; + private int numberWings; + private string direction; + private string model; + private int maxSpeed; + + public Hydroplane(int power, int length, int weight, int landingSpeed, int takeoffSpeed, int lengthFloat, int numberWings, string model, int maxSpeed) + : base(numberWings) + { + this.power = power; + this.length = length; + this.weight = weight; + this.landingSpeed = landingSpeed; + this.takeoffSpeed = takeoffSpeed; + this.lengthFloat = lengthFloat; + this.numberWings = numberWings; + this.model = model; + this.maxSpeed = maxSpeed; + direction = string.Empty; + speed = 0; + } + + public override void GetInfo() + => Console.WriteLine($"Model: {model} \nPower: {power}horsepower \nLength: {length}m \nWeight: {weight}kg \nLanding speed: {landingSpeed}km/h \nTakeoff speed: {takeoffSpeed}km/h \nMax speed: {maxSpeed}km/h"); + + public override void Movement() => Console.WriteLine($"Самолёт движется со скоростью {speed} км/ч, в направлении: {direction}"); + + public override void TakeSpeed() => Console.WriteLine($"Speed right now: {speed}km/h"); + + public int GetSpeed() => speed; + + public void SpeedDown() => speed = landingSpeed; + + public void Braking() => speed = 0; + + public void SetDirection() + { + Console.Write("Введите направление, курс полёта: "); + direction = Console.ReadLine(); + } + + public int SpeedUp() => speed == 0 ? speed = takeoffSpeed : speed = maxSpeed; + } +} diff --git a/CourseApp/Class/ILanding.cs b/CourseApp/Class/ILanding.cs new file mode 100644 index 0000000..f84e142 --- /dev/null +++ b/CourseApp/Class/ILanding.cs @@ -0,0 +1,9 @@ +namespace CourseApp.Class +{ + public interface ILanding + { + void SpeedDown(); + + void Braking(); + } +} diff --git a/CourseApp/Class/ITakeoff.cs b/CourseApp/Class/ITakeoff.cs new file mode 100644 index 0000000..855fe81 --- /dev/null +++ b/CourseApp/Class/ITakeoff.cs @@ -0,0 +1,9 @@ +namespace CourseApp.Class +{ + public interface ITakeoff + { + void SetDirection(); + + int SpeedUp(); + } +} diff --git a/CourseApp/Class/Plane.cs b/CourseApp/Class/Plane.cs new file mode 100644 index 0000000..e63b3bd --- /dev/null +++ b/CourseApp/Class/Plane.cs @@ -0,0 +1,22 @@ +namespace CourseApp +{ + public abstract class Plane + { + private int numberWings; + + public Plane() + { + } + + public Plane(int numberWings) + { + this.numberWings = numberWings; + } + + public abstract void TakeSpeed(); + + public abstract void Movement(); + + public abstract void GetInfo(); + } +} diff --git a/CourseApp/Class/Program.cs b/CourseApp/Class/Program.cs new file mode 100644 index 0000000..44bc364 --- /dev/null +++ b/CourseApp/Class/Program.cs @@ -0,0 +1,22 @@ +namespace CourseApp.Class +{ + public class Program + { + public static void Main() + { + Hydroplane hydroplane = new Hydroplane(450, 9, 1361, 80, 90, 8, 2, "DHC-2 Beaver", 255); + + hydroplane.GetInfo(); + hydroplane.SetDirection(); + hydroplane.TakeSpeed(); + hydroplane.SpeedUp(); + hydroplane.TakeSpeed(); + hydroplane.SpeedUp(); + hydroplane.Movement(); + hydroplane.SpeedDown(); + hydroplane.TakeSpeed(); + hydroplane.Braking(); + hydroplane.TakeSpeed(); + } + } +} diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs deleted file mode 100644 index d6d2c87..0000000 --- a/CourseApp/Program.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace CourseApp -{ - using System; - - public class Program - { - public static void Main(string[] args) - { - Console.WriteLine("Hello World"); - } - } -} From 89bb0b4ba4a07369ad636aa78360d92ceeea0dbd Mon Sep 17 00:00:00 2001 From: Gv1don Date: Sun, 18 Sep 2022 22:09:13 +0300 Subject: [PATCH 2/9] Fix on comments --- CourseApp.Tests/HydroplaneMethodsTests.cs | 45 ++++++++---- CourseApp/Class/Hydroplane.cs | 89 ++++++++++++++++------- CourseApp/Class/ITakeoff.cs | 4 +- CourseApp/Class/Plane.cs | 14 ++-- CourseApp/Class/Program.cs | 11 ++- 5 files changed, 107 insertions(+), 56 deletions(-) diff --git a/CourseApp.Tests/HydroplaneMethodsTests.cs b/CourseApp.Tests/HydroplaneMethodsTests.cs index bfcd57c..41fe80e 100644 --- a/CourseApp.Tests/HydroplaneMethodsTests.cs +++ b/CourseApp.Tests/HydroplaneMethodsTests.cs @@ -8,40 +8,53 @@ public class HydroplaneMethodsTests [Fact] public void SpeedUp_SetSampleDate_ReturnsCorrectSpeed() { - Hydroplane hydroplane = new Hydroplane(450, 9, 1361, 80, 90, 8, 2, "DHC-2 Beaver", 255); - var result = hydroplane.SpeedUp(); - - Assert.Equal(hydroplane.GetSpeed(), result); - } - - [Fact] - public void SpeedUpDouble_SetSampleDate_ReturnCorrectSpeed() - { - var hydroplane = new Hydroplane(450, 9, 1361, 80, 90, 8, 2, "DHC-2 Beaver", 255); + Hydroplane hydroplane = new Hydroplane(80, 90, "DHC-2 Beaver", 255); hydroplane.SpeedUp(); - var result = hydroplane.SpeedUp(); - Assert.Equal(hydroplane.GetSpeed(), result); + Assert.Equal(80, hydroplane.GetSpeed); } [Fact] public void SpeedDown_SetSampleDate_ReturnCorrectSpeed() { - var hydroplane = new Hydroplane(450, 9, 1361, 80, 90, 8, 2, "DHC-2 Beaver", 255); + var hydroplane = new Hydroplane(80, 90, "DHC-2 Beaver", 255); hydroplane.SpeedUp(); hydroplane.SpeedDown(); - Assert.Equal(80, hydroplane.GetSpeed()); + Assert.Equal(80, hydroplane.GetSpeed); } [Fact] public void Braking_SetSampleDate_ReturnsCorrectSpeed() { - var hydroplane = new Hydroplane(450, 9, 1361, 80, 90, 8, 2, "DHC-2 Beaver", 255); + var hydroplane = new Hydroplane(80, 90, "DHC-2 Beaver", 255); hydroplane.SpeedUp(); hydroplane.Braking(); - Assert.Equal(0, hydroplane.GetSpeed()); + Assert.Equal(0, hydroplane.GetSpeed); + } + + [Fact] + public void GetInfo_SetSampleData_ReturnCorrectInfo() + { + var hydroplane = new Hydroplane(80, 90, "DHC-2 Beaver", 255); + Assert.Equal( + @"Model: DHC-2 Beaver +Landing speed: 80km/h +Takeoff speed: 90km/h +Max speed: 255km/h", hydroplane.GetInfo()); + } + + [Fact] + public void CheckConstructor_SetSampleData_ReturnCorrectInfo() + { + Plane hydroplane = new Hydroplane(90, 110, "SameHydroplane", 300); + + Assert.Equal( + @"Model: SameHydroplane +Landing speed: 90km/h +Takeoff speed: 110km/h +Max speed: 300km/h", hydroplane.GetInfo()); } } } diff --git a/CourseApp/Class/Hydroplane.cs b/CourseApp/Class/Hydroplane.cs index 740e649..03668a0 100644 --- a/CourseApp/Class/Hydroplane.cs +++ b/CourseApp/Class/Hydroplane.cs @@ -4,53 +4,88 @@ public class Hydroplane : Plane, ILanding, ITakeoff { - private int lengthFloat; - private int speed; - private int power; - private int length; - private int weight; private int landingSpeed; + private string info; private int takeoffSpeed; - private int numberWings; - private string direction; private string model; private int maxSpeed; + private int speed; + private string direction; - public Hydroplane(int power, int length, int weight, int landingSpeed, int takeoffSpeed, int lengthFloat, int numberWings, string model, int maxSpeed) - : base(numberWings) + public Hydroplane(int landingSpeed, int takeoffSpeed, string model, int maxSpeed) { - this.power = power; - this.length = length; - this.weight = weight; this.landingSpeed = landingSpeed; this.takeoffSpeed = takeoffSpeed; - this.lengthFloat = lengthFloat; - this.numberWings = numberWings; this.model = model; this.maxSpeed = maxSpeed; - direction = string.Empty; - speed = 0; } - public override void GetInfo() - => Console.WriteLine($"Model: {model} \nPower: {power}horsepower \nLength: {length}m \nWeight: {weight}kg \nLanding speed: {landingSpeed}km/h \nTakeoff speed: {takeoffSpeed}km/h \nMax speed: {maxSpeed}km/h"); + public override int GetSpeed + { + get + { + return speed; + } - public override void Movement() => Console.WriteLine($"Самолёт движется со скоростью {speed} км/ч, в направлении: {direction}"); + set + { + speed = value; + } + } - public override void TakeSpeed() => Console.WriteLine($"Speed right now: {speed}km/h"); + public string Direction + { + get + { + return direction; + } - public int GetSpeed() => speed; + set + { + direction = value; + } + } - public void SpeedDown() => speed = landingSpeed; + public override string GetInfo() + { + info = @$"Model: {model} +Landing speed: {landingSpeed}km/h +Takeoff speed: {takeoffSpeed}km/h +Max speed: {maxSpeed}km/h"; + Console.WriteLine(info); + return info; + } + + public override void Movement() + { + Console.Write("Enter the direction of the plane: "); + Direction = Console.ReadLine(); + Console.WriteLine($"The plane is moving at a speed {GetSpeed}km/h in the direction: {Direction}"); + } - public void Braking() => speed = 0; + public void SpeedDown() + { + GetSpeed = landingSpeed; + Console.WriteLine("The plane is landing"); + } - public void SetDirection() + public void Braking() { - Console.Write("Введите направление, курс полёта: "); - direction = Console.ReadLine(); + GetSpeed = 0; + Console.WriteLine("The plane has landed"); } - public int SpeedUp() => speed == 0 ? speed = takeoffSpeed : speed = maxSpeed; + public void SpeedUp() + { + if (GetSpeed == 0) + { + Console.WriteLine("The Plane is taking off"); + speed = landingSpeed; + } + else + { + speed = maxSpeed; + } + } } } diff --git a/CourseApp/Class/ITakeoff.cs b/CourseApp/Class/ITakeoff.cs index 855fe81..9cabb6c 100644 --- a/CourseApp/Class/ITakeoff.cs +++ b/CourseApp/Class/ITakeoff.cs @@ -2,8 +2,8 @@ { public interface ITakeoff { - void SetDirection(); + string Direction { get; set; } - int SpeedUp(); + void SpeedUp(); } } diff --git a/CourseApp/Class/Plane.cs b/CourseApp/Class/Plane.cs index e63b3bd..8157c6f 100644 --- a/CourseApp/Class/Plane.cs +++ b/CourseApp/Class/Plane.cs @@ -2,21 +2,25 @@ { public abstract class Plane { - private int numberWings; + private int length; + private int weight; + private int power; public Plane() { } - public Plane(int numberWings) + public Plane(int length, int weight, int power) { - this.numberWings = numberWings; + this.length = length; + this.weight = weight; + this.power = power; } - public abstract void TakeSpeed(); + public abstract int GetSpeed { get; set; } public abstract void Movement(); - public abstract void GetInfo(); + public abstract string GetInfo(); } } diff --git a/CourseApp/Class/Program.cs b/CourseApp/Class/Program.cs index 44bc364..ffc2c41 100644 --- a/CourseApp/Class/Program.cs +++ b/CourseApp/Class/Program.cs @@ -1,22 +1,21 @@ namespace CourseApp.Class { + using System; + public class Program { public static void Main() { - Hydroplane hydroplane = new Hydroplane(450, 9, 1361, 80, 90, 8, 2, "DHC-2 Beaver", 255); + Hydroplane hydroplane = new Hydroplane(80, 90, "DHC-2 Beaver", 255); hydroplane.GetInfo(); - hydroplane.SetDirection(); - hydroplane.TakeSpeed(); hydroplane.SpeedUp(); - hydroplane.TakeSpeed(); hydroplane.SpeedUp(); hydroplane.Movement(); hydroplane.SpeedDown(); - hydroplane.TakeSpeed(); + Console.WriteLine($"Speed right now is {hydroplane.GetSpeed}"); hydroplane.Braking(); - hydroplane.TakeSpeed(); + Console.WriteLine($"Speed right now is {hydroplane.GetSpeed}"); } } } From 1c8851f20cb0c7d8231e8425b5491ca1ae4c6f46 Mon Sep 17 00:00:00 2001 From: Gv1don Date: Sat, 24 Sep 2022 18:02:04 +0300 Subject: [PATCH 3/9] Add correct solution for task --- CourseApp.Tests/HydroplaneMethodsTests.cs | 60 -------------- CourseApp.Tests/PlaneMethodsTests.cs | 65 +++++++++++++++ CourseApp/Class/Hydroplane.cs | 91 --------------------- CourseApp/Class/ILanding.cs | 13 ++- CourseApp/Class/ITakeoff.cs | 13 ++- CourseApp/Class/Plane.cs | 98 ++++++++++++++++++++--- CourseApp/Class/Program.cs | 17 ++-- CourseApp/Class/Transport.cs | 43 ++++++++++ 8 files changed, 212 insertions(+), 188 deletions(-) delete mode 100644 CourseApp.Tests/HydroplaneMethodsTests.cs create mode 100644 CourseApp.Tests/PlaneMethodsTests.cs delete mode 100644 CourseApp/Class/Hydroplane.cs create mode 100644 CourseApp/Class/Transport.cs diff --git a/CourseApp.Tests/HydroplaneMethodsTests.cs b/CourseApp.Tests/HydroplaneMethodsTests.cs deleted file mode 100644 index 41fe80e..0000000 --- a/CourseApp.Tests/HydroplaneMethodsTests.cs +++ /dev/null @@ -1,60 +0,0 @@ -namespace CourseApp.Tests -{ - using CourseApp.Class; - using Xunit; - - public class HydroplaneMethodsTests - { - [Fact] - public void SpeedUp_SetSampleDate_ReturnsCorrectSpeed() - { - Hydroplane hydroplane = new Hydroplane(80, 90, "DHC-2 Beaver", 255); - hydroplane.SpeedUp(); - - Assert.Equal(80, hydroplane.GetSpeed); - } - - [Fact] - public void SpeedDown_SetSampleDate_ReturnCorrectSpeed() - { - var hydroplane = new Hydroplane(80, 90, "DHC-2 Beaver", 255); - hydroplane.SpeedUp(); - hydroplane.SpeedDown(); - - Assert.Equal(80, hydroplane.GetSpeed); - } - - [Fact] - public void Braking_SetSampleDate_ReturnsCorrectSpeed() - { - var hydroplane = new Hydroplane(80, 90, "DHC-2 Beaver", 255); - hydroplane.SpeedUp(); - hydroplane.Braking(); - - Assert.Equal(0, hydroplane.GetSpeed); - } - - [Fact] - public void GetInfo_SetSampleData_ReturnCorrectInfo() - { - var hydroplane = new Hydroplane(80, 90, "DHC-2 Beaver", 255); - Assert.Equal( - @"Model: DHC-2 Beaver -Landing speed: 80km/h -Takeoff speed: 90km/h -Max speed: 255km/h", hydroplane.GetInfo()); - } - - [Fact] - public void CheckConstructor_SetSampleData_ReturnCorrectInfo() - { - Plane hydroplane = new Hydroplane(90, 110, "SameHydroplane", 300); - - Assert.Equal( - @"Model: SameHydroplane -Landing speed: 90km/h -Takeoff speed: 110km/h -Max speed: 300km/h", hydroplane.GetInfo()); - } - } -} diff --git a/CourseApp.Tests/PlaneMethodsTests.cs b/CourseApp.Tests/PlaneMethodsTests.cs new file mode 100644 index 0000000..2d6a931 --- /dev/null +++ b/CourseApp.Tests/PlaneMethodsTests.cs @@ -0,0 +1,65 @@ +namespace CourseApp.Tests +{ + using CourseApp.Class; + using Xunit; + + public class PlaneMethodsTests + { + [Fact] + public void GetInfo_SetSampleData_ReturnCorrectInfo() + { + var plane = new Plane("DHC-2 Beaver", 255, 450, 9.22, 2.74, 1361); + Assert.Equal( + @"Info for Plane DHC-2 Beaver +Weight: 1361kg +Length: 9.22m +Height: 2.74m +Power: 450 horsepower +Max Speed: 255km\h", plane.GetInfo()); + } + + [Fact] + public void CheckConstructor_SetSampleData_ReturnCorrectInfo() + { + var plane = new Plane(); + + Assert.Equal( + @"Info for Plane No_name +Weight: 0kg +Length: 0m +Height: 0m +Power: 0 horsepower +Max Speed: 0km\h", plane.GetInfo()); + } + + [Fact] + public void Movement_CallMovementMethod_CorrectMoveInfo() + { + var plane = new Plane(); + plane.Direction = "Ivanovo"; + Assert.Equal("No_name is moving at a speed 0km/h in the Ivanovo", plane.Movement()); + } + + [Fact] + public void Landing_CallLandingMethod_CorrectLandingInfo() + { + var plane = new Plane(); + Assert.Equal("No_name going to land", plane.Landing()); + } + + [Fact] + public void Takeoff_CallLandingMethod_CorrectTakeoffInfo() + { + var plane = new Plane(); + Assert.Equal("No_name taking off", plane.Takeoff()); + } + + [Fact] + public void CustomException_WrongData_CorrectExceptionMessage() + { + var plane = new Plane(); + plane.Speed = 300; + Assert.Equal("Error: speed is too large or too little for No_name", plane.Exc); + } + } +} diff --git a/CourseApp/Class/Hydroplane.cs b/CourseApp/Class/Hydroplane.cs deleted file mode 100644 index 03668a0..0000000 --- a/CourseApp/Class/Hydroplane.cs +++ /dev/null @@ -1,91 +0,0 @@ -namespace CourseApp.Class -{ - using System; - - public class Hydroplane : Plane, ILanding, ITakeoff - { - private int landingSpeed; - private string info; - private int takeoffSpeed; - private string model; - private int maxSpeed; - private int speed; - private string direction; - - public Hydroplane(int landingSpeed, int takeoffSpeed, string model, int maxSpeed) - { - this.landingSpeed = landingSpeed; - this.takeoffSpeed = takeoffSpeed; - this.model = model; - this.maxSpeed = maxSpeed; - } - - public override int GetSpeed - { - get - { - return speed; - } - - set - { - speed = value; - } - } - - public string Direction - { - get - { - return direction; - } - - set - { - direction = value; - } - } - - public override string GetInfo() - { - info = @$"Model: {model} -Landing speed: {landingSpeed}km/h -Takeoff speed: {takeoffSpeed}km/h -Max speed: {maxSpeed}km/h"; - Console.WriteLine(info); - return info; - } - - public override void Movement() - { - Console.Write("Enter the direction of the plane: "); - Direction = Console.ReadLine(); - Console.WriteLine($"The plane is moving at a speed {GetSpeed}km/h in the direction: {Direction}"); - } - - public void SpeedDown() - { - GetSpeed = landingSpeed; - Console.WriteLine("The plane is landing"); - } - - public void Braking() - { - GetSpeed = 0; - Console.WriteLine("The plane has landed"); - } - - public void SpeedUp() - { - if (GetSpeed == 0) - { - Console.WriteLine("The Plane is taking off"); - speed = landingSpeed; - } - else - { - speed = maxSpeed; - } - } - } -} diff --git a/CourseApp/Class/ILanding.cs b/CourseApp/Class/ILanding.cs index f84e142..f771532 100644 --- a/CourseApp/Class/ILanding.cs +++ b/CourseApp/Class/ILanding.cs @@ -1,9 +1,6 @@ -namespace CourseApp.Class -{ - public interface ILanding - { - void SpeedDown(); +namespace CourseApp.Class; - void Braking(); - } -} +public interface ILanding +{ + public string Landing(); +} \ No newline at end of file diff --git a/CourseApp/Class/ITakeoff.cs b/CourseApp/Class/ITakeoff.cs index 9cabb6c..0aa5855 100644 --- a/CourseApp/Class/ITakeoff.cs +++ b/CourseApp/Class/ITakeoff.cs @@ -1,9 +1,6 @@ -namespace CourseApp.Class -{ - public interface ITakeoff - { - string Direction { get; set; } +namespace CourseApp.Class; - void SpeedUp(); - } -} +public interface ITakeoff +{ + public string Takeoff(); +} \ No newline at end of file diff --git a/CourseApp/Class/Plane.cs b/CourseApp/Class/Plane.cs index 8157c6f..dc171dc 100644 --- a/CourseApp/Class/Plane.cs +++ b/CourseApp/Class/Plane.cs @@ -1,26 +1,100 @@ -namespace CourseApp +namespace CourseApp.Class { - public abstract class Plane + using System; + + public class Plane : Transport, ILanding { - private int length; - private int weight; - private int power; + private string model; + private int speed; + private string direction; public Plane() + : this("No_name", 0, 0, 0, 0, 0) + { + } + + public Plane(string model, int maxSpeed, int power, double length, double height, int weight) + : base(length, height, weight, power, maxSpeed) + { + this.model = model; + direction = "some direction"; + } + + public string Exc { get; set; } + + public int Speed + { + get + { + return speed; + } + + set + { + try + { + if (value > MaxSpeed || value < 0) + { + throw new Exception($"speed is too large or too little for {model}"); + } + else + { + speed = value; + } + } + catch (Exception e) + { + Exc = $"Error: {e.Message}"; + Console.WriteLine(Exc); + } + } + } + + public string Direction + { + get + { + return direction; + } + + set + { + direction = value; + } + } + + public string Takeoff() { + string takeoff = $"{model} taking off"; + Console.WriteLine(takeoff); + return takeoff; } - public Plane(int length, int weight, int power) + public string Landing() { - this.length = length; - this.weight = weight; - this.power = power; + string landing = $"{model} going to land"; + Console.WriteLine(landing); + return landing; } - public abstract int GetSpeed { get; set; } + public override string GetInfo() + { + string info = @$"Info for Plane {model} +Weight: {Weight}kg +Length: {Length}m +Height: {Height}m +Power: {Power} horsepower +Max Speed: {MaxSpeed}km\h"; - public abstract void Movement(); + Console.WriteLine(info); + return info; + } - public abstract string GetInfo(); + public override string Movement() + { + string move = $"{model} is moving at a speed {Speed}km/h in the {Direction}"; + Console.WriteLine(move); + return move; + } } } diff --git a/CourseApp/Class/Program.cs b/CourseApp/Class/Program.cs index ffc2c41..b16af0a 100644 --- a/CourseApp/Class/Program.cs +++ b/CourseApp/Class/Program.cs @@ -6,16 +6,15 @@ public class Program { public static void Main() { - Hydroplane hydroplane = new Hydroplane(80, 90, "DHC-2 Beaver", 255); + var plane = new Plane("DHC-2 Beaver", 255, 450, 9.22, 2.74, 1361); - hydroplane.GetInfo(); - hydroplane.SpeedUp(); - hydroplane.SpeedUp(); - hydroplane.Movement(); - hydroplane.SpeedDown(); - Console.WriteLine($"Speed right now is {hydroplane.GetSpeed}"); - hydroplane.Braking(); - Console.WriteLine($"Speed right now is {hydroplane.GetSpeed}"); + plane.GetInfo(); + plane.Takeoff(); + plane.Speed = 250; + plane.Direction = "Belarus"; + plane.Movement(); + plane.Landing(); + plane.Speed = 300; } } } diff --git a/CourseApp/Class/Transport.cs b/CourseApp/Class/Transport.cs new file mode 100644 index 0000000..3a7bb6e --- /dev/null +++ b/CourseApp/Class/Transport.cs @@ -0,0 +1,43 @@ +namespace CourseApp +{ + public abstract class Transport + { + public Transport() + : this(0, 0, 0, 0, 0) + { + } + + public Transport(double length, double height, int weight, int power, int maxSpeed) + { + Length = length; + Weight = weight; + Power = power; + Height = height; + MaxSpeed = maxSpeed; + } + + public int Power { get; } + + public double Length { get; } + + public int Weight { get; } + + public double Height { get; } + + public int MaxSpeed { get; } + + public abstract string Movement(); + + public virtual string GetInfo() + { + string info = @$"Info for transport +Weight: {Weight}kg +Length: {Length}m +Height: {Height}m +Power: {Power}horsepower +Max Speed: {MaxSpeed}km\h"; + + return info; + } + } +} From 6eba2a3c738109e5e938a89b2bdf83e07b8046f9 Mon Sep 17 00:00:00 2001 From: Gv1don Date: Mon, 7 Nov 2022 11:59:52 +0300 Subject: [PATCH 4/9] Poymorphism task --- CourseApp.Tests/AutoMethodsTests.cs | 49 +++++++++++++++ CourseApp.Tests/PlaneMethodsTests.cs | 38 ++++++------ CourseApp/Class/ActionTypes.cs | 33 ++++++++++ CourseApp/Class/Auto.cs | 38 ++++++++++++ CourseApp/Class/ILanding.cs | 2 +- CourseApp/Class/ITakeoff.cs | 2 +- CourseApp/Class/Plane.cs | 91 ++++++---------------------- CourseApp/Class/Program.cs | 39 +++++++++--- CourseApp/Class/Transport.cs | 58 ++++++++++++++---- CourseApp/CourseApp.csproj | 4 -- CourseApp/stylecop.json | 12 ++++ 11 files changed, 246 insertions(+), 120 deletions(-) create mode 100644 CourseApp.Tests/AutoMethodsTests.cs create mode 100644 CourseApp/Class/ActionTypes.cs create mode 100644 CourseApp/Class/Auto.cs create mode 100644 CourseApp/stylecop.json diff --git a/CourseApp.Tests/AutoMethodsTests.cs b/CourseApp.Tests/AutoMethodsTests.cs new file mode 100644 index 0000000..6b38c66 --- /dev/null +++ b/CourseApp.Tests/AutoMethodsTests.cs @@ -0,0 +1,49 @@ +namespace CourseApp.Tests +{ + using CourseApp.Class; + using Xunit; + + public class AutoMethodsTests + { + [Fact] + public void GetInfo_SetSampleData_ReturnCorrectInfo_Auto() + { + Transport auto = new Auto(4.9, 1.71, 2145, 333, 250, "Audi Q8"); + Assert.Equal( + @"Info for Audi Q8 +Weight: 2145 kg +Length: 4,9 m +Height: 1,71 m +Power: 333 horsepower +Max Speed: 250 km\h", auto.GetInfo()); + } + + [Fact] + public void CheckConstructor_SetSampleData_ReturnCorrectInfo_Auto() + { + Transport auto = new Auto(); + + Assert.Equal( + @"Info for Auto +Weight: 0 kg +Length: 0 m +Height: 0 m +Power: 0 horsepower +Max Speed: 0 km\h", auto.GetInfo()); + } + + [Fact] + public void Movement_CallMovementMethod_CorrectMove_Auto() + { + Transport auto = new Auto(); + Assert.Equal(ActionTypes.Move, auto.Movement()); + } + + [Fact] + public void ToString_CheckStringPresentation_CorrectPresentationOfInstance() + { + Transport auto = new Auto(4.9, 1.71, 2145, 333, 250, "Audi Q8"); + Assert.Equal("Auto Audi Q8", auto.ToString()); + } + } +} diff --git a/CourseApp.Tests/PlaneMethodsTests.cs b/CourseApp.Tests/PlaneMethodsTests.cs index 2d6a931..b3e3b66 100644 --- a/CourseApp.Tests/PlaneMethodsTests.cs +++ b/CourseApp.Tests/PlaneMethodsTests.cs @@ -8,58 +8,56 @@ public class PlaneMethodsTests [Fact] public void GetInfo_SetSampleData_ReturnCorrectInfo() { - var plane = new Plane("DHC-2 Beaver", 255, 450, 9.22, 2.74, 1361); + Transport plane = new Plane(9.22, 2.74, 1361, 450, 255, "DHC-2 Beaver"); Assert.Equal( - @"Info for Plane DHC-2 Beaver -Weight: 1361kg -Length: 9.22m -Height: 2.74m + @"Info for DHC-2 Beaver +Weight: 1361 kg +Length: 9,22 m +Height: 2,74 m Power: 450 horsepower -Max Speed: 255km\h", plane.GetInfo()); +Max Speed: 255 km\h", plane.GetInfo()); } [Fact] public void CheckConstructor_SetSampleData_ReturnCorrectInfo() { - var plane = new Plane(); + Transport plane = new Plane(); Assert.Equal( - @"Info for Plane No_name -Weight: 0kg -Length: 0m -Height: 0m + @"Info for Plane +Weight: 0 kg +Length: 0 m +Height: 0 m Power: 0 horsepower -Max Speed: 0km\h", plane.GetInfo()); +Max Speed: 0 km\h", plane.GetInfo()); } [Fact] public void Movement_CallMovementMethod_CorrectMoveInfo() { var plane = new Plane(); - plane.Direction = "Ivanovo"; - Assert.Equal("No_name is moving at a speed 0km/h in the Ivanovo", plane.Movement()); + Assert.Equal(ActionTypes.Move, plane.Movement()); } [Fact] public void Landing_CallLandingMethod_CorrectLandingInfo() { var plane = new Plane(); - Assert.Equal("No_name going to land", plane.Landing()); + Assert.Equal(ActionTypes.LAnding, plane.Landing()); } [Fact] public void Takeoff_CallLandingMethod_CorrectTakeoffInfo() { var plane = new Plane(); - Assert.Equal("No_name taking off", plane.Takeoff()); + Assert.Equal(ActionTypes.TAkeoff, plane.Takeoff()); } [Fact] - public void CustomException_WrongData_CorrectExceptionMessage() + public void ToString_CheckStringPresentation_CorrectPresentationOfInstance() { - var plane = new Plane(); - plane.Speed = 300; - Assert.Equal("Error: speed is too large or too little for No_name", plane.Exc); + Transport plane = new Plane(9.22, 2.74, 1361, 450, 255, "DHC-2 Beaver"); + Assert.Equal("Plane DHC-2 Beaver", plane.ToString()); } } } diff --git a/CourseApp/Class/ActionTypes.cs b/CourseApp/Class/ActionTypes.cs new file mode 100644 index 0000000..4106dc7 --- /dev/null +++ b/CourseApp/Class/ActionTypes.cs @@ -0,0 +1,33 @@ +namespace CourseApp.Class +{ + /// + /// Types of actions. + /// + public enum ActionTypes + { + /// + /// No type. + /// + Nothing, + + /// + /// Type for movement. + /// + Move, + + /// + /// Type for takeoff. + /// + TAkeoff, + + /// + /// Types for landing. + /// + LAnding, + + /// + /// Types for beep. + /// + BEep, + } +} diff --git a/CourseApp/Class/Auto.cs b/CourseApp/Class/Auto.cs new file mode 100644 index 0000000..1bb23de --- /dev/null +++ b/CourseApp/Class/Auto.cs @@ -0,0 +1,38 @@ +namespace CourseApp.Class +{ + using System; + + public class Auto : Transport + { + private ActionTypes actionType; + + public Auto() + : this(0, 0, 0, 0, 0, "Auto") + { + } + + public Auto(double length, double height, int weight, int power, int maxSpeed, string model) + : base(length, height, weight, power, maxSpeed, model) + { + } + + public override string ToString() + { + return $"Auto {Model}"; + } + + public override ActionTypes Movement() + { + actionType = ActionTypes.Move; + Console.WriteLine($"{Model} is moving at a speed {Speed}km/h"); + return actionType; + } + + public ActionTypes Beep() + { + actionType = ActionTypes.BEep; + Console.WriteLine("Beep"); + return actionType; + } + } +} diff --git a/CourseApp/Class/ILanding.cs b/CourseApp/Class/ILanding.cs index f771532..a4a54a1 100644 --- a/CourseApp/Class/ILanding.cs +++ b/CourseApp/Class/ILanding.cs @@ -2,5 +2,5 @@ namespace CourseApp.Class; public interface ILanding { - public string Landing(); + public ActionTypes Landing(); } \ No newline at end of file diff --git a/CourseApp/Class/ITakeoff.cs b/CourseApp/Class/ITakeoff.cs index 0aa5855..eae44f7 100644 --- a/CourseApp/Class/ITakeoff.cs +++ b/CourseApp/Class/ITakeoff.cs @@ -2,5 +2,5 @@ namespace CourseApp.Class; public interface ITakeoff { - public string Takeoff(); + public ActionTypes Takeoff(); } \ No newline at end of file diff --git a/CourseApp/Class/Plane.cs b/CourseApp/Class/Plane.cs index dc171dc..d98543d 100644 --- a/CourseApp/Class/Plane.cs +++ b/CourseApp/Class/Plane.cs @@ -4,97 +4,44 @@ public class Plane : Transport, ILanding { - private string model; - private int speed; - private string direction; + private ActionTypes actionType; public Plane() - : this("No_name", 0, 0, 0, 0, 0) + : this(0, 0, 0, 0, 0, "Plane") { } - public Plane(string model, int maxSpeed, int power, double length, double height, int weight) - : base(length, height, weight, power, maxSpeed) + public Plane(double length, double height, int weight, int power, int maxSpeed, string model) + : base(length, height, weight, power, maxSpeed, model) { - this.model = model; - direction = "some direction"; } - public string Exc { get; set; } + public string Direction { get; set; } - public int Speed + public override string ToString() { - get - { - return speed; - } - - set - { - try - { - if (value > MaxSpeed || value < 0) - { - throw new Exception($"speed is too large or too little for {model}"); - } - else - { - speed = value; - } - } - catch (Exception e) - { - Exc = $"Error: {e.Message}"; - Console.WriteLine(Exc); - } - } - } - - public string Direction - { - get - { - return direction; - } - - set - { - direction = value; - } + return $"Plane {Model}"; } - public string Takeoff() + public ActionTypes Takeoff() { - string takeoff = $"{model} taking off"; - Console.WriteLine(takeoff); - return takeoff; + actionType = ActionTypes.TAkeoff; + Console.WriteLine($"{Model} taking off"); + return actionType; } - public string Landing() + public ActionTypes Landing() { - string landing = $"{model} going to land"; - Console.WriteLine(landing); - return landing; - } - - public override string GetInfo() - { - string info = @$"Info for Plane {model} -Weight: {Weight}kg -Length: {Length}m -Height: {Height}m -Power: {Power} horsepower -Max Speed: {MaxSpeed}km\h"; - - Console.WriteLine(info); - return info; + actionType = ActionTypes.LAnding; + Console.WriteLine($"{Model} going to land"); + return actionType; } - public override string Movement() + public override ActionTypes Movement() { - string move = $"{model} is moving at a speed {Speed}km/h in the {Direction}"; - Console.WriteLine(move); - return move; + actionType = ActionTypes.Move; + Console.WriteLine($"{Model} is moving at a speed {Speed}km/h in the {Direction}"); + return actionType; } } } diff --git a/CourseApp/Class/Program.cs b/CourseApp/Class/Program.cs index b16af0a..cdd7b1a 100644 --- a/CourseApp/Class/Program.cs +++ b/CourseApp/Class/Program.cs @@ -1,20 +1,39 @@ namespace CourseApp.Class { - using System; - public class Program { public static void Main() { - var plane = new Plane("DHC-2 Beaver", 255, 450, 9.22, 2.74, 1361); + Transport plane = new Plane(9.22, 2.74, 1361, 450, 255, "DHC-2 Beaver"); + if (plane is Plane beaver) + { + System.Console.WriteLine(beaver.GetInfo()); + System.Console.WriteLine(beaver.ToString()); + beaver.Takeoff(); + beaver.Speed = 250; + beaver.Direction = "Belarus"; + beaver.Movement(); + beaver.Landing(); + beaver.Speed = 300; + System.Console.WriteLine(beaver.Speed); + } + + Transport auto = new Auto(); + if (auto is Auto audi) + { + } + + Transport[] transports = new Transport[2] { plane, auto }; + + if (transports[0] is Plane plane1) + { + plane1.Movement(); + } - plane.GetInfo(); - plane.Takeoff(); - plane.Speed = 250; - plane.Direction = "Belarus"; - plane.Movement(); - plane.Landing(); - plane.Speed = 300; + if (transports[1] is Auto auto1) + { + auto1.Movement(); + } } } } diff --git a/CourseApp/Class/Transport.cs b/CourseApp/Class/Transport.cs index 3a7bb6e..dc0768f 100644 --- a/CourseApp/Class/Transport.cs +++ b/CourseApp/Class/Transport.cs @@ -1,21 +1,29 @@ namespace CourseApp { + using System; + using CourseApp.Class; + public abstract class Transport { + private int _speed; + public Transport() - : this(0, 0, 0, 0, 0) + : this(0, 0, 0, 0, 0, "No_name") { } - public Transport(double length, double height, int weight, int power, int maxSpeed) + public Transport(double length, double height, int weight, int power, int maxSpeed, string model) { Length = length; Weight = weight; Power = power; Height = height; MaxSpeed = maxSpeed; + Model = model; } + public string Model { get; set; } + public int Power { get; } public double Length { get; } @@ -26,18 +34,44 @@ public Transport(double length, double height, int weight, int power, int maxSpe public int MaxSpeed { get; } - public abstract string Movement(); + public int Speed + { + get + { + return _speed; + } + + set + { + try + { + if (value > MaxSpeed || value < 0) + { + throw new Exception($"speed is too large or too little for {Model}"); + } + else + { + _speed = value; + } + } + catch (Exception e) + { + new ArgumentException($"Error: {e.Message}"); + Console.WriteLine($"Error: {e.Message}"); + } + } + } + + public abstract ActionTypes Movement(); - public virtual string GetInfo() + public string GetInfo() { - string info = @$"Info for transport -Weight: {Weight}kg -Length: {Length}m -Height: {Height}m -Power: {Power}horsepower -Max Speed: {MaxSpeed}km\h"; - - return info; + return @$"Info for {Model} +Weight: {Weight} kg +Length: {Length} m +Height: {Height} m +Power: {Power} horsepower +Max Speed: {MaxSpeed} km\h"; } } } diff --git a/CourseApp/CourseApp.csproj b/CourseApp/CourseApp.csproj index eb22147..ba2f49f 100644 --- a/CourseApp/CourseApp.csproj +++ b/CourseApp/CourseApp.csproj @@ -15,8 +15,4 @@ true - - - - diff --git a/CourseApp/stylecop.json b/CourseApp/stylecop.json new file mode 100644 index 0000000..4a96e8f --- /dev/null +++ b/CourseApp/stylecop.json @@ -0,0 +1,12 @@ +{ + "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json", + "settings": { + "documentationRules": { + "documentExposedElements": false, + "documentInterfaces": false, + "companyName": "Test Company", + "copyrightText": "This source code is Copyright © {companyName} and MAY NOT be copied, reproduced,\npublished, distributed or transmitted to or stored in any manner without prior\nwritten consent from {companyName} (www.yourcompany.com).", + "xmlHeader":false + } + } +} \ No newline at end of file From 2fcd3ded70dc3937e9377a98729770d41313ef0e Mon Sep 17 00:00:00 2001 From: Gv1don Date: Thu, 10 Nov 2022 19:24:25 +0300 Subject: [PATCH 5/9] Add enum with player types --- CourseApp/RPGSaga/PlayerTypes.cs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 CourseApp/RPGSaga/PlayerTypes.cs diff --git a/CourseApp/RPGSaga/PlayerTypes.cs b/CourseApp/RPGSaga/PlayerTypes.cs new file mode 100644 index 0000000..f6d6764 --- /dev/null +++ b/CourseApp/RPGSaga/PlayerTypes.cs @@ -0,0 +1,28 @@ +namespace CourseApp.Class +{ + /// + /// Types of actions. + /// + public enum PlayerTypes + { + /// + /// No type. + /// + NotTypes, + + /// + /// Type for Knight. + /// + Knight, + + /// + /// Type for Archer. + /// + Archer, + + /// + /// Types for Wizard. + /// + Wizard, + } +} From 735c3697602c70f70c294161731074088bfc5002 Mon Sep 17 00:00:00 2001 From: Gv1don Date: Thu, 10 Nov 2022 19:30:32 +0300 Subject: [PATCH 6/9] Rename element of the enum --- CourseApp/RPGSaga/PlayerTypes.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CourseApp/RPGSaga/PlayerTypes.cs b/CourseApp/RPGSaga/PlayerTypes.cs index f6d6764..11ccc8b 100644 --- a/CourseApp/RPGSaga/PlayerTypes.cs +++ b/CourseApp/RPGSaga/PlayerTypes.cs @@ -8,7 +8,7 @@ public enum PlayerTypes /// /// No type. /// - NotTypes, + NotType, /// /// Type for Knight. From 34fdb733da6457509fdc424dc246e968c8081df2 Mon Sep 17 00:00:00 2001 From: Gv1don Date: Thu, 10 Nov 2022 19:46:24 +0300 Subject: [PATCH 7/9] Create class for creating player --- CourseApp/RPGSaga/CreatePlayers.cs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 CourseApp/RPGSaga/CreatePlayers.cs diff --git a/CourseApp/RPGSaga/CreatePlayers.cs b/CourseApp/RPGSaga/CreatePlayers.cs new file mode 100644 index 0000000..feba7fc --- /dev/null +++ b/CourseApp/RPGSaga/CreatePlayers.cs @@ -0,0 +1,26 @@ +namespace CourseApp.RPGSaga +{ + using System; + using CourseApp.Class; + + public static class CreatePlayers + { + private static Random _random = new (); + + public static PlayerTypes GetHeroType() + { + var type = _random.Next(0, 3); + switch (type) + { + case 0: + return PlayerTypes.Knight; + case 1: + return PlayerTypes.Wizard; + case 2: + return PlayerTypes.Archer; + default: + return PlayerTypes.NotType; + } + } + } +} From 340763b392860addb326d1d31ff31859cf770821 Mon Sep 17 00:00:00 2001 From: Gv1don Date: Wed, 23 Nov 2022 18:03:09 +0300 Subject: [PATCH 8/9] Add RPGSaga --- .../CourseApp.RPGSagaTests.csproj | 18 +++ CourseApp.RPGSagaTests/RPGSagaTests.cs | 11 ++ CourseApp.Tests/AutoMethodsTests.cs | 2 +- CourseApp.Tests/PlaneMethodsTests.cs | 2 +- CourseApp/Class/ActionTypes.cs | 2 +- CourseApp/Class/Auto.cs | 2 +- CourseApp/Class/ILanding.cs | 2 +- CourseApp/Class/ITakeoff.cs | 2 +- CourseApp/Class/Plane.cs | 2 +- CourseApp/Class/Program.cs | 39 ------ CourseApp/Class/Transport.cs | 2 +- CourseApp/Game.cs | 124 ++++++++++++++++++ CourseApp/RPGSaga/Archer.cs | 41 ++++++ CourseApp/RPGSaga/CreatePlayers.cs | 2 +- CourseApp/RPGSaga/Knight.cs | 37 ++++++ CourseApp/RPGSaga/Logger.cs | 17 +++ CourseApp/RPGSaga/Player.cs | 35 +++++ CourseApp/RPGSaga/PlayerTypes.cs | 2 +- CourseApp/RPGSaga/Wizard.cs | 41 ++++++ 19 files changed, 334 insertions(+), 49 deletions(-) create mode 100644 CourseApp.RPGSagaTests/CourseApp.RPGSagaTests.csproj create mode 100644 CourseApp.RPGSagaTests/RPGSagaTests.cs delete mode 100644 CourseApp/Class/Program.cs create mode 100644 CourseApp/Game.cs create mode 100644 CourseApp/RPGSaga/Archer.cs create mode 100644 CourseApp/RPGSaga/Knight.cs create mode 100644 CourseApp/RPGSaga/Logger.cs create mode 100644 CourseApp/RPGSaga/Player.cs create mode 100644 CourseApp/RPGSaga/Wizard.cs diff --git a/CourseApp.RPGSagaTests/CourseApp.RPGSagaTests.csproj b/CourseApp.RPGSagaTests/CourseApp.RPGSagaTests.csproj new file mode 100644 index 0000000..7b01765 --- /dev/null +++ b/CourseApp.RPGSagaTests/CourseApp.RPGSagaTests.csproj @@ -0,0 +1,18 @@ + + + + net6.0 + enable + enable + + false + + + + + + + + + + diff --git a/CourseApp.RPGSagaTests/RPGSagaTests.cs b/CourseApp.RPGSagaTests/RPGSagaTests.cs new file mode 100644 index 0000000..4462fe2 --- /dev/null +++ b/CourseApp.RPGSagaTests/RPGSagaTests.cs @@ -0,0 +1,11 @@ +namespace CourseApp.RPGSagaTests +{ + [TestClass] + public class UnitTest1 + { + [TestMethod] + public void TestMethod1() + { + } + } +} \ No newline at end of file diff --git a/CourseApp.Tests/AutoMethodsTests.cs b/CourseApp.Tests/AutoMethodsTests.cs index 6b38c66..64bea8e 100644 --- a/CourseApp.Tests/AutoMethodsTests.cs +++ b/CourseApp.Tests/AutoMethodsTests.cs @@ -1,6 +1,6 @@ namespace CourseApp.Tests { - using CourseApp.Class; + using CourseApp.RPGSaga; using Xunit; public class AutoMethodsTests diff --git a/CourseApp.Tests/PlaneMethodsTests.cs b/CourseApp.Tests/PlaneMethodsTests.cs index b3e3b66..f7e6e81 100644 --- a/CourseApp.Tests/PlaneMethodsTests.cs +++ b/CourseApp.Tests/PlaneMethodsTests.cs @@ -1,6 +1,6 @@ namespace CourseApp.Tests { - using CourseApp.Class; + using CourseApp.RPGSaga; using Xunit; public class PlaneMethodsTests diff --git a/CourseApp/Class/ActionTypes.cs b/CourseApp/Class/ActionTypes.cs index 4106dc7..b3c1295 100644 --- a/CourseApp/Class/ActionTypes.cs +++ b/CourseApp/Class/ActionTypes.cs @@ -1,4 +1,4 @@ -namespace CourseApp.Class +namespace CourseApp.RPGSaga { /// /// Types of actions. diff --git a/CourseApp/Class/Auto.cs b/CourseApp/Class/Auto.cs index 1bb23de..f14d499 100644 --- a/CourseApp/Class/Auto.cs +++ b/CourseApp/Class/Auto.cs @@ -1,4 +1,4 @@ -namespace CourseApp.Class +namespace CourseApp.RPGSaga { using System; diff --git a/CourseApp/Class/ILanding.cs b/CourseApp/Class/ILanding.cs index a4a54a1..5b97233 100644 --- a/CourseApp/Class/ILanding.cs +++ b/CourseApp/Class/ILanding.cs @@ -1,4 +1,4 @@ -namespace CourseApp.Class; +namespace CourseApp.RPGSaga; public interface ILanding { diff --git a/CourseApp/Class/ITakeoff.cs b/CourseApp/Class/ITakeoff.cs index eae44f7..7e5c896 100644 --- a/CourseApp/Class/ITakeoff.cs +++ b/CourseApp/Class/ITakeoff.cs @@ -1,4 +1,4 @@ -namespace CourseApp.Class; +namespace CourseApp.RPGSaga; public interface ITakeoff { diff --git a/CourseApp/Class/Plane.cs b/CourseApp/Class/Plane.cs index d98543d..904b092 100644 --- a/CourseApp/Class/Plane.cs +++ b/CourseApp/Class/Plane.cs @@ -1,4 +1,4 @@ -namespace CourseApp.Class +namespace CourseApp.RPGSaga { using System; diff --git a/CourseApp/Class/Program.cs b/CourseApp/Class/Program.cs deleted file mode 100644 index cdd7b1a..0000000 --- a/CourseApp/Class/Program.cs +++ /dev/null @@ -1,39 +0,0 @@ -namespace CourseApp.Class -{ - public class Program - { - public static void Main() - { - Transport plane = new Plane(9.22, 2.74, 1361, 450, 255, "DHC-2 Beaver"); - if (plane is Plane beaver) - { - System.Console.WriteLine(beaver.GetInfo()); - System.Console.WriteLine(beaver.ToString()); - beaver.Takeoff(); - beaver.Speed = 250; - beaver.Direction = "Belarus"; - beaver.Movement(); - beaver.Landing(); - beaver.Speed = 300; - System.Console.WriteLine(beaver.Speed); - } - - Transport auto = new Auto(); - if (auto is Auto audi) - { - } - - Transport[] transports = new Transport[2] { plane, auto }; - - if (transports[0] is Plane plane1) - { - plane1.Movement(); - } - - if (transports[1] is Auto auto1) - { - auto1.Movement(); - } - } - } -} diff --git a/CourseApp/Class/Transport.cs b/CourseApp/Class/Transport.cs index dc0768f..45bff70 100644 --- a/CourseApp/Class/Transport.cs +++ b/CourseApp/Class/Transport.cs @@ -1,7 +1,7 @@ namespace CourseApp { using System; - using CourseApp.Class; + using CourseApp.RPGSaga; public abstract class Transport { diff --git a/CourseApp/Game.cs b/CourseApp/Game.cs new file mode 100644 index 0000000..0fa95e2 --- /dev/null +++ b/CourseApp/Game.cs @@ -0,0 +1,124 @@ +namespace CourseApp.RPGSaga +{ + using System; + using System.Collections.Generic; + using CourseApp.RPGSaga; + + public class Game + { + private static string[] _names = new string[20] { "Евгений", "Евграф", "Евдоким", "Евсей", "Евстафий", "Авдей", "Авксентий", "Агапит", "Агафон", "Акакий", "Ждан", "Зиновий", "Иакинф", "Иван", "Игнатий", "Вавила", "Вадим", "Валентин", "Валерий", "Валерьян" }; + private static Random _random = new Random(); + private static bool death; + + public static void Main(string[] args) + { + List players = new List(); + List battle = new List(); + int battleCounter = 0; + int roundCounter = 0; + + Console.Write("Введите кол-во героев: "); + int numberOfPlayers = Convert.ToInt16(Console.ReadLine()); + Console.WriteLine(); + if (numberOfPlayers % 2.0 != 0) + { + Console.WriteLine("Количество героев нечетное!"); + return; + } + + for (int i = 0; i < numberOfPlayers; i++) + { + var randomName = _names[_random.Next(0, 20)]; + switch (CreatePlayers.GetHeroType()) + { + case PlayerTypes.NotType: + new Exception("Player has not type"); + break; + case PlayerTypes.Knight: + players.Add(new Knight(randomName)); + break; + case PlayerTypes.Archer: + players.Add(new Archer(randomName)); + break; + case PlayerTypes.Wizard: + players.Add(new Wizard(randomName)); + break; + } + } + + while (players.Count > 1) + { + battle.Add(players[_random.Next(0, players.Count)]); + players.Remove(battle[0]); + battle.Add(players[_random.Next(0, players.Count)]); + players.Remove(battle[1]); + + Logger.Space(); + + if (battleCounter % 2 == 0) + { + Logger.WriteLog($"Кон {roundCounter += 1}"); + } + + Logger.Space(); + Logger.WriteLog(battle[0].ToString() + " vs " + battle[1].ToString()); + + while (death == false) + { + if (_random.Next(0, 5) == 2) + { + battle[0].Ultimate(); + Logger.WriteLog(battle[0].ToString() + " ультует!"); + } + else + { + battle[1].GetDamage(battle[0].GiveDamage()); + Logger.WriteLog(battle[0].ToString() + $" наносит {battle[0].GiveDamage()} урона противнику: " + battle[1].ToString()); + } + + if (battle[1].Health < 1) + { + death = true; + players.Add(battle[0]); + Logger.WriteLog(battle[1].ToString() + " погибает"); + break; + } + + if (_random.Next(0, 10) == 2) + { + battle[1].Ultimate(); + Logger.WriteLog(battle[1].ToString() + " ультует!"); + } + else + { + battle[0].GetDamage(battle[1].GiveDamage()); + Logger.WriteLog(battle[1].ToString() + $" наносит {battle[1].GiveDamage()} урона противнику: " + battle[0].ToString()); + } + + if (battle[0].Health < 1) + { + death = true; + players.Add(battle[1]); + Logger.WriteLog(battle[0].ToString() + " погибает"); + break; + } + } + + death = false; + battle.Clear(); + battleCounter++; + } + + Logger.Space(); + Logger.WriteLog(players[0].ToString() + " побеждает! Здоровья погибшим)"); + Logger.Space(); + Logger.WriteLog(@" ___ ___ ______ ___ ___ + \ \ ____ / / |_ _| | \ | | + \ \ / \ / / | | | \| | + \ \/ /\ \/ / _| |_ | \ | + \____/ \____/ |______| |___|\_____| + +"); + } + } +} diff --git a/CourseApp/RPGSaga/Archer.cs b/CourseApp/RPGSaga/Archer.cs new file mode 100644 index 0000000..04e2db5 --- /dev/null +++ b/CourseApp/RPGSaga/Archer.cs @@ -0,0 +1,41 @@ +namespace CourseApp.RPGSaga +{ + public class Archer : Player + { + private bool ult = false; + + public Archer(string name) + { + Name = name; + } + + public override string ToString() + { + return $"(Лучник) {Name}"; + } + + public override void Ultimate() + { + if (ult == true) + { + GiveDamage(); + } + else + { + ult = true; + } + } + + public override int GiveDamage() + { + if (ult == false) + { + return Strength; + } + else + { + return Strength + 2; + } + } + } +} diff --git a/CourseApp/RPGSaga/CreatePlayers.cs b/CourseApp/RPGSaga/CreatePlayers.cs index feba7fc..9c220b3 100644 --- a/CourseApp/RPGSaga/CreatePlayers.cs +++ b/CourseApp/RPGSaga/CreatePlayers.cs @@ -1,7 +1,7 @@ namespace CourseApp.RPGSaga { using System; - using CourseApp.Class; + using CourseApp.RPGSaga; public static class CreatePlayers { diff --git a/CourseApp/RPGSaga/Knight.cs b/CourseApp/RPGSaga/Knight.cs new file mode 100644 index 0000000..8525834 --- /dev/null +++ b/CourseApp/RPGSaga/Knight.cs @@ -0,0 +1,37 @@ +namespace CourseApp.RPGSaga +{ + using System; + + public class Knight : Player + { + private bool ult = false; + + public Knight(string name) + { + Name = name; + } + + public override string ToString() + { + return $"(Рыцарь) {Name}"; + } + + public override void Ultimate() + { + ult = true; + } + + public override int GiveDamage() + { + if (ult == false) + { + return Strength; + } + else + { + ult = false; + return (int)Math.Round(Strength * 1.3); + } + } + } +} diff --git a/CourseApp/RPGSaga/Logger.cs b/CourseApp/RPGSaga/Logger.cs new file mode 100644 index 0000000..364d28e --- /dev/null +++ b/CourseApp/RPGSaga/Logger.cs @@ -0,0 +1,17 @@ +namespace CourseApp.RPGSaga +{ + using System; + + public static class Logger + { + public static void WriteLog(string message) + { + Console.WriteLine($"{DateTime.Now} : {message}"); + } + + public static void Space() + { + Console.WriteLine(); + } + } +} diff --git a/CourseApp/RPGSaga/Player.cs b/CourseApp/RPGSaga/Player.cs new file mode 100644 index 0000000..ca0a23d --- /dev/null +++ b/CourseApp/RPGSaga/Player.cs @@ -0,0 +1,35 @@ +namespace CourseApp.RPGSaga +{ + using System; + using CourseApp.RPGSaga; + + public abstract class Player + { + private Random randomValue = new Random(); + + public Player() + { + Health = randomValue.Next(40, 100); + Strength = randomValue.Next(20, 40); + } + + public PlayerTypes MyProperty { get; set; } + + public string Name { get; set; } + + public int Health { get; set; } + + public int Strength { get; set; } + + public virtual void Ultimate() + { + } + + public abstract int GiveDamage(); + + public virtual void GetDamage(int damage) + { + Health -= damage; + } + } +} diff --git a/CourseApp/RPGSaga/PlayerTypes.cs b/CourseApp/RPGSaga/PlayerTypes.cs index 11ccc8b..cebcfa6 100644 --- a/CourseApp/RPGSaga/PlayerTypes.cs +++ b/CourseApp/RPGSaga/PlayerTypes.cs @@ -1,4 +1,4 @@ -namespace CourseApp.Class +namespace CourseApp.RPGSaga { /// /// Types of actions. diff --git a/CourseApp/RPGSaga/Wizard.cs b/CourseApp/RPGSaga/Wizard.cs new file mode 100644 index 0000000..6067548 --- /dev/null +++ b/CourseApp/RPGSaga/Wizard.cs @@ -0,0 +1,41 @@ +namespace CourseApp.RPGSaga +{ + public class Wizard : Player + { + public Wizard(string name) + { + Name = name; + Invisible = false; + } + + public bool Invisible { get; set; } + + public override string ToString() + { + return $"(Маг) {Name}"; + } + + public override void Ultimate() + { + Invisible = true; + } + + public override int GiveDamage() + { + return Strength; + } + + public override void GetDamage(int damage) + { + if (Invisible == false) + { + Health -= damage; + } + else + { + Invisible = false; + Logger.WriteLog($"{ToString()} использует неуязвимость!"); + } + } + } +} From c70347617ec78a3251af811d198c607c9fb616eb Mon Sep 17 00:00:00 2001 From: gv1don Date: Fri, 13 Jan 2023 16:47:05 +0300 Subject: [PATCH 9/9] Corrections on comments --- CourseApp/Class/Plane.cs | 2 +- CourseApp/Game.cs | 69 +++++++++++++++++++----------- CourseApp/RPGSaga/CreatePlayers.cs | 1 - CourseApp/RPGSaga/Player.cs | 1 - 4 files changed, 44 insertions(+), 29 deletions(-) diff --git a/CourseApp/Class/Plane.cs b/CourseApp/Class/Plane.cs index 904b092..cdd8668 100644 --- a/CourseApp/Class/Plane.cs +++ b/CourseApp/Class/Plane.cs @@ -2,7 +2,7 @@ { using System; - public class Plane : Transport, ILanding + public class Plane : Transport, ILanding, ITakeoff { private ActionTypes actionType; diff --git a/CourseApp/Game.cs b/CourseApp/Game.cs index 0fa95e2..3988cbe 100644 --- a/CourseApp/Game.cs +++ b/CourseApp/Game.cs @@ -2,7 +2,6 @@ { using System; using System.Collections.Generic; - using CourseApp.RPGSaga; public class Game { @@ -12,39 +11,18 @@ public class Game public static void Main(string[] args) { - List players = new List(); List battle = new List(); int battleCounter = 0; int roundCounter = 0; - Console.Write("Введите кол-во героев: "); - int numberOfPlayers = Convert.ToInt16(Console.ReadLine()); - Console.WriteLine(); - if (numberOfPlayers % 2.0 != 0) + var numberOfPlayers = GameInitial(); + + if (numberOfPlayers == 0) { - Console.WriteLine("Количество героев нечетное!"); return; } - for (int i = 0; i < numberOfPlayers; i++) - { - var randomName = _names[_random.Next(0, 20)]; - switch (CreatePlayers.GetHeroType()) - { - case PlayerTypes.NotType: - new Exception("Player has not type"); - break; - case PlayerTypes.Knight: - players.Add(new Knight(randomName)); - break; - case PlayerTypes.Archer: - players.Add(new Archer(randomName)); - break; - case PlayerTypes.Wizard: - players.Add(new Wizard(randomName)); - break; - } - } + var players = HeroFactory(numberOfPlayers, new List { }); while (players.Count > 1) { @@ -120,5 +98,44 @@ public static void Main(string[] args) "); } + + public static int GameInitial() + { + Console.Write("Введите кол-во героев: "); + int numberOfPlayers = Convert.ToInt16(Console.ReadLine()); + Console.WriteLine(); + if (numberOfPlayers % 2.0 != 0 || numberOfPlayers <= 0) + { + Console.WriteLine("Количество героев нечетное!"); + return 0; + } + + return numberOfPlayers; + } + + public static List HeroFactory(int numberOfPlayers, List players) + { + for (int i = 0; i < numberOfPlayers; i++) + { + var randomName = _names[_random.Next(0, 20)]; + switch (CreatePlayers.GetHeroType()) + { + case PlayerTypes.NotType: + new Exception("Player has not type"); + break; + case PlayerTypes.Knight: + players.Add(new Knight(randomName)); + break; + case PlayerTypes.Archer: + players.Add(new Archer(randomName)); + break; + case PlayerTypes.Wizard: + players.Add(new Wizard(randomName)); + break; + } + } + + return players; + } } } diff --git a/CourseApp/RPGSaga/CreatePlayers.cs b/CourseApp/RPGSaga/CreatePlayers.cs index 9c220b3..5d1038f 100644 --- a/CourseApp/RPGSaga/CreatePlayers.cs +++ b/CourseApp/RPGSaga/CreatePlayers.cs @@ -1,7 +1,6 @@ namespace CourseApp.RPGSaga { using System; - using CourseApp.RPGSaga; public static class CreatePlayers { diff --git a/CourseApp/RPGSaga/Player.cs b/CourseApp/RPGSaga/Player.cs index ca0a23d..8ce6463 100644 --- a/CourseApp/RPGSaga/Player.cs +++ b/CourseApp/RPGSaga/Player.cs @@ -1,7 +1,6 @@ namespace CourseApp.RPGSaga { using System; - using CourseApp.RPGSaga; public abstract class Player {