/// <summary> /// Print all hisrory of tranactions. /// </summary> public static void ShowAllTranactions() { try { using (StreamReader sr = new StreamReader(FilePath, Encoding.Default)) { string line; while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } } } catch (Exception e) { Console.WriteLine(e.Message); } List <Transaction> trs = new List <Transaction>(Parking.GetInstance().transactions); foreach (Transaction t in trs.ToArray()) { Console.WriteLine(t); } }
/// <summary> /// Park vehicle and turn on meter. /// </summary> public void AddVehicle() { if (Settings.capacity > Parking.GetInstance().vehicles.Count) { this.ReadInfo(out int type, out double balance, out string licenseNumber); Vehicle v = this.CreateVehicle(type, balance, licenseNumber); v.IsParked = true; if (!VehicleViewer.IsParked(v.Id)) { Parking.GetInstance().vehicles.Add(v); this.tm = new TimerCallback(this.GetPaid); this.timer = new Timer(this.tm, v, 0, Settings.period); Console.WriteLine("Your vehicle is parked."); } else { Console.WriteLine("Your vehicle has already parked."); } } else { Console.WriteLine("Sorry, the ParkingLot is full."); } }
/// <summary> /// Withdraw money from vehicle account. /// </summary> /// /// <param name="vehicle">Represents object of vehicle.</param> public void GetPaid(object vehicle) { Vehicle v = (Vehicle)vehicle; double sum = 0; if (v.IsParked) { if (v.Balance >= v.Rate) { sum = v.Rate; v.Balance -= sum; Parking.GetInstance().Balance += sum; Parking.GetInstance().transactions.Add(new Transaction() { Amount = sum, TimeOfTransaction = DateTime.Now, VehicleId = v.Id }); } else { sum = v.Rate * Settings.penalty; v.Balance -= sum; Parking.GetInstance().Balance += sum; Parking.GetInstance().transactions.Add(new Transaction() { Amount = sum, TimeOfTransaction = DateTime.Now, VehicleId = v.Id }); } } else { this.timer.Change(Timeout.Infinite, Timeout.Infinite); } }
/// <summary> /// Set timers for automatic writing logs. /// </summary> public static void LogTransactions() { File.Create(FilePath).Close(); TimerCallback tmc1 = new TimerCallback(WriteTransactions); TimerCallback tmc2 = new TimerCallback(CheckTransactions); Timer timer1 = new Timer(tmc1, Parking.GetInstance().transactions, 60000, 60000); Timer timer2 = new Timer(tmc2, Parking.GetInstance().transactions, 0, 1000); }
/// <summary> /// Print all transactions for last minute. /// </summary> public static void ViewLastMinuteTransactions() { List <Transaction> tr = new List <Transaction>(Parking.GetInstance().transactions); foreach (Transaction t in tr) { Console.WriteLine(t); } }
static void Main(string[] args) { while (true) { Console.WriteLine(Menu); string choise = ReadInput(); if (int.TryParse(choise, out int num) && Enumerable.Range(1, 9).Contains(num)) { switch (num) { case 1: Console.WriteLine($"The balance of ParkingLot is {Parking.GetInstance().Balance}"); break; case 2: Console.WriteLine($"The amount of money earned for last minute is { TransactionsViewer.ShowLastMinuteIncome()}"); break; case 3: Console.WriteLine($"There are {Settings.capacity - Parking.GetInstance().vehicles.Count} spare places at the parking."); break; case 4: TransactionsViewer.ViewLastMinuteTransactions(); break; case 5: TransactionsLogger.ShowAllTranactions(); break; case 6: VehicleViewer.ShowParkedVehicle(); break; case 7: new VehicleCreator().AddVehicle(); TransactionsLogger.LogTransactions(); break; case 8: VehicleRemover.TakeVehicle(); break; case 9: VehicleAccount.FundAccount(VehicleViewer.FindVehicle(VehicleViewer.ReadLicenseNumber())); break; } } else { Console.WriteLine("Incorrect input. Try again!"); } } }
/// <summary> /// Return the value of paking lot income for last minute. /// </summary> /// <returns>Return the income of paking lot for last minute.</returns> public static double ShowLastMinuteIncome() { double income = 0; List <Transaction> tr = new List <Transaction>(Parking.GetInstance().transactions); foreach (Transaction t in tr.ToArray()) { income += t.Amount; } return(income); }
/// <summary> /// Check whether the vehicle is parked. /// </summary> public static void TakeVehicle() { var id = VehicleViewer.ReadLicenseNumber(); if (VehicleViewer.IsParked(id)) { var v = VehicleViewer.FindVehicle(id); check : if (v.Balance < 0) { Console.WriteLine($"You have a debt for parking in amount of {v.Balance}. Please fund your account."); VehicleAccount.FundAccount(v); goto check; } v.IsParked = false; Parking.GetInstance().vehicles.Remove(v); Console.WriteLine("Taking vehicle is successful."); } else { Console.WriteLine($"There is no vehicle with license number {id}"); } }