/*
         * Deletes the guest at given index in list of guests for the
         * current booking.
         * Undecorates that guest if if it is also a
         * customer and updates CurrentCustomer with the correct
         * memory reference.
         */
        public void DeleteGuest(int index)
        {
            // First unwrap guest decorator from current customer if they are
            // the guest being deleted:
            if (CurrentBook.GetGuests().ElementAt(index).IsCustomer())
            {
                List <PersonComponent> savedGuests = CurrentBook.GetGuests();
                DateTime arrival;
                DateTime departure;
                CurrentBook.GetDates(out arrival, out departure);

                CurrentCust = CurrentCust.UndecorateOnce();
                CurrentBook = bFact.UpdateBooking(CurrentBook.GetBookingNb(),
                                                  CurrentCust,
                                                  arrival,
                                                  departure);

                foreach (PersonComponent g in savedGuests)
                {
                    CurrentBook.GetGuests().Add(g);
                }
            }

            // Then delete selected guest reference from guests list:
            CurrentBook.GetGuests().RemoveAt(index);
        }
Esempio n. 2
0
        // METHODS:

        /*
         * Constructor.
         *
         * throws ArgumentException if bookingNb is not strictly greater
         * than 0, if departure day is not strictly later than arrival
         * day, or if customer is not decorated as a customer.
         */
        public Booking(int bookingNb,
                       PersonComponent customer,
                       DateTime arrival,
                       DateTime departure)
        {
            if (customer.GetCustNb() <= 0)
            {
                throw new ArgumentException("Booking.cust must be"
                                            + "  decorated as a Customer");
            }

            if (bookingNb <= 0)
            {
                throw new ArgumentException("Booking.bookingNb must be"
                                            + " strictly greater than 0");
            }

            if (departure.DayOfYear <= arrival.DayOfYear &&
                departure.Year <= arrival.Year)
            {
                throw new ArgumentException("departure day must be strictly"
                                            + " later than arrival day");
            }

            this.bookingNb = bookingNb;
            this.cust      = customer;
            this.arrival   = arrival;
            this.departure = departure;
        }
        /*
         * Returns a reference to the updated PersonComponent instance.
         */
        public PersonComponent UpdateCustomer(PersonComponent customer,
                                              String newName,
                                              String newAddress)
        {
            // create a fresh customer with the new values but same customerNb
            PersonComponent p = new Person(newName);
            PersonDecorator c = new Customer(newAddress, customer.GetCustNb());

            c.SetComponent(p);

            // get all decorators of customer being updated
            List <PersonDecorator> decorators;

            customer.Unwrap(out decorators);

            // decorate the new customer with those decorators except those
            // of type 'Customer'
            foreach (PersonDecorator decorator in decorators)
            {
                if (decorator.GetType() != typeof(Customer))
                {
                    decorator.SetComponent(c);
                    c = decorator;
                }
            }

            return(c);
        }
 /*
  * Generates a new booking instance on the basis of the data
  * passed as parameters.
  */
 public BookingComponent GetNewBooking(PersonComponent customer,
                                       DateTime arrival,
                                       DateTime departure)
 {
     return(new Booking(
                this.nxtBookingNb++, customer, arrival, departure));
 }
        // METHODS:

        /*
         * Adds a guest to the decorated booking.
         */
        public override void AddGuest(PersonComponent guest)
        {
            if (decoratedComponent != null)
            {
                decoratedComponent.AddGuest(guest);
            }
        }
 /*
  * Generates a new booking instance on the basis of the data
  * passed as parameters.
  */
 private BookingComponent getNewBooking(int bookingNb,
                                        PersonComponent customer,
                                        DateTime arrival,
                                        DateTime departure)
 {
     return(new Booking(bookingNb, customer, arrival, departure));
 }
 /*
  * Returns a reference to the updated booking instance.
  */
 public BookingComponent UpdateBooking(int bookingNb,
                                       PersonComponent newCustomer,
                                       DateTime newArrival,
                                       DateTime newDeparture)
 {
     return(new Booking(
                bookingNb, newCustomer, newArrival, newDeparture));
 }
Esempio n. 8
0
 /*
  * Adds a guest to the booking.
  *
  * Throws ArgumentException if there is already 4 guests added to
  * the booking.
  */
 public override void AddGuest(PersonComponent guest)
 {
     if (guests.Count >= 4)
     {
         throw new ArgumentException("this booking already has"
                                     + " 4 guests");
     }
     this.guests.Add(guest);
 }
        /*
         * Updates given customer's details with new values for all bookings
         * of his.
         */
        public void UpdateCurrentCustomer(String newName, String newAddress)
        {
            BookingComponent                    processedBooking;
            List <PersonComponent>              savedGuests;
            List <BookingDecorator>             decorationStack;
            List <Dictionary <String, String> > bookingData;
            DateTime arrival;
            DateTime departure;

            // update values within all persisted bookings made by current
            // customer
            foreach (int bookingNb
                     in dpFacade.GetAllBookingNbs(CurrentCust.GetCustNb()))
            {
                dpFacade.Read(bookingNb, out bookingData);

                processedBooking = bFact.Restore(bookingData)
                                   .Unwrap(out decorationStack);

                savedGuests = processedBooking.GetGuests();

                processedBooking.GetDates(out arrival, out departure);

                processedBooking = bFact.UpdateBooking(
                    processedBooking.GetBookingNb(),
                    pFact.UpdateCustomer(
                        processedBooking.GetCustomer(),
                        newName,
                        newAddress),
                    arrival,
                    departure);

                if (decorationStack != null)
                {
                    foreach (BookingDecorator reference in decorationStack)
                    {
                        reference.Setcomponent(processedBooking);
                        processedBooking = reference;
                    }
                }

                foreach (PersonComponent g in savedGuests)
                {
                    processedBooking.AddGuest(g);
                }

                CurrentCust = processedBooking.GetCustomer();
                dpFacade.Persist(processedBooking);
            }

            // reload current booking into the system to upload changes
            if (IsABookingLoaded())
            {
                RestoreBooking(CurrentBook.GetBookingNb());
            }
        }
        /*
         * Generates a new guest instance on the basis of the data
         * passed as parameters.
         */
        public PersonComponent GetNewGuest(
            PersonComponent customer,
            String passportNb,
            int age)
        {
            Guest g = new Guest(passportNb, age);

            g.SetComponent(customer);
            return(g);
        }
        /*
         * Returns the decorated BookingComponent's customer
         * (or null if the root component is not a concrete Booking).
         */
        public override PersonComponent GetCustomer()
        {
            PersonComponent customer = null;

            if (decoratedComponent != null)
            {
                customer = decoratedComponent.GetCustomer();
            }

            return(customer);
        }
Esempio n. 12
0
 public void TestBookingConstructor_DatesException()
 {
     // arrange
     PersonComponent c = PersonFactory.Instance
                         .GetNewCustomer("name", "address");
     // act
     BookingComponent b = new Booking(3,
                                      c,
                                      new DateTime(2016, 04, 18),
                                      new DateTime(2016, 02, 15));
 }
Esempio n. 13
0
 public void TestBookingConstructor_BookingNbException()
 {
     // arrange
     PersonComponent c = PersonFactory.Instance
                         .GetNewCustomer("name", "address");
     // act
     BookingComponent b = new Booking(-10,
                                      c,
                                      new DateTime(1998, 02, 18),
                                      new DateTime(1998, 03, 2));
 }
        /*
         * Returns the root PersonComponent of the decoration stack;
         * references outputs a list of pointers to all the PersonDecorator
         * instances in the decoration stack.
         */
        public override PersonComponent Unwrap(
            out List <PersonDecorator> references)
        {
            PersonComponent        component  = this;
            List <PersonDecorator> decorators = new List <PersonDecorator>();

            while (component.isDecorator())
            {
                decorators.Add((PersonDecorator)component);
                component = ((PersonDecorator)component).decoratedComponent;
            }

            references = decorators;
            return(component);
        }
        /*
         * Loads the customer matching given customer number into the system
         * (from persisted data).
         * Returns true if the customer was found & loaded successfully,
         * otherwise false.
         */
        public bool RestoreCustomer(int customerNb)
        {
            bool wasRestored = true;
            Dictionary <String, String> customerData;

            if (!dpFacade.Read(customerNb, out customerData))
            {
                wasRestored = false;
            }
            else
            {
                CurrentCust = pFact.RestoreCustomer(customerData);
            }
            return(wasRestored);
        }
        /*
         * Loads the booking matching given booking number into the system
         * (from persisted data).
         * Returns true if the booking was found & loaded successfully,
         * otherwise false.
         */
        public bool RestoreBooking(int bookingNb)
        {
            bool wasRestored = true;
            List <Dictionary <String, String> > bookingData;

            if (!dpFacade.Read(bookingNb, out bookingData))
            {
                wasRestored = false;
            }
            else
            {
                CurrentBook = bFact.Restore(bookingData);
                CurrentCust = CurrentBook.GetCustomer();
            }
            return(wasRestored);
        }
Esempio n. 17
0
        public void TestBookingConstrutor_NormalCase()
        {
            // arrange
            PersonComponent c = PersonFactory.Instance
                                .GetNewCustomer("name", "address");

            BookingComponent b1 = new Booking(1,
                                              c,
                                              new DateTime(2016, 12, 09),
                                              new DateTime(2016, 12, 13));

            BookingComponent b2 = new Booking(1,
                                              c,
                                              new DateTime(2016, 12, 09),
                                              new DateTime(2016, 12, 13));

            // assert
            Assert.IsTrue(b1.Equals(b2), "Booking.Equals method problem");
        }
Esempio n. 18
0
        public void TestBookingAddGuest_NormalCase()
        {
            // arrange
            PersonComponent expected = PersonFactory.Instance
                                       .GetNewGuest("myself",
                                                    "08855N8",
                                                    31);
            BookingComponent b1 = new Booking(
                1,
                PersonFactory.Instance
                .GetNewCustomer("name",
                                "address"),
                new DateTime(1024, 12, 09),
                new DateTime(2016, 9, 3));

            // act
            b1.AddGuest(expected);

            // assert
            Assert.IsTrue(b1.GetGuests().Contains(expected),
                          "Problem with Booking.AddGuest");
        }
        /*
         * Adds current customer to current booking's list of guests.
         */
        public void AddCustomerToGuests(String passportNb, int age)
        {
            List <PersonComponent> savedGuests = CurrentBook.GetGuests();
            DateTime arrival;
            DateTime departure;

            CurrentBook.GetDates(out arrival, out departure);

            CurrentCust = pFact.GetNewGuest(CurrentCust,
                                            passportNb,
                                            age);
            CurrentBook = bFact.UpdateBooking(CurrentBook.GetBookingNb(),
                                              CurrentCust,
                                              arrival,
                                              departure);

            foreach (PersonComponent g in savedGuests)
            {
                CurrentBook.GetGuests().Add(g);
            }

            CurrentBook.AddGuest(CurrentCust);
        }
        /*
         * Returns a reference to a new PersonComponent identical in content
         * to calling instance, except unwraped from the PersonDecorator
         * passed as a parameter (or the to the Booking itself if it is not
         * decorated at all).
         */
        public override PersonComponent Undecorate(PersonDecorator reference)
        {
            if (this == reference)
            {
                return(decoratedComponent);
            }

            /* Short circuit method if the decorator to remove is the
             * last one added.
             */
            List <PersonDecorator> references;
            PersonComponent        result = this.Unwrap(out references);

            foreach (PersonDecorator decorator in references)
            {
                if (decorator != reference)
                {
                    decorator.decoratedComponent = result;
                    result = decorator;
                }
            }

            return(result);
        }
 public void SetComponent(PersonComponent p)
 {
     decoratedComponent = p;
 }
 /*
  * Closes the BookingComponent instance currently loaded in the
  * system.
  */
 public void CurrentBookingClose()
 {
     CurrentBook = null;
     CurrentCust = null;
 }
 /*
  * Instanciates a new customer.
  */
 public void CreateCustomer(String name, String address)
 {
     CurrentCust = pFact.GetNewCustomer(name, address);
 }
Esempio n. 24
0
 /*
  * Must add a guest to a given BookingComponent.
  */
 public abstract void AddGuest(PersonComponent guest);
 /*
  * Returns a list of all the booking numbers persisted on disc made
  * by a given customer.
  */
 public List <int> GetAllBookingNbs(PersonComponent customer)
 {
     return(dpFacade.GetAllBookingNbs(customer.GetCustNb()));
 }