-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParking.cs
134 lines (113 loc) · 4.29 KB
/
Parking.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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.IO;
namespace ParkingEmulatorLogic
{
public class Parking
{
#region Parking Creation
private static readonly Lazy<Parking> lazyInstance = new Lazy<Parking>(() => new Parking());
public static Parking Instance { get { return lazyInstance.Value; } }
private static Timer chargingTimer;
private static Timer transactionLoggingTimer;
private Parking()
{
var auto = new AutoResetEvent(false);
TimerCallback chargingCallback = new TimerCallback(ChargeFee);
TimerCallback transactionLoggingCallback = new TimerCallback(LogTransactions);
chargingTimer = new Timer(chargingCallback, auto, Settings.Timeout, Settings.Timeout);
transactionLoggingTimer = new Timer(transactionLoggingCallback, auto, Settings.LoggingInterval, Settings.LoggingInterval);
}
#endregion
#region Properties
private static readonly List<int> carIds = new List<int>();
private List<Transaction> transactions = new List<Transaction>();
private double lastMinuteProfit;
public List<Transaction> LastMinuteTransactions { get; set; } = new List<Transaction>();
public List<Car> Cars { get; set; } = new List<Car>();
public static List<int> CarsIds { get { return carIds; } }
public int FreeSpace { get { return Settings.ParkingSpace - Cars.Count; } }
public double PassiveBalance { get; set; }
public double ActiveBalance { get; set; }
public double CommonBalance { get { return PassiveBalance + ActiveBalance; } }
#endregion
#region Data Getting Methods
public void GetAllCars()
{
if (Cars.Count == 0)
{
Console.WriteLine("There is no cars on the parking\n");
}
else
{
Console.WriteLine("CarId\tCar Type\tBalance");
foreach (var car in Cars)
{
Console.WriteLine(car.Id + "\t" + car.CarType + "\t\t\t" + car.Balance.ToString("F"));
}
}
}
public static void GetLastMinuteProfit()
{
}
#endregion
public bool RemoveCarFromParking(int carId)
{
bool isDeleted;
string result = string.Empty;
var carDel = Cars.FirstOrDefault(car => car.Id == carId);
if (carDel == null)
{
return isDeleted = false;
}
else
{
Cars.Remove(carDel);
return isDeleted = true;
}
}
private void ChargeFee(object stateInfo)
{
foreach (var car in Cars)
{
double feeSize = Settings.PriceSet[car.CarType];
if (car.Balance < Settings.PriceSet[car.CarType])
{
feeSize += feeSize*Settings.Fine;
car.Balance -= feeSize;
ActiveBalance += feeSize;
}
else
{
car.Balance -= feeSize;
PassiveBalance += feeSize;
}
lastMinuteProfit += feeSize;
var transaction = new Transaction(car.Id, feeSize);
transactions.Add(transaction);
LastMinuteTransactions.Add(transaction);
}
}
public void CarBalanceRefilling(double fine, int carId)
{
var refillingCar = Cars.FirstOrDefault(car => car.Id == carId);
refillingCar.Balance += fine;
ActiveBalance -= fine;
PassiveBalance += fine;
}
private void LogTransactions(object stateInfo)
{
Transaction.AddToTransactionLog(LastMinuteTransactions);
lastMinuteProfit = 0;
LastMinuteTransactions.Clear();
}
public void CloseParking()
{
chargingTimer.Dispose();
transactionLoggingTimer.Dispose();
Transaction.AddToTransactionLog(LastMinuteTransactions);
}
}
}