· 6 years ago · Sep 27, 2019, 05:00 PM
1using System;
2using System.Collections.Generic;
3
4namespace ParkingApp
5{
6 class ParkingManager
7 {
8 /* BASIC PART */
9 private List <ParkingSession> ActiveParkingSessions = new List<ParkingSession>();
10
11 private List<ParkingSession> CompletedParkingSessions = new List<ParkingSession>();
12
13 public const int ParkingCapacty = 5;
14
15 public int FreeLeavePeriod = 15;
16
17 /*private List<int> AvaliableTicketNumber = new List<int>(new int[ParkingCapacty]); */
18
19 Stack<int> AvaliableTicketNumber = new Stack<int>();
20
21 public ParkingManager()
22 {
23 for (int i = 1; i <= ParkingCapacty; i++)
24 {
25 AvaliableTicketNumber.Push(i);
26 }
27 }
28
29
30 private string TariffTable = "" +
31 "Minutes | Rate(RUB)\n" +
32 "15 | 0" +
33 "60 | 50" +
34 "120 | 100" +
35 "180 | 140" +
36 "240 | 180" +
37 "600+ | 350";
38
39 public ParkingSession EnterParking(string carPlateNumber)
40 {
41 /* Check that there is a free parking place (by comparing the parking capacity
42 * with the number of active parking sessions). If there are no free places, return null
43 *
44 * Also check that there are no existing active sessions with the same car plate number,
45 * if such session exists, also return null
46 *
47 * Otherwise:
48 * Create a new Parking session, fill the following properties:
49 * EntryDt = current date time
50 * CarPlateNumber = carPlateNumber (from parameter)
51 * TicketNumber = unused parking ticket number = generate this programmatically
52 *
53 * Add the newly created session to the list of active sessions
54 *
55 * Advanced task:
56 * Link the new parking session to an existing user by car plate number (if such user exists)
57 */
58 if (ActiveParkingSessions.Count < FreeLeavePeriod)
59 {
60 return null;
61 }
62
63 foreach (ParkingSession session in ActiveParkingSessions)
64 {
65 if (session.CarPlateNumber == carPlateNumber)
66 return null;
67 }
68
69 int ticketNumber = AvaliableTicketNumber.Pop();
70
71 ParkingSession newSession = new ParkingSession(carPlateNumber, ticketNumber);
72
73 ActiveParkingSessions.Add(newSession);
74
75 return newSession;
76
77 }
78
79 public bool TryLeaveParkingWithTicket(int ticketNumber, out ParkingSession session)
80 {
81 /*
82 * Check that the car leaves parking within the free leave period
83 * from the PaymentDt (or if there was no payment made, from the EntryDt)
84 * 1. If yes:
85 * 1.1 Complete the parking session by setting the ExitDt property
86 * 1.2 Move the session from the list of active sessions to the list of past sessions *
87 * 1.3 return true and the completed parking session object in the out parameter
88 *
89 * 2. Otherwise, return false, session = null
90 */
91 int sessionIndex;
92 TimeSpan? diff;
93 foreach (ParkingSession _session in ActiveParkingSessions)
94 {
95 if (_session.TicketNumber == ticketNumber)
96 {
97 session = _session;
98 sessionIndex = ActiveParkingSessions.IndexOf(session);
99
100 if (session.PaymentDt != null)
101 {
102 diff = DateTime.Now - session.PaymentDt;
103 }
104 else
105 {
106 diff = DateTime.Now - session.EntryDt;
107 }
108 if (diff.Value.TotalMinutes >= 15)
109 {
110 session.ExitDt = DateTime.Now;
111 ActiveParkingSessions.RemoveAt(sessionIndex);
112 CompletedParkingSessions.Add(session);
113 AvaliableTicketNumber.Push(ticketNumber);
114 return true;
115 }
116 else
117 {
118 session = null;
119 return false;
120 }
121 }
122 }
123 session = null;
124 return false;
125 }
126
127 public decimal GetRemainingCost(int ticketNumber)
128 {
129 /* Return the amount to be paid for the parking
130 * If a payment had already been made but additional charge was then given
131 * because of a late exit, this method should return the amount
132 * that is yet to be paid (not the total charge)
133 */
134 ParkingSession currentSession;
135 TimeSpan? diff = null;
136 int diffInt;
137 foreach (ParkingSession session in ActiveParkingSessions)
138 {
139 if (session.TicketNumber == ticketNumber)
140 {
141 currentSession = session;
142 break;
143 }
144
145 }
146 if (TryLeaveParkingWithTicket(ticketNumber, out currentSession))
147 {
148 return 0;
149 }
150 else if (currentSession.PaymentDt != null){
151 diff = DateTime.Now - currentSession.PaymentDt;
152 diffInt = diff.Value.Minutes;
153 }
154 else
155 {
156 diff = DateTime.Now - currentSession.EntryDt;
157 diffInt = diff.Value.Minutes;
158 }
159
160 if (diffInt < 15)
161 {
162 return 0;
163 }
164 else if (diffInt < 60)
165 {
166 return 50;
167 }
168 else if (diffInt < 120)
169 {
170 return 100;
171 }
172 else if (diffInt < 180)
173 {
174 return 140;
175 }
176 else if (diffInt < 240)
177 {
178 return 180;
179 }
180 else
181 {
182 return 350;
183 }
184 }
185
186 public void PayForParking(int ticketNumber, decimal amount)
187 {
188 /*
189 * Save the payment details in the corresponding parking session
190 * Set PaymentDt to current date and time
191 *
192 * For simplicity we won't make any additional validation here and always
193 * assume that the parking charge is paid in full
194 */
195
196 }
197
198 /* ADDITIONAL TASK 2 */
199 public bool TryLeaveParkingByCarPlateNumber(string carPlateNumber, out ParkingSession session)
200 {
201 /* There are 3 scenarios for this method:
202
203 1. The user has not made any payments but leaves the parking within the free leave period
204 from EntryDt:
205 1.1 Complete the parking session by setting the ExitDt property
206 1.2 Move the session from the list of active sessions to the list of past sessions *
207 1.3 return true and the completed parking session object in the out parameter
208
209 2. The user has already paid for the parking session (session.PaymentDt != null):
210 Check that the current time is within the free leave period from session.PaymentDt
211 2.1. If yes, complete the session in the same way as in the previous scenario
212 2.2. If no, return false, session = null
213
214 3. The user has not paid for the parking session:
215 3a) If the session has a connected user (see advanced task from the EnterParking method):
216 ExitDt = PaymentDt = current date time;
217 TotalPayment according to the tariff table:
218
219 IMPORTANT: before calculating the parking charge, subtract FreeLeavePeriod
220 from the total number of minutes passed since entry
221 i.e. if the registered visitor enters the parking at 10:05
222 and attempts to leave at 10:25, no charge should be made, otherwise it would be unfair
223 to loyal customers, because an ordinary printed ticket could be inserted in the payment
224 kiosk at 10:15 (no charge) and another 15 free minutes would be given (up to 10:30)
225
226 return the completed session in the out parameter and true in the main return value
227
228 3b) If there is no connected user, set session = null, return false (the visitor
229 has to insert the parking ticket and pay at the kiosk)
230 */
231 throw new NotImplementedException();
232 }
233 }
234}