Over a million developers have joined DZone.
Platinum Partner

Parking Lot - Simplistic Design C#

· Web Dev Zone

The Web Dev Zone is brought to you in partnership with Mendix. Download this Forrester Report to gain a better understanding of the low-code platform market and how to make a strategic platform selection you won’t regret.

Parking Lot Design Implementation in C#.

    public class ParkingLot
    {
        List _parkingSpots;

        public ParkingLot()
        {
            _parkingSpots = new List();
            // 10 parking spots; 2 free; 6 Regular Paid; 2 Handicapped Free

            //2 free
            for (int i = 0; i  p.IsFree == true && p.IsAvailable);
        }

        public ParkingSpot FindPaidSpot()
        {
            return this._parkingSpots.Find(p => p.IsFree == false && p.IsAvailable);
        }

        public int GetAvailableSpotsCount()
        {
            return this._parkingSpots.Count(p => p.IsAvailable);
        }

        public int GetTotalSpots()
        {
            return this._parkingSpots.Count;
        }
    }

    public class ParkingSpot
    {
        public bool IsAvailable { get; set; }
        public bool IsFree { get; set; }
        public IVehicle ParkedVehicle { get; set; }
        public ParkingMeter Meter { get; set; }

        public void Park(IVehicle vehicle)
        {
            if (this.ParkedVehicle == null)
            {
                this.ParkedVehicle = vehicle;
                this.IsAvailable = false;
            }
            else
            {
                throw new Exception("Parking Spot is Taken. Cannot Park here!");
            }
        }

        public ParkingSpot()
        {
            this.IsAvailable = true;
            this.IsFree = true;
        }

        public ParkingSpot(bool isFree)
        {
            this.IsAvailable = true;
            this.IsFree = isFree;
            if (!this.IsFree)
            {
                this.Meter = new ParkingMeter();
            }
        }
    }

    public class HandicappedParkingSpot : ParkingSpot
    {

    }

    public class ParkingMeter
    {
        public DateTime EndTime { get; set; }
        public int MinutesRemaining
        {
            get
            {
                if (DateTime.Now >= EndTime)
                    return 0;
                else
                    return (EndTime - DateTime.Now).Minutes;
            }
        }

        public int ParkingIntervalMins
        {
            get
            {
                return 1;
            }
        }

        public void Pay(int quarters)
        {
            EndTime = DateTime.Now.AddMinutes(quarters * ParkingIntervalMins);
        }

    }


    public interface IVehicle
    {
        string Make { get; set; }
        string Model { get; set; }
        void Drive();
    }

    public class Car : IVehicle
    {
        public string Make
        {
            get;
            set;
        }

        public string Model
        {
            get;
            set;
        }

        public void Drive()
        {
        }

        public void Park()
        {
        }

    }

    public class Truck : IVehicle
    {
        public string Make
        {
            get;
            set;
        }

        public string Model
        {
            get;
            set;
        }

        public void Drive()
        {
        }

        public void Park()
        {
        }
    }

The Web Dev Zone is brought to you in partnership with Mendix. Better understand the aPaaS landscape and how the right platform can accelerate your software delivery cadence and capacity with the Gartner 2015 Magic Quadrant for Enterprise Application Platform as a Service.

Topics:
web dev,c#

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}