-
Notifications
You must be signed in to change notification settings - Fork 5
/
Vitaliy_S_HW8.cs
135 lines (132 loc) · 4.03 KB
/
Vitaliy_S_HW8.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
namespace Vitaliy_S_HW_8
{
abstract class Shape : IComparable<Shape>
{
private string name;
public string Name { get { return name; } set { name = value; } }
public Shape(string name)
{
this.name = name;
}
abstract public double Area();
abstract public double Perimetr();
public int CompareTo(Shape? other)
{
if (this.Area() > other.Area())
{
return 1;
}
else if (this.Area() < other.Area())
{
return -1;
}
else
return 0;
}
}
class Circle : Shape
{
private double radius;
public Circle(string name) : base(name)
{
A1:
try
{
Console.WriteLine($"Type radius of the {name}");
radius = Convert.ToDouble(Console.ReadLine());
if (radius <= 0) throw new ApplicationException("rudius can't be negative or equal to zero");
}
catch (ApplicationException ex)
{
Console.WriteLine(ex.Message);
goto A1;
}
}
public override double Area()
{
return Math.PI * (radius * radius);
}
public override double Perimetr()
{
return 2 * Math.PI * radius;
}
}
class Square : Shape
{
private double side;
public Square(string name) : base(name)
{
A2:
try
{
Console.WriteLine($"Type side of the {name}");
side = Convert.ToDouble(Console.ReadLine());
if (side <= 0) throw new ApplicationException("side can't be negative or equal to zero");
}
catch (ApplicationException ex)
{
Console.WriteLine(ex.Message);
goto A2;
}
}
public override double Area()
{
return 2 * side;
}
public override double Perimetr()
{
return 4 * side;
}
}
class Program
{
static void Main(string[] args)
{
List<Shape> shapes = new List<Shape>()
{
new Circle("Circle N_1"),
new Circle("Circle N_2"),
new Circle("Circle N_3"),
new Circle("Circle N_4"),
new Circle("Circle N_5"),
new Square("Square N_1"),
new Square("Square N_2"),
new Square("Square N_3"),
new Square("Square N_4"),
new Square("Square N_5")
};
Console.WriteLine();
foreach (var item in shapes)
{
if (item.GetType() == typeof(Circle))
{
Console.WriteLine($" Area of {item.Name} = {item.Area():F2}");
Console.WriteLine($"Perimetr of {item.Name} = {item.Perimetr():F2}\n");
}
if (item.GetType() == typeof(Square))
{
Console.WriteLine($" Area of {item.Name} = {item.Area():F2}");
Console.WriteLine($"Perimetr of {item.Name} = {item.Perimetr():F2}\n");
}
}
double maxper = shapes[0].Perimetr();
string name = shapes[0].Name;
for (int i = 0; i < shapes.Count; i++)
{
if (shapes[i].Perimetr() > maxper)
{
maxper = shapes[i].Perimetr();
name = shapes[i].Name;
}
}
Console.WriteLine("\nMax perimetr");
Console.WriteLine($"{name} is the shape with the largest perimetr {maxper}");
shapes.Sort();
Console.WriteLine();
foreach (var item in shapes)
{
Console.WriteLine($"Shape: {item.Name}, area: {item.Area():F2}");
}
}
}
}