Ejemplo n.º 1
0
        public ParkingSession EnterParking(string carPlateNumber)
        {
            ParkingSession session;
            int            ticketNumber;

            if (activeSessions.FindIndex(item => item.CarPlateNumber == carPlateNumber) != -1)
            {
                Console.WriteLine("A car with this number already exists");
                return(null);
            }
            if (activeSessions.Count == ParkingCapacity)
            {
                Console.WriteLine("No free places");
                return(null);
            }
            if (activeSessions.Count != 0)
            {
                ticketNumber = activeSessions[activeSessions.Count - 1].TicketNumber + 1;
            }
            else
            {
                ticketNumber = 0;
            }
            session = new ParkingSession(carPlateNumber, getCurrentTime(), ticketNumber);
            activeSessions.Add(session);
            DataBase.ChangeActiveSessions(activeSessions);
            Console.WriteLine(
                "Car enters the parking at {0}\n" +
                "ticket number: {1}", session.EntryDt, session.TicketNumber);
            return(session);
        }
Ejemplo n.º 2
0
 private void CompleteSession(ParkingSession session, DateTime currentDt)
 {
     session.ExitDt = currentDt;
     activeSessions.Remove(session);
     pastSessions.Add(session);
     SaveData();
 }
Ejemplo n.º 3
0
        public bool TryLeaveParkingWithTicket(int ticketNumber)
        {
            ParkingSession session = activeSessions.Find(item => item.TicketNumber == ticketNumber);

            if (session.PaymentDt == null)
            {
                Console.WriteLine("Pay for parking!");
                return(false);
            }
            DateTime exitDt = getCurrentTime();

            if (session.EntryDt.Second - exitDt.Second < FreeLeavePeriod * 60)
            {
                session.ExitDt = exitDt;
                Console.WriteLine("Car exit the parking at " + session.ExitDt);
                completedSessions.Add(session);
                activeSessions.Remove(session);
                DataBase.ChangeActiveSessions(activeSessions);
                DataBase.ChangeCompletedSessions(completedSessions);
            }
            else
            {
                Console.WriteLine("Need additional pay");
                GetRemainingCost(ticketNumber);
                session.ExitDt = getCurrentTime();
                Console.WriteLine("Car exit the parking at " + session.ExitDt);
                completedSessions.Add(session);
                activeSessions.Remove(session);
                DataBase.ChangeActiveSessions(activeSessions);
                DataBase.ChangeCompletedSessions(completedSessions);
            }
            return(true);
        }
Ejemplo n.º 4
0
        public void GetRemainingCost(int ticketNumber)
        {
            ParkingSession session   = activeSessions.Find(item => item.TicketNumber == ticketNumber);
            DateTime       paymentDt = getCurrentTime();

            session.TotalPayment += GetCostFromTariff(paymentDt - session.PaymentDt);
            Console.WriteLine("Additional pay " + session.TotalPayment);
            DataBase.ChangeActiveSessions(activeSessions);
        }
Ejemplo n.º 5
0
        public ParkingManager()
        {
            LoadData();

            // Test hardcode data for UserAppWindow.UI
            // Just examples

            List <ParkingSession> PastSessions = pastSessions;
            ParkingSession        active       = new ParkingSession
            {
                EntryDt        = new DateTime(2021, 1, 26, 22, 03, 00),
                UserId         = 3,
                CarPlateNumber = "A451BX919",
                TicketNumber   = 99
            };
            ParkingSession active1 = new ParkingSession
            {
                EntryDt        = new DateTime(2021, 1, 26, 19, 03, 00),
                CarPlateNumber = "A451BX777",
                TicketNumber   = 100,
                UserId         = 4
            };

            activeSessions.Add(active);
            activeSessions.Add(active1);

            ParkingSession completed = new ParkingSession
            {
                EntryDt        = new DateTime(2020, 11, 29, 22, 03, 00),
                CarPlateNumber = "A451BX750",
                TicketNumber   = 24,
                UserId         = 1,
                PaymentDt      = new DateTime(2020, 11, 29, 22, 03, 08),
                ExitDt         = new DateTime(2020, 11, 29, 23, 19, 22),
                TotalPayment   = 300,
                User           = users.FirstOrDefault(u => u.Id == 1)
            };

            ParkingSession completed1 = new ParkingSession
            {
                EntryDt        = new DateTime(2020, 09, 29, 22, 03, 00),
                CarPlateNumber = "A451BX333",
                TicketNumber   = 25,
                UserId         = 2,
                PaymentDt      = new DateTime(2020, 09, 29, 22, 08, 08),
                ExitDt         = new DateTime(2020, 09, 30, 23, 19, 22),
                TotalPayment   = 250,
                User           = users.FirstOrDefault(u => u.Id == 2)
            };

            pastSessions.Add(completed);
            pastSessions.Add(completed1);
        }
Ejemplo n.º 6
0
        public void PayForParking(int ticketNumber)
        {
            ParkingSession session = activeSessions.Find(item => item.TicketNumber == ticketNumber);

            if (session != null)
            {
                session.PaymentDt    = getCurrentTime();
                session.TotalPayment = GetCostFromTariff(session.PaymentDt - session.EntryDt);
                Console.WriteLine("Visitor pay " + session.TotalPayment);
                DataBase.ChangeActiveSessions(activeSessions);
            }
        }
Ejemplo n.º 7
0
        public bool TryLeaveParkingByCarPlateNumber(string carPlateNumber)
        {
            ParkingSession session = activeSessions.Find(item => item.CarPlateNumber == carPlateNumber);
            DateTime       exitDt  = getCurrentTime();

            if ((exitDt - session.EntryDt).TotalSeconds < FreeLeavePeriod * 60)
            {
                session.ExitDt = exitDt;
                Console.WriteLine("Car exit the parking at " + session.ExitDt);
                DataBase.ChangeActiveSessions(activeSessions);
                return(true);
            }
            if (session.PaymentDt != null)
            {
                if (session.PaymentDt.Value.Second - exitDt.Second < FreeLeavePeriod * 60)
                {
                    session.ExitDt = exitDt;
                    Console.WriteLine("Car exit the parking at " + session.ExitDt);
                    DataBase.ChangeActiveSessions(activeSessions);
                }
                else
                {
                    Console.WriteLine("Need additional pay");
                    GetRemainingCost(session.TicketNumber);
                    session.ExitDt = getCurrentTime();
                    Console.WriteLine("Car exit the parking at " + session.ExitDt);
                    DataBase.ChangeActiveSessions(activeSessions);
                }
            }
            else
            {
                User user = users.Find(item => item.CarPlateNumber == carPlateNumber);
                if (user != null)
                {
                    TimeSpan?detTime = exitDt - session.EntryDt;
                    detTime = detTime.Value.Subtract(new TimeSpan(0, FreeLeavePeriod, 0));
                    session.TotalPayment = GetCostFromTariff(detTime);
                    session.ExitDt       = exitDt;
                    Console.WriteLine("Car exit the parking at " + session.ExitDt);
                    completedSessions.Add(session);
                    activeSessions.Remove(session);
                    DataBase.ChangeActiveSessions(activeSessions);
                    DataBase.ChangeCompletedSessions(completedSessions);
                }
                else
                {
                    Console.WriteLine("Pay for parking!");
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 8
0
        public bool TryLeaveParkingByCarPlateNumber(string carPlateNumber, out ParkingSession session)
        {
            session = activeSessions.FirstOrDefault(s => s.CarPlateNumber == carPlateNumber);
            if (session == null)
            {
                return(false);
            }

            var currentDt = DateTime.Now;

            if (session.PaymentDt != null)
            {
                if ((currentDt - session.PaymentDt.Value).TotalMinutes <= freeLeavePeriod)
                {
                    CompleteSession(session, currentDt);
                    return(true);
                }
                else
                {
                    session = null;
                    return(false);
                }
            }
            else
            {
                // No payment, within free leave period -> allow exit
                if ((currentDt - session.EntryDt).TotalMinutes <= freeLeavePeriod)
                {
                    session.TotalPayment = 0;
                    CompleteSession(session, currentDt);
                    return(true);
                }
                else
                {
                    // The session has no connected customer
                    if (session.User == null)
                    {
                        session = null;
                        return(false);
                    }
                    else
                    {
                        session.TotalPayment = GetCost((currentDt - session.EntryDt).TotalMinutes - freeLeavePeriod);
                        session.PaymentDt    = currentDt;
                        CompleteSession(session, currentDt);
                        return(true);
                    }
                }
            }
        }
Ejemplo n.º 9
0
        public bool TryLeaveParkingWithTicket(int ticketNumber, out ParkingSession session)
        {
            session = GetSessionByTicketNumber(ticketNumber);

            var currentDt = DateTime.Now;  // Getting the current datetime only once

            var diff = (currentDt - (session.PaymentDt ?? session.EntryDt)).TotalMinutes;

            if (diff <= freeLeavePeriod)
            {
                session.TotalPayment = 0;
                CompleteSession(session, currentDt);
                return(true);
            }
            else
            {
                session = null;
                return(false);
            }
        }
Ejemplo n.º 10
0
        public ParkingSession EnterParking(string carPlateNumber)
        {
            if (activeSessions.Count >= parkingCapacity || activeSessions.Any(s => s.CarPlateNumber == carPlateNumber))
            {
                return(null);
            }

            var session = new ParkingSession
            {
                CarPlateNumber = carPlateNumber,
                EntryDt        = DateTime.Now,
                TicketNumber   = nextTicketNumber++,
                User           = users.FirstOrDefault(u => u.CarPlateNumber == carPlateNumber)
            };

            session.UserId = session.User?.Id;

            activeSessions.Add(session);
            SaveData();
            return(session);
        }