Exemplo n.º 1
0
        private static void SetupAirlineData()
        {
            FlightRoute londonToParis = new FlightRoute("London", "Paris")
            {
                BaseCost                 = 50,
                BasePrice                = 100,
                LoyaltyPointsGained      = 5,
                MinimumTakeOffPercentage = 0.7
            };

            _scheduledFlight = new ScheduledFlight(londonToParis);

            _scheduledFlight.SetAircraftForRoute(
                new Plane {
                Id = 123, Name = "Antonov AN-2", NumberOfSeats = 12
            });
        }
Exemplo n.º 2
0
        private static void Setup()
        {
            var londonToParis = new FlightRoute("London", "Paris")
            {
                BaseCost                 = 50,
                BasePrice                = 100,
                LoyaltyPointsGained      = 5,
                MinimumTakeOffPercentage = 0.7
            };

            _scheduledFlight = new ScheduledFlight(
                londonToParis,
                Container.GetInstance <ISummaryBuilder>(),
                Container.GetInstance <ISummaryOutputGenerator>(),
                Container.GetInstance <IFleetRepository>());

            _scheduledFlight.SetAircraftForRoute(123);
        }
        public void Profit_Surplus_Is_Nagative_And_Airline_Employees_Do_Not_Exceed_Minimum_Percentage()
        {
            var madridToPrague = new FlightRoute("Madrid", "Prague")
            {
                BaseCost                 = 80,
                BasePrice                = 150,
                LoyaltyPointsGained      = 10,
                MinimumTakeOffPercentage = 0.3
            };

            var scheduledFlight = new ScheduledFlight(madridToPrague);

            scheduledFlight.SetAircraftForRoute(new Plane {
                Id = 124, Name = "Vader Darth-87", NumberOfSeats = 10
            });

            scheduledFlight.Passengers.Add(new Passenger {
                Type = PassengerType.General, Name = "John", Age = 32, AllowedBags = 1
            });
            scheduledFlight.Passengers.Add(new Passenger {
                Type = PassengerType.Discounted, Name = "Jen", Age = 28, AllowedBags = 1
            });
            scheduledFlight.Passengers.Add(new Passenger {
                Type = PassengerType.LoyaltyMember, Name = "Mike", Age = 26, LoyaltyPoints = 1500, IsUsingLoyaltyPoints = true, AllowedBags = 2
            });
            scheduledFlight.Passengers.Add(new Passenger {
                Type = PassengerType.AirlineEmployee, Name = "Natalie", Age = 30, AllowedBags = 1
            });
            scheduledFlight.Passengers.Add(new Passenger {
                Type = PassengerType.Discounted, Name = "Chris", Age = 38, AllowedBags = 0
            });

            var costOfFlight = scheduledFlight.Passengers.Count * madridToPrague.BaseCost;

            var totalPriceForGeneral = scheduledFlight.Passengers.Where(p => p.Type == PassengerType.General).Count() * madridToPrague.BasePrice;
            var totalPriceForNonUsingLoyaltyPoints = scheduledFlight.Passengers.Where(p => p.Type == PassengerType.LoyaltyMember && p.IsUsingLoyaltyPoints == false).Count() * madridToPrague.BasePrice;
            var totalPriceForDiscounted            = (scheduledFlight.Passengers.Where(p => p.Type == PassengerType.Discounted).Count() * madridToPrague.BasePrice) / 2;

            var profitFromFlight = (totalPriceForGeneral + totalPriceForNonUsingLoyaltyPoints + totalPriceForDiscounted) - costOfFlight;

            var seatsTaken = scheduledFlight.Passengers.Count();

            Assert.False(scheduledFlight.CheckProfitSurplus(profitFromFlight));
        }
        public void Calculate_Loyalty_Points()
        {
            var madridToPrague = new FlightRoute("Madrid", "Prague")
            {
                BaseCost                 = 80,
                BasePrice                = 150,
                LoyaltyPointsGained      = 10,
                MinimumTakeOffPercentage = 0.5
            };

            var scheduledFlight = new ScheduledFlight(madridToPrague);

            scheduledFlight.SetAircraftForRoute(new Plane {
                Id = 124, Name = "Vader Darth-87", NumberOfSeats = 10
            });

            scheduledFlight.Passengers.Add(new Passenger {
                Type = PassengerType.General, Name = "John", Age = 32, AllowedBags = 1
            });
            scheduledFlight.Passengers.Add(new Passenger {
                Type = PassengerType.General, Name = "Jen", Age = 28, AllowedBags = 1
            });
            scheduledFlight.Passengers.Add(new Passenger {
                Type = PassengerType.General, Name = "Tom", Age = 47, AllowedBags = 1
            });
            scheduledFlight.Passengers.Add(new Passenger {
                Type = PassengerType.LoyaltyMember, Name = "Mike", Age = 26, LoyaltyPoints = 1500, IsUsingLoyaltyPoints = true, AllowedBags = 2
            });
            scheduledFlight.Passengers.Add(new Passenger {
                Type = PassengerType.LoyaltyMember, Name = "Pan", Age = 56, LoyaltyPoints = 500, IsUsingLoyaltyPoints = false, AllowedBags = 2
            });
            scheduledFlight.Passengers.Add(new Passenger {
                Type = PassengerType.AirlineEmployee, Name = "Natalie", Age = 30, AllowedBags = 1
            });
            scheduledFlight.Passengers.Add(new Passenger {
                Type = PassengerType.Discounted, Name = "Chris", Age = 38, AllowedBags = 0
            });

            var totalLoyaltyPointsAccrued  = scheduledFlight.Passengers.Where(p => p.Type == PassengerType.LoyaltyMember && p.IsUsingLoyaltyPoints == false).Count() * madridToPrague.LoyaltyPointsGained;
            var totalLoyaltyPointsRedeemed = scheduledFlight.Passengers.Where(p => p.Type == PassengerType.LoyaltyMember && p.IsUsingLoyaltyPoints == true).Count() * madridToPrague.BasePrice;;

            Assert.Equal(10, totalLoyaltyPointsAccrued);
            Assert.Equal(150, totalLoyaltyPointsRedeemed);
        }
        public void Minimum_Percentage_Exceeded()
        {
            List <Plane> planes = new List <Plane>();

            planes.Add(new Plane {
                Id = 123, Name = "Golden C3P-O", NumberOfSeats = 6
            });
            planes.Add(new Plane {
                Id = 124, Name = "Stormtrooper FN-11", NumberOfSeats = 8
            });

            var madridToPrague = new FlightRoute("Madrid", "Prague")
            {
                BaseCost                 = 80,
                BasePrice                = 150,
                LoyaltyPointsGained      = 10,
                MinimumTakeOffPercentage = 0.5
            };

            var scheduledFlight = new ScheduledFlight(madridToPrague);

            scheduledFlight.SetAircraftForRoute(planes[0]);

            scheduledFlight.Passengers.Add(new Passenger {
                Type = PassengerType.General, Name = "George", Age = 23, AllowedBags = 1
            });
            scheduledFlight.Passengers.Add(new Passenger {
                Type = PassengerType.General, Name = "Steph", Age = 25, AllowedBags = 1
            });
            scheduledFlight.Passengers.Add(new Passenger {
                Type = PassengerType.General, Name = "Alex", Age = 50, AllowedBags = 1
            });
            scheduledFlight.Passengers.Add(new Passenger {
                Type = PassengerType.LoyaltyMember, Name = "Luke", Age = 33, LoyaltyPoints = 1500, IsUsingLoyaltyPoints = true, AllowedBags = 2
            });

            var seatsTaken = scheduledFlight.Passengers.Count();

            Assert.True(scheduledFlight.CheckMinPercentageExceeded(seatsTaken));
        }
Exemplo n.º 6
0
        public FlightSummary Process(ScheduledFlight scheduledFlight, Passenger passenger)
        {
            var output = new FlightSummary();

            if (passenger.Type == PassengerType.LoyaltyMember)
            {
                if (passenger.IsUsingLoyaltyPoints)
                {
                    var loyaltyPointsRedeemed = Convert.ToInt32(Math.Ceiling(scheduledFlight.FlightRoute.BasePrice));
                    passenger.LoyaltyPoints           -= loyaltyPointsRedeemed;
                    output.TotalLoyaltyPointsRedeemed += loyaltyPointsRedeemed;
                }
                else
                {
                    output.TotalLoyaltyPointsAccrued += scheduledFlight.FlightRoute.LoyaltyPointsGained;
                    output.ProfitFromFlight          += scheduledFlight.FlightRoute.BasePrice;
                }

                output.TotalExpectedBaggage = 2;
            }

            return(output);
        }
Exemplo n.º 7
0
        public void Should_Choose_Bigger_Airplane()
        {
            var passengerService = new PassengerService();
            var aircraftService  = new AircraftService();

            aircraftService
            .AddPlanes(TestData.SmallAntonovPlane,
                       TestData.BombardierPlane,
                       TestData.ATRPlane);
            var calculationService = new CalculationService();

            calculationService.AddRules(new ProfitRule(), new RelaxedRule());
            var flightService   = new FlightService(TestData.LondonToParis);
            var scheduledFlight = new ScheduledFlight(
                flightService,
                passengerService,
                aircraftService,
                calculationService,
                new SummaryFormat(passengerService, aircraftService));

            scheduledFlight.SetAircraftForRoute(122);
            scheduledFlight.SetCurrentRule <ProfitRule>();

            scheduledFlight.AddPassenger(TestData.SteveGeneralPassenger);
            scheduledFlight.AddPassenger(TestData.MarkGeneralPassenger);
            scheduledFlight.AddPassenger(TestData.JamesGeneralPassenger);
            scheduledFlight.AddPassenger(TestData.JaneGeneralPassenger);
            scheduledFlight.AddPassenger(TestData.AlanGeneralPassenger);
            scheduledFlight.AddPassenger(TestData.SuzyGeneralPassenger);
            scheduledFlight.AddPassenger(TestData.AnthonyGeneralPassenger);
            scheduledFlight.AddPassenger(TestData.MikeGeneralPassenger);
            scheduledFlight.AddPassenger(TestData.JohnLoyaltyPassenger);
            scheduledFlight.AddPassenger(TestData.SarahLoyaltyPassenger);
            scheduledFlight.AddPassenger(TestData.JackLoyaltyPassenger);
            scheduledFlight.AddPassenger(TestData.TrevorAirlineEmployeePassenger);

            scheduledFlight.Process();
            _output.WriteLine(scheduledFlight.GetSummary());

            Assert.Equal(12, passengerService.GetCountForPassengerType <Passenger>());
            Assert.Equal(8, passengerService.GetCountForPassengerType <GeneralPassenger>());
            Assert.Equal(3, passengerService.GetCountForPassengerType <LoyaltyPassenger>());
            Assert.Equal(1, passengerService.GetCountForPassengerType <AirlineEmployeePassenger>());

            Assert.Equal(15, flightService.TotalExpectedBaggage);
            Assert.Equal(1000, flightService.ProfitFromFlight);
            Assert.Equal(600, flightService.CostOfFlight);
            Assert.Equal(10, flightService.TotalLoyaltyPointsAccrued);
            Assert.Equal(100, flightService.TotalLoyaltyPointsRedeemed);
            Assert.Equal(400, flightService.ProfitSurplus);
            Assert.Equal(
                @"Flight summary for London to Paris

Total passengers: 12
    General sales: 8
    Loyalty member sales: 3
    Airline employee comps: 1

Total expected baggage: 15

Total revenue from flight: 1000
Total costs from flight: 600
Flight generating profit of: 400

Total loyalty points given away: 10
Total loyalty points redeemed: 100


FLIGHT MAY NOT PROCEED
Other more suitable aircraft are:
Bombardier Q400 could handle this flight
ATR 640 could handle this flight
",
                scheduledFlight.GetSummary());
        }
Exemplo n.º 8
0
        public void Ensure_That_Original_Use_Case_Works()
        {
            var passengerService = new PassengerService();
            var aircraftService  = new AircraftService();

            aircraftService.AddPlanes(TestData.AntonovPlane);
            var calculationService = new CalculationService();

            calculationService.AddRules(new ProfitRule(), new RelaxedRule());
            var flightService   = new FlightService(TestData.LondonToParis);
            var scheduledFlight = new ScheduledFlight(
                flightService,
                passengerService,
                aircraftService,
                calculationService,
                new SummaryFormat(passengerService, aircraftService));

            scheduledFlight.SetAircraftForRoute(123);
            scheduledFlight.SetCurrentRule <ProfitRule>();

            scheduledFlight.AddPassenger(TestData.SteveGeneralPassenger);
            scheduledFlight.AddPassenger(TestData.MarkGeneralPassenger);
            scheduledFlight.AddPassenger(TestData.JamesGeneralPassenger);
            scheduledFlight.AddPassenger(TestData.JaneGeneralPassenger);
            scheduledFlight.AddPassenger(TestData.AlanGeneralPassenger);
            scheduledFlight.AddPassenger(TestData.SuzyGeneralPassenger);
            scheduledFlight.AddPassenger(TestData.JohnLoyaltyPassenger);
            scheduledFlight.AddPassenger(TestData.SarahLoyaltyPassenger);
            scheduledFlight.AddPassenger(TestData.JackLoyaltyPassenger);
            scheduledFlight.AddPassenger(TestData.TrevorAirlineEmployeePassenger);

            scheduledFlight.Process();


            Assert.Equal(10, passengerService.GetCountForPassengerType <Passenger>());
            Assert.Equal(6, passengerService.GetCountForPassengerType <GeneralPassenger>());
            Assert.Equal(3, passengerService.GetCountForPassengerType <LoyaltyPassenger>());
            Assert.Equal(1, passengerService.GetCountForPassengerType <AirlineEmployeePassenger>());

            Assert.Equal(13, flightService.TotalExpectedBaggage);
            Assert.Equal(800, flightService.ProfitFromFlight);
            Assert.Equal(500, flightService.CostOfFlight);
            Assert.Equal(10, flightService.TotalLoyaltyPointsAccrued);
            Assert.Equal(100, flightService.TotalLoyaltyPointsRedeemed);
            Assert.Equal(300, flightService.ProfitSurplus);
            Assert.Equal(
                @"Flight summary for London to Paris

Total passengers: 10
    General sales: 6
    Loyalty member sales: 3
    Airline employee comps: 1

Total expected baggage: 13

Total revenue from flight: 800
Total costs from flight: 500
Flight generating profit of: 300

Total loyalty points given away: 10
Total loyalty points redeemed: 100


THIS FLIGHT MAY PROCEED",
                scheduledFlight.GetSummary());
        }
        public (bool isValid, List <string> messages) Validate(FlightSummary flightSummary, ScheduledFlight scheduledFlight)
        {
            var isValid  = flightSummary.SeatsTaken < scheduledFlight.Aircraft.NumberOfSeats;
            var messages = new List <string>();

            if (!isValid)
            {
                var possiblePlanes = _planeRepository.GetSet()
                                     .Where(x => x.NumberOfSeats > flightSummary.SeatsTaken)
                                     .ToList();

                if (possiblePlanes.Count > 0)
                {
                    messages.Add("Other more suitable aircraft are:");

                    possiblePlanes.ForEach(p => {
                        messages.Add($"{p.Name} could handle this flight.");
                    });
                }
            }

            return(isValid, messages);
        }
Exemplo n.º 10
0
 public (bool isValid, List <string> messages) Validate(FlightSummary flightSummary, ScheduledFlight scheduledFlight)
 => (flightSummary.SeatsTaken / (double)scheduledFlight.Aircraft.NumberOfSeats > scheduledFlight.FlightRoute.MinimumTakeOffPercentage, null);
Exemplo n.º 11
0
        public async Task <IActionResult> Post([FromBody] ScheduledFlight model)
        {
            int?newScheduledFlightId = null;

            using (var connection = new SqlConnection(_connectionString))
            {
                await connection.OpenAsync();

                var transaction = connection.BeginTransaction();

                try
                {
                    var insertScheduledFlightSql = @"
INSERT INTO [dbo].[ScheduledFlight]
           ([FlightNumber]
           ,[DepartureAirportId]
           ,[DepartureHour]
           ,[DepartureMinute]
           ,[ArrivalAirportId]
           ,[ArrivalHour]
           ,[ArrivalMinute]
           ,[IsSundayFlight]
           ,[IsMondayFlight]
           ,[IsTuesdayFlight]
           ,[IsWednesdayFlight]
           ,[IsThursdayFlight]
           ,[IsFridayFlight]
           ,[IsSaturdayFlight])
     VALUES
           (@FlightNumber
           ,@DepartureAirportId
           ,@DepartureHour
           ,@DepartureMinute
           ,@ArrivalAirportId
           ,@ArrivalHour
           ,@ArrivalMinute
           ,@IsSundayFlight
           ,@IsMondayFlight
           ,@IsTuesdayFlight
           ,@IsWednesdayFlight
           ,@IsThursdayFlight
           ,@IsFridayFlight
           ,@IsSaturdayFlight);
SELECT CAST(SCOPE_IDENTITY() as int)";
                    newScheduledFlightId = await connection.ExecuteScalarAsync <int>(insertScheduledFlightSql, model, transaction);

                    model.Id = newScheduledFlightId.Value;
                    var flights = model.GenerateFlights(DateTime.Now, DateTime.Now.AddMonths(12));

                    var insertFlightsSql = @"INSERT INTO [dbo].[Flight]
           ([ScheduledFlightId]
           ,[Day]
           ,[ScheduledDeparture]
           ,[ActualDeparture]
           ,[ScheduledArrival]
           ,[ActualArrival])
     VALUES
           (@ScheduledFlightId
           ,@Day
           ,@ScheduledDeparture
           ,@ActualDeparture
           ,@ScheduledArrival
           ,@ActualArrival)";

                    await connection.ExecuteAsync(insertFlightsSql, flights, transaction);

                    throw new Exception("OH NOES!");
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    //Log the exception (ex)
                    try
                    {
                        transaction.Rollback();
                    }
                    catch (Exception ex2)
                    {
                        // Handle any errors that may have occurred
                        // on the server that would cause the rollback to fail, such as
                        // a closed connection.
                        // Log the exception ex2
                    }
                    return(StatusCode(500));
                }
            }
            return(Ok(newScheduledFlightId));
        }
Exemplo n.º 12
0
 public FlightServices(ScheduledFlight scheduledFlight)
 {
     this.scheduledFlight = scheduledFlight;
 }
Exemplo n.º 13
0
 public FlightBookingViewModel()
 {
     ScheduledFlight = new ScheduledFlight();
 }
Exemplo n.º 14
0
 public (bool isValid, List <string> messages) Validate(FlightSummary flightSummary, ScheduledFlight scheduledFlight)
 => (flightSummary.ProfitSurplus > 0, null);