-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGarage.cs
169 lines (155 loc) · 5.33 KB
/
Garage.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
namespace Ex03.GarageLogic
{
public class Garage
{
// $G$ CSS-002 (-3) Bad members variable name (should be in the form of m_PascalCase).
private Dictionary<string, GarageItem> garageItems;
public Garage()
{
garageItems = new Dictionary<string, GarageItem>();
}
public bool AddVehicle(GarageItem i_ItemToAdd)
{
bool res = false;
if (!garageItems.ContainsKey(i_ItemToAdd.Vehicle.RegNumber))
{
garageItems.Add(i_ItemToAdd.Vehicle.RegNumber, i_ItemToAdd);
res = true;
}
else { throw new FormatException("Error: Reg Number already in system , adding failed"); }
return res;
}
public string ShowAllReg()
{
string output = "";
foreach (var Items in garageItems)
{
output += Items.Value.Vehicle.RegNumber.ToString()+Environment.NewLine;
}
return output;
}
public string ShowAllRegByState(eVehicleState i_State) // need to sort by state?
{
string output = "";
foreach (var Item in garageItems)
{
if (Item.Value.State == i_State)
{
output += Item.Value.Vehicle.RegNumber.ToString() + Environment.NewLine;
}
}
return output;
}
public GarageItem ChangeStateOfVehicle(string i_RegNumber, eVehicleState i_State)
{
if (garageItems.ContainsKey(i_RegNumber))
{
garageItems[i_RegNumber].State = i_State;
}
return garageItems[i_RegNumber];
}
public GarageItem FillAirToMax(string i_RegNumber)
{
if (garageItems.ContainsKey(i_RegNumber))
{
foreach (var wheel in garageItems[i_RegNumber].Vehicle.Wheels)
{
wheel.FillAirToMax();
}
}
return garageItems[i_RegNumber];
}
public GarageItem FillAir(string i_RegNumber, float i_Amount)
{
if (garageItems.ContainsKey(i_RegNumber))
{
foreach (var wheel in garageItems[i_RegNumber].Vehicle.Wheels)
{
try
{
wheel.FillAir(i_Amount);
}
catch(ValueOutOfRangeException ex)
{
throw ex;
}
}
}
return garageItems[i_RegNumber];
}
public void FillFuel(string i_RegNumber, eFuelType i_FuelType, float i_AmountToFill)
{
if (garageItems.ContainsKey(i_RegNumber))
{
if (garageItems[i_RegNumber].Vehicle.GetEngine is FuelEngine)
{
FuelEngine ptr = garageItems[i_RegNumber].Vehicle.GetEngine as FuelEngine;
if (ptr.FuelType == i_FuelType)
{
try
{
ptr.Fuel += i_AmountToFill;
}
catch (ValueOutOfRangeException ex)
{
throw ex;
}
}
else
{
throw new FormatException("incorrect fuel type ");
}
}
else
{
throw new FormatException("Vehicle is not fuel type ! ");
}
}
else
{
throw new FormatException("Vehicle is not found ");
}
}
public void ChargeEnergy(string i_RegNumber, float i_AmountToFill) // need to check mins or hour issue
{
if (garageItems.ContainsKey(i_RegNumber))
{
if (garageItems[i_RegNumber].Vehicle.GetEngine is ElectricEngine)
{
ElectricEngine ptr = garageItems[i_RegNumber].Vehicle.GetEngine as ElectricEngine;
try
{
ptr.Battery += i_AmountToFill;
}
catch (ValueOutOfRangeException ex)
{
throw ex;
}
}
else
{
throw new FormatException("Vehicle is not Electric type ! ");
}
}
else
{
throw new FormatException("Vehicle is not found ");
}
}
public string ShowVehicleDetails(string i_RegNumber) // no need exceptions
{
string output ="";
if (garageItems.ContainsKey(i_RegNumber))
{
output = garageItems[i_RegNumber].ToString();
}
else
{
output += "Vehicle isnt found ";
}
return output;
}
}
}