Esempio n. 1
0
        /// <summary>Attach the model to the view.</summary>
        /// <param name="model">The model to work with</param>
        /// <param name="view">The view to attach to</param>
        /// <param name="explorerPresenter">The parent explorer presenter</param>
        public void Attach(object model, object view, ExplorerPresenter explorerPresenter)
        {
            this.summary = model as Summary;
            this.view = view as ISummaryView;
            this.explorerPresenter = explorerPresenter;

            // populate the simulation names in the view.
            Simulation simulation = Apsim.Parent(this.summary, typeof(Simulation)) as Simulation;
            if (simulation != null)
            {
                if (simulation.Parent is Experiment)
                {
                    Experiment experiment = simulation.Parent as Experiment;
                    string[] simulationNames = experiment.Names();
                    this.view.SimulationNames = simulationNames;
                    if (simulationNames.Length > 0)
                        this.view.SimulationName = simulationNames[0];
                }
                else
                {
                    this.view.SimulationNames = new string[] { simulation.Name };
                    this.view.SimulationName = simulation.Name;
                }

                // create a data store - we'll need it later
                dataStore = new DataStore(simulation, false);

                // populate the view
                this.SetHtmlInView();

                // subscribe to the simulation name changed event.
                this.view.SimulationNameChanged += OnSimulationNameChanged;
            }
        }
 private static void ParseLoyaltyPoints(string[] elements, Summary summary)
 {
     if (elements.Length > 4)
     {
         // if not provided the value for whether using loyalty points, it would be treated as false
         // and if it's false, no need to parse the value of number of loyalty points
         var strLoyaltyPoints = elements.ElementAt(4);
         var usingLoyaltyPoints = elements.ElementAt(5);
         if (usingLoyaltyPoints != null &&
             string.Equals("TRUE", usingLoyaltyPoints.Trim(), StringComparison.CurrentCultureIgnoreCase))
         {
             var loyaltyPoints = ParseStringToNumber(strLoyaltyPoints);
             // passenger bringing extra baggage
             summary.LoyaltyPointsRedeemed += loyaltyPoints;
         }
     }
 }
        public ISummary Process(IEnumerable<string> inputLines)
        {
            var summary = new Summary();

            if (inputLines == null)
                return summary;

            var listInputLines = inputLines.ToList();

            if (listInputLines.Count == 0)
                return summary;

            var validInput = _inputrValidator.IsValidInput(listInputLines, null);

            if (!validInput)
            {
                summary.InvalidInput = true;
                return summary;
            }

            double costPerPassenger = 0;
            double ticketPrice = 0;
            double targetOccupancy = 0d;
            int totalSeats = 0;
            // TODO: following code should be moved to another class and should be injected through constructor
            foreach (var elements in listInputLines.Where(item => !string.IsNullOrWhiteSpace(item)).Select(item => item.Split(null)))
            {
                // all input lines are validated.
                // element at index 0 is action, index 1 is type of action

                var instructionType = elements.ElementAt(1);

                if (string.Equals(Constants.Route, instructionType, StringComparison.InvariantCultureIgnoreCase))
                {
                    //TODO: Following should be moved to another method and return Tuple
                    double.TryParse(elements.ElementAt(4).Trim(), out costPerPassenger);
                    double.TryParse(elements.ElementAt(5).Trim(), out ticketPrice);
                    double.TryParse(elements.ElementAt(6).Trim(), out targetOccupancy);
                    continue;
                }
                if (string.Equals(Constants.Aircraft, instructionType, StringComparison.InvariantCultureIgnoreCase))
                {
                    int.TryParse(elements.ElementAt(3).Trim(), out totalSeats);
                    continue;
                }
                if (string.Equals(Constants.Loyalty, instructionType, StringComparison.InvariantCultureIgnoreCase))
                {
                    summary.NumberOfLoyaltyPassengers++;
                    ParseExtraBaggage(elements, summary);
                    ParseLoyaltyPoints(elements, summary);
                    summary.NumberOfBags++;
                    continue;
                }

                if (string.Equals(Constants.Airline, instructionType, StringComparison.InvariantCultureIgnoreCase))
                {
                    summary.NumberOfAirlinePassengers++;
                    summary.NumberOfBags++;
                    continue;
                }
                // each passenger will carry at least bag
                summary.NumberOfBags++;
                summary.NumberOfGeneralPassengers++;
            }

            summary.TotalNumberOfPassengers = summary.NumberOfGeneralPassengers + summary.NumberOfAirlinePassengers +
                                              summary.NumberOfLoyaltyPassengers;

            summary.TotalCostOfFlight = summary.TotalNumberOfPassengers * costPerPassenger;
            summary.UnadjustedTicketRevenue = summary.TotalNumberOfPassengers * ticketPrice;
            summary.AdjustedTicketRevenue = summary.UnadjustedTicketRevenue - (summary.LoyaltyPointsRedeemed + summary.NumberOfAirlinePassengers * ticketPrice);
            var actualOccupancy = 100d; // assuming actual occupancy 100%
            if (totalSeats > 0)
                actualOccupancy = ((double)summary.TotalNumberOfPassengers / totalSeats) * 100;
            summary.CanFlightProceed = (summary.AdjustedTicketRevenue > summary.TotalCostOfFlight)
                && (actualOccupancy > targetOccupancy)
                && (totalSeats >= summary.TotalNumberOfPassengers);
            return summary;
        }
 private static void ParseExtraBaggage(string[] elements, Summary summary)
 {
     if (elements.Length > 6)
     {
         // loyalty passenger allowed to carry an extra bag;
         var usingExtraBaggage = elements.ElementAt(6);
         if (usingExtraBaggage != null &&
             string.Equals("TRUE", usingExtraBaggage.Trim(), StringComparison.CurrentCultureIgnoreCase))
         {
             // passenger bringing extra baggage
             summary.NumberOfBags++;
         }
     }
 }