public async Task <bool> RemoveShuttlePassengerById(string id)
        {
            // HAS NOT BEEN DEBUG'd yet.

            Debug.WriteLine("[DAO] - Removing Shuttle Passenger...");

            //EF only allows deletion by 'creating' the to-be-deleted object from the database, then actually deleting it

            ShuttlePassenger passenger = _context.ShuttlePassenger.Local.First(x => x.RetrieveId() == id);

            _context.Remove(passenger);

            //to avoid InvalidOperationException. uncomment below chunk if that exception happens. this 'should' fix it.

            /*if (passenger == null)
             * {
             *  passenger = new ShuttlePassenger() { Id = id };
             *  _context.ShuttlePassenger.Attach(passenger);
             * }*/

            _context.ShuttlePassenger.Remove(passenger);
            Debug.WriteLine("[DAO] - Removing Shuttle Passenger... Successful.");

            //Check for successful changes to database
            //Successful
            if (await _context.SaveChangesAsync() > 0)
            {
                return(true);
            }
            //Fail
            else
            {
                return(false);
            }
        }
        public async Task <bool> InsertShuttlePassenger(ShuttlePassenger shuttlePassenger)
        {
            Debug.WriteLine("[DAO] - Adding Shuttle Passenger...");
            _context.Add(shuttlePassenger);

            //successful save after add
            if (await _context.SaveChangesAsync() > 0)
            {
                _context.Entry <ShuttlePassenger>(shuttlePassenger).State = EntityState.Detached;    //detaches the object after adding. should solve?
                Debug.WriteLine("[DAO] - Added Shuttle Passenger.");
                return(true);
            }

            //Fail
            else
            {
                return(false);
            }
        }
示例#3
0
        //helper function.
        //runs through every shuttleschedule, passenger and bus to determine the most efficient way to add seats
        //compiles and returns a List<ShuttlePassenger> object that represents the passengers to add
        public List <ShuttlePassenger> PrepareShuttlePassengers(string scheduleId, DateTime dateTime, string direction, int numOfPassengers)
        {
            List <ShuttlePassenger> passengerList;
            ShuttleBus       bus;
            ShuttlePassenger newPassenger;

            List <ShuttlePassenger> passengersToAdd = new List <ShuttlePassenger>();

            int passengerCounter = 1;   //to keep track of passenger Id when passengers are split across several buses

            int neededAmountOfSeats = numOfPassengers;

            List <ShuttleSchedule> shuttleScheduleList = _shuttleScheduleDAO.RetrieveAllShuttleBookingByDateAndDirectionAndState(dateTime, direction, "CREATED");
            List <string>          busIdList           = new List <string>();

            List <string> accessedBuses = new List <string>();

            //for every shuttleschedule in the same direction and time
            //we need the buses under these schedules
            foreach (ShuttleSchedule s in shuttleScheduleList)
            {
                Debug.WriteLine("[PrepareShuttlePassengers] - Checking schedule of id: " + s.RetrieveId());

                if (busIdList == null)
                {
                    busIdList = GetBusIdInSameSchedule(s.RetrieveId());
                }
                else
                {
                    busIdList.AddRange(GetBusIdInSameSchedule(s.RetrieveId()));
                    List <string> tempList = busIdList;
                    busIdList = tempList.Distinct().ToList();  //get only distinct new buses. ensures no duplicate buses.


                    //shuttle bus 1 has duplicates in this - why?
                }

                foreach (string busId in busIdList)
                {
                    //quick check to see if busId has been interacted with before
                    bool hasBeenAccessed = false;
                    foreach (string accessedBusId in accessedBuses)
                    {
                        if (busId == accessedBusId)
                        {
                            hasBeenAccessed = true;
                        }
                    }

                    if (hasBeenAccessed)
                    {
                        continue;
                    }

                    accessedBuses.Add(busId);

                    //end of quick check

                    Debug.WriteLine("[PrepareShuttlePassengers - EXISTING - CHECK] - Looking at Bus " + busId);

                    bus = _shuttleBusDAO.RetrieveShuttleBusById(busId);

                    passengerList = GetShuttlePassengersOfBus(busId, dateTime, direction);
                    Debug.WriteLine("[PrepareShuttlePassengers - EXISTING - CHECK] - Bus has " + passengerList.Count + " passengers");

                    if (passengerList.Count == bus.RetrieveShuttleBusCapacity())
                    {
                        //this bus is full. go to next available bus.
                        Debug.WriteLine("[PrepareShuttlePassengers - EXISTING - CHECK] - Bus of Id " + bus.RetrieveId() + " already fully booked.");
                        continue;
                    }

                    if (passengerList.Count + neededAmountOfSeats <= bus.RetrieveShuttleBusCapacity())
                    {
                        //possible to book all remaining needed seats under this bus.
                        Debug.WriteLine("[PrepareShuttlePassengers - EXISTING - CHECK/pass] - GOOD CAPACITY - Able to book " + neededAmountOfSeats +
                                        " amount of seats for bus " + bus.RetrieveId());

                        for (int i = 0; i < neededAmountOfSeats; i++)
                        {
                            newPassenger = new ShuttlePassenger(GeneratePassengerID(scheduleId, passengerCounter.ToString()), DateTime.Now,
                                                                scheduleId, bus.RetrieveId(), passengerCounter.ToString());

                            Debug.WriteLine("[PrepareShuttlePassengers - EXISTING - ADD] - Preparing passenger of Id " + newPassenger.RetrieveId());

                            passengersToAdd.Add(newPassenger);

                            passengerCounter++;
                        }

                        return(passengersToAdd);
                    }

                    else
                    {
                        //can add some seats, but not enough to finish the booking

                        int possibleSeatsCount = bus.RetrieveShuttleBusCapacity() - passengerList.Count;

                        Debug.WriteLine("[PrepareShuttlePassengers - EXISTING - CHECK] - OVER CAPACITY - " +
                                        "Unable to book " + neededAmountOfSeats + " amount of seats for bus " + bus.RetrieveId());
                        Debug.WriteLine("[PrepareShuttlePassengers - EXISTING - CHECK] - OVER CAPACITY - " +
                                        "Bus " + bus.RetrieveId() + " currently can offer " +
                                        (possibleSeatsCount) + " seats.");

                        neededAmountOfSeats -= possibleSeatsCount;  //update remaining amt of seats to add

                        for (int i = 0; i < possibleSeatsCount; i++)
                        {
                            newPassenger = new ShuttlePassenger(GeneratePassengerID(scheduleId, passengerCounter.ToString()), DateTime.Now,
                                                                scheduleId, bus.RetrieveId(), passengerCounter.ToString());

                            Debug.WriteLine("[PrepareShuttlePassengers - EXISTING - ADD] - Preparing passenger of Id " + newPassenger.RetrieveId());

                            passengersToAdd.Add(newPassenger);

                            passengerCounter++;
                        }

                        Debug.WriteLine("[PrepareShuttlePassengers - EXISTING - CHECK] - " + neededAmountOfSeats + " more seats needed.");
                    }

                    //proceed to next available shuttle schedule
                    //^SHOULD NOT be the same shuttleschedule
                }
            }

            Debug.WriteLine("[PrepareShuttlePassengers] - All currently booked buses are full. " +
                            "Checking unbooked buses for " + neededAmountOfSeats + " seats.");

            List <string> bookedBusList = busIdList; //same list as in the first for loop

            List <ShuttleBus> busList = _shuttleBusDAO.RetrieveAllShuttleBuses();

            bool isBooked;

            // for every bus in the system
            // we need to look for unbooked buses.
            foreach (ShuttleBus b in busList)
            {
                isBooked = false;

                if (bookedBusList != null)
                {
                    //check if bus in system is referenced by currently booked buses
                    // we don't want to book slots for buses that we already know are full

                    //also, prevents null exception in the later for loop

                    foreach (string id in bookedBusList)
                    {
                        if (b.RetrieveId() == id)
                        {
                            //id matches. bus 'b' is already booked.
                            //end this current foreloop so we can proceed to next bus.
                            Debug.WriteLine("[PrepareShuttlePassengers - UNBOOKED - CHECK] - Bus of Id " + id + " already booked.");
                            isBooked = true;
                            break;
                        }
                    }
                }

                if (isBooked)
                {
                    continue;   //bus is booked, we are only looking for unbooked buses. go to next bus in database.
                }
                else
                {
                    //this bus isn't booked. can try to add passengers into it
                    if (neededAmountOfSeats <= b.RetrieveShuttleBusCapacity())
                    {
                        Debug.WriteLine("[PrepareShuttlePassengers - UNBOOKED - CHECK/pass] - GOOD CAPACITY - Able to book " + neededAmountOfSeats + " amount of seats for empty bus " + b.RetrieveId());

                        for (int i = 0; i < neededAmountOfSeats; i++)
                        {
                            newPassenger = new ShuttlePassenger(GeneratePassengerID(scheduleId, passengerCounter.ToString()), DateTime.Now,
                                                                scheduleId, b.RetrieveId(), passengerCounter.ToString());

                            Debug.WriteLine("[PrepareShuttlePassengers - UNBOOKED - ADD] - Preparing passenger of Id " + newPassenger.RetrieveId());

                            passengersToAdd.Add(newPassenger);

                            passengerCounter++;
                        }

                        return(passengersToAdd);        //bus have enough seats. check passed.
                    }

                    else
                    {
                        Debug.WriteLine("[PrepareShuttlePassengers - UNBOOKED - CHECK] - OVER-CAPACITY - Unable to book " + neededAmountOfSeats + " amount of seats for empty bus " + b.RetrieveId());

                        //can't book all at one go. book the bus until its full, then go to the next bus.

                        int possibleSeatsCount = b.RetrieveShuttleBusCapacity();

                        Debug.WriteLine("[PrepareShuttlePassengers - UNBOOKED - CHECK] - OVER CAPACITY - " +
                                        "Empty Bus " + b.RetrieveId() + " can offer " +
                                        (possibleSeatsCount) + " seats.");


                        for (int i = 0; i < possibleSeatsCount; i++)
                        {
                            newPassenger = new ShuttlePassenger(GeneratePassengerID(scheduleId, passengerCounter.ToString()), DateTime.Now,
                                                                scheduleId, b.RetrieveId(), passengerCounter.ToString());

                            Debug.WriteLine("[PrepareShuttlePassengers - UNBOOKED - ADD] - Preparing passenger of Id " + newPassenger.RetrieveId());

                            passengersToAdd.Add(newPassenger);

                            passengerCounter++;
                        }

                        neededAmountOfSeats -= possibleSeatsCount;
                        Debug.WriteLine("[PrepareShuttlePassengers - UNBOOKED - CHECK] - " + neededAmountOfSeats + " more seats needed.");
                        //update required amount of seats. then run the loop again.
                    }
                }
            }

            Debug.WriteLine("[PrepareShuttlePassengers - CHECK/FAIL] - Not enough seats in the system. Failed check.");

            return(null);
        }