Exemplo n.º 1
0
        /*
         * Persists the BookingComponent to given filePath, returns true if
         * data was persisted successfuly or false if not.
         */
        public bool Persist(BookingComponent booking, String filePath)
        {
            String dataDirName = Path.GetDirectoryName(filePath);

            if (!Directory.Exists(dataDirName))
            {
                CreateDir(dataDirName);
            }

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            try
            {
                System.IO.File.AppendAllText(filePath, booking.ToCSV());
            }
            catch
            {
                return(false);
            }

            return(true);
        }
        /*
         * Updates the BookingComponent instance currently loaded in the
         * system.
         */
        public void UpdateBooking(DateTime arrival, DateTime departure)
        {
            List <PersonComponent>  savedGuests = CurrentBook.GetGuests();
            List <BookingDecorator> decorationStack;
            BookingComponent        booking = CurrentBook.Unwrap(out decorationStack);

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

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

            CurrentBook = booking;

            foreach (PersonComponent g in savedGuests)
            {
                CurrentBook.AddGuest(g);
            }
        }
        /*
         * Decorates a BookingComponent, wrapping it inside an EveningMeal
         * decorator.
         */
        public EveningMeal AddEveningMeal(BookingComponent booking,
                                          String dietRequirements)
        {
            EveningMeal em = new EveningMeal(dietRequirements);

            em.Setcomponent(booking);
            return(em);
        }
        /*
         * Decorates a BookingComponent, wrapping it inside a Breakfast
         * decorator.
         */
        public Breakfast AddBreakfast(BookingComponent booking,
                                      String dietRequirements)
        {
            Breakfast bf = new Breakfast(dietRequirements);

            bf.Setcomponent(booking);
            return(bf);
        }
        // METHODS RELATED TO BOOKINGS:

        /*
         * Persists the BookingComponent to {dataDirectory}/{bookingNb}.csv;
         * returns true if data was persisted successfuly or false if not.
         */
        public bool Persist(BookingComponent booking)
        {
            String filePath = (String.Format(@"{0}/{1}.csv",
                                             dataDirectory,
                                             booking.GetBookingNb()));

            return(dataWriter.Persist(booking, filePath));
        }
 /*
  * Adds Guests to a BookingComponent according to data contents of a
  * dictonary<attribute, values>, presumably recovered from persisted
  * data (the dictonary keys should follow the naming implemented in
  * the *Field.cs enumerations)
  */
 private BookingComponent addGuestsData(
     BookingComponent b,
     List <Dictionary <String, String> > gData)
 {
     foreach (Dictionary <String, String> d in gData)
     {
         b.AddGuest(personFactory.RestoreGuest(d));
     }
     return(b);
 }
        /*
         * Returns the root BookingComponent of the decoration stack;
         * references outputs a list of pointers to all the BookingDecorator
         * instances in the decoration stack.
         */
        public override BookingComponent Unwrap(
            out List <BookingDecorator> references)
        {
            BookingComponent        component  = this;
            List <BookingDecorator> decorators = new List <BookingDecorator>();

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

            references = decorators;
            return(component);
        }
        /*
         * Decorates a BookingComponent according to data contents of a
         * dictonary<attribute, values>, presumably recovered from persisted
         * data (the dictonary keys should follow the naming implemented in
         * the *Field.cs enumerations)
         */
        private BookingComponent addExtrasData(
            BookingComponent b,
            Dictionary <String, String> bData)
        {
            if (b != null &&
                bData != null)
            {
                String s1;
                String s2;
                String s3;

                if (bData.TryGetValue(
                        BreakfastField.DIET_REQUIREMENT_BREAKFAST
                        .ToString(),
                        out s1))
                {
                    b = AddBreakfast(b, s1);
                }

                if (bData.TryGetValue(
                        EveningMealField.DIET_REQUIREMENT_EVENING
                        .ToString(),
                        out s1))
                {
                    b = AddEveningMeal(b, s1);
                }

                if (bData.TryGetValue(CarHireField.DRIVER_NAME.ToString(),
                                      out s1) &&
                    bData.TryGetValue(CarHireField.START.ToString(),
                                      out s2) &&
                    bData.TryGetValue(CarHireField.END.ToString(),
                                      out s3))
                {
                    b = AddCarHire(b,
                                   s1,
                                   Convert.ToDateTime(s2),
                                   Convert.ToDateTime(s3));
                }
            }
            return(b);
        }
        /*
         * Instantiates a BookingComponent from a
         * List<Dictonary<attribute, values>>, presumably recovered from
         * persisted data (the dictonaries keys should follow the naming
         * implemented in the *Field.cs enumerations)
         *
         * Thows Argument exception if there is a problem with the contents
         * of the dictionary passed as a parameter.
         */
        public BookingComponent Restore(
            List <Dictionary <String, String> > booking)
        {
            BookingComponent result = null;

            Dictionary <String, String>         bData;
            Dictionary <String, String>         cData;
            List <Dictionary <String, String> > guestsData;

            extract(booking, out bData, out cData, out guestsData);

            String bookingNb;
            String csvArrival;
            String csvDeparture;

            if (bData != null &&
                cData != null &&
                bData.TryGetValue(BookingField.BOOKING_NUMBER.ToString(),
                                  out bookingNb) &&
                bData.TryGetValue(BookingField.ARRIVAL.ToString(),
                                  out csvArrival) &&
                bData.TryGetValue(BookingField.DEPARTURE.ToString(),
                                  out csvDeparture))
            {
                result = getNewBooking(Int32.Parse(bookingNb),
                                       personFactory.RestoreCustomer(cData),
                                       Convert.ToDateTime(csvArrival),
                                       Convert.ToDateTime(csvDeparture));

                if (result.GetCustomer().IsGuest())
                {
                    result.AddGuest(result.GetCustomer());
                }
            }

            result = addGuestsData(result, guestsData);
            result = addExtrasData(result, bData);
            return(result);
        }
        /*
         * Decorates a BookingComponent, wrapping it inside a CarHire
         * decorator.
         *
         * Throws ArgumentException if either  hire start or hire end
         * date falls without the booking dates range.
         */
        public CarHire AddCarHire(BookingComponent booking,
                                  String driverName,
                                  DateTime start,
                                  DateTime end)
        {
            DateTime arrival;
            DateTime departure;

            booking.GetDates(out arrival, out departure);

            if (start < arrival || end > departure)
            {
                throw new ArgumentException("CarHire start & end dates must"
                                            + " be within the booking dates"
                                            + " range.");
            }

            CarHire ch = new CarHire(driverName, start, end);

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

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

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

            return(result);
        }
 public void Setcomponent(BookingComponent booking)
 {
     decoratedComponent = booking;
 }