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