Esempio n. 1
0
 public Result Apply(ScheduledFlightContext ruleContext)
 {
     return(ruleContext.ScheduledFlightSummary.SalesPerPassengerType[PassengerType.AirlineEmployee] >
            (double)ruleContext.Aircraft.NumberOfSeats * ruleContext.FlightRoute.MinimumTakeOffPercentage
         ? Result.Ok()
         : Result.Fail("Airline employees less than minimum percentage of passengers required"));
 }
 public ScheduledFlightValidationContext ValidateFlight(ScheduledFlightContext scheduledFlightContext)
 {
     return(new ScheduledFlightValidationContext
     {
         ValidationResult = _ruleSetProcessor.ApplyRuleSet(scheduledFlightContext)
     });
 }
        public string GenerateOutput(ScheduledFlightContext scheduledFlightContext)
        {
            var result = new StringBuilder();

            result.Append("Flight summary for " + scheduledFlightContext.FlightRoute.Title);

            result.Append(VERTICAL_WHITE_SPACE);

            result.Append("Total passengers: " + scheduledFlightContext.ScheduledFlightSummary.SeatsTaken);

            result.Append(NEW_LINE);

            foreach (var salesPerPassengerType in scheduledFlightContext.ScheduledFlightSummary.SalesPerPassengerType)
            {
                result.Append(INDENTATION + salesPerPassengerType.Key + " sales: " + salesPerPassengerType.Value + NEW_LINE);
            }

            result.Append(VERTICAL_WHITE_SPACE);
            result.Append("Total expected baggage: " + scheduledFlightContext.ScheduledFlightSummary.TotalExpectedBaggage);

            result.Append(VERTICAL_WHITE_SPACE);

            result.Append("Total revenue from flight: " + scheduledFlightContext.ScheduledFlightSummary.RevenueFromFlight);
            result.Append(NEW_LINE);
            result.Append("Total costs from flight: " + scheduledFlightContext.ScheduledFlightSummary.CostOfFlight);
            result.Append(NEW_LINE);

            var profitSurplus = scheduledFlightContext.ScheduledFlightSummary.RevenueFromFlight - scheduledFlightContext.ScheduledFlightSummary.CostOfFlight;

            result.Append((profitSurplus > 0
                              ? "Flight generating profit of: "
                              : "Flight losing money of: ") + profitSurplus);

            result.Append(VERTICAL_WHITE_SPACE);

            result.Append("Total loyalty points given away: " + scheduledFlightContext.ScheduledFlightSummary.TotalLoyaltyPointsAccrued + NEW_LINE);
            result.Append("Total loyalty points redeemed: " + scheduledFlightContext.ScheduledFlightSummary.TotalLoyaltyPointsRedeemed + NEW_LINE);

            result.Append(VERTICAL_WHITE_SPACE);

            var flightValidation = _scheduledFlightValidator.ValidateFlight(scheduledFlightContext);

            if (flightValidation.ValidationResult.IsSuccess)
            {
                result.Append(FlightMayProceed());

                return(result.ToString());
            }

            result.Append(FlightMayNotProceed(flightValidation.ValidationResult.Error));

            if (scheduledFlightContext.ScheduledFlightSummary.Overbooked)
            {
                AddAvailableAircraftsInformation(result, scheduledFlightContext.Aircraft.Id,
                                                 scheduledFlightContext.ScheduledFlightSummary.SeatsTaken);
            }

            return(result.ToString());
        }
        public Result Apply(ScheduledFlightContext ruleContext)
        {
            var orRules = _orRules.Select(orRule => orRule.Apply(ruleContext)).ToList();

            return(ruleContext.ScheduledFlightSummary.RevenueFromFlight > ruleContext.ScheduledFlightSummary.CostOfFlight || orRules.Any(r => r.IsSuccess)
                ? Result.Ok()
                : Result.Fail("Not profitable"));
        }
Esempio n. 5
0
        public Result ApplyRuleSet(ScheduledFlightContext ruleContext)
        {
            var ruleSet = _ruleSetFactory.GetRuleSet(ruleContext.RuleSetType);

            var results = ruleSet.GetRules().Select(rule => rule.Apply(ruleContext)).ToList();

            return(results.Any(r => r.IsFailure)
                ? Result.Fail(string.Join(",", results.Where(r => r.IsFailure).Select(r => r.Error)))
                : Result.Ok());
        }
        public Result Apply(ScheduledFlightContext ruleContext)
        {
            var seatsAvailable = ruleContext.ScheduledFlightSummary.SeatsTaken < ruleContext.Aircraft.NumberOfSeats;

            if (seatsAvailable)
            {
                return(Result.Ok());
            }

            ruleContext.ScheduledFlightSummary.Overbooked = true;

            return(Result.Fail("overbooked"));
        }
        public string GetSummary(RuleSetType ruleSetType)
        {
            var scheduledFlightSummary = _summaryBuilder.BuildSummary(FlightRoute, Aircraft, Passengers);

            var scheduledFlightContext = new ScheduledFlightContext()
            {
                Aircraft               = Aircraft,
                FlightRoute            = FlightRoute,
                RuleSetType            = ruleSetType,
                ScheduledFlightSummary = scheduledFlightSummary
            };

            return(_summaryOutputGenerator.GenerateOutput(scheduledFlightContext));
        }
Esempio n. 8
0
        public bool RuleEvaluationReturnsExpectedResultWhen(int numberOfSeats, int seatsTaken, double minTakeOffPercentage)
        {
            var ruleContext = new ScheduledFlightContext()
            {
                Aircraft = AnyAircraft(numberOfSeats),
                ScheduledFlightSummary = new ScheduledFlightSummary()
                {
                    SeatsTaken = seatsTaken
                },
                FlightRoute = AnyFlightRoute(minTakeOffPercentage)
            };

            var result = _rule.Apply(ruleContext);

            return(result.IsSuccess);
        }
        public bool RuleEvaluationReturnsExpectedResultWhen(int numberOfSeats, int airlineEmployeesSeatsTaken, double minTakeOffPercentage)
        {
            var scheduledFlightSummary = new ScheduledFlightSummary();

            scheduledFlightSummary.SalesPerPassengerType[PassengerType.AirlineEmployee] = airlineEmployeesSeatsTaken;

            var ruleContext = new ScheduledFlightContext()
            {
                Aircraft = AnyAircraft(numberOfSeats),
                ScheduledFlightSummary = scheduledFlightSummary,
                FlightRoute            = AnyFlightRoute(minTakeOffPercentage)
            };

            var result = _rule.Apply(ruleContext);

            return(result.IsSuccess);
        }
Esempio n. 10
0
        public bool RuleEvaluationReturnsExpectedResultWithoutOrRulesWhen(double profitFromFlight, double costOfFlight)
        {
            var rule = new RevenueExceedsCostRule(Enumerable.Empty <IRule>());

            var ruleContext = new ScheduledFlightContext()
            {
                ScheduledFlightSummary = new ScheduledFlightSummary()
                {
                    RevenueFromFlight = profitFromFlight,
                    CostOfFlight      = costOfFlight
                }
            };

            var result = rule.Apply(ruleContext);

            return(result.IsSuccess);
        }
Esempio n. 11
0
        public bool RuleEvaluationReturnsExpectedResultWithOrRulesWhen(double profitFromFlight, double costOfFlight)
        {
            var orRule = Substitute.For <IRule>();

            orRule.Apply(Arg.Any <ScheduledFlightContext>()).Returns(Result.Ok());

            var rule = new RevenueExceedsCostRule(new List <IRule>()
            {
                orRule
            });

            var ruleContext = new ScheduledFlightContext()
            {
                ScheduledFlightSummary = new ScheduledFlightSummary()
                {
                    RevenueFromFlight = profitFromFlight,
                    CostOfFlight      = costOfFlight
                }
            };

            var result = rule.Apply(ruleContext);

            return(result.IsSuccess);
        }
 public Result Apply(ScheduledFlightContext ruleContext)
 {
     return(ruleContext.ScheduledFlightSummary.SeatsTaken / (double)ruleContext.Aircraft.NumberOfSeats > ruleContext.FlightRoute.MinimumTakeOffPercentage
         ? Result.Ok()
         : Result.Fail("Minimum percentage of passengers booked is not met"));
 }