Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter rental data");
            Console.Write("Car model: ");
            string model = Console.ReadLine();

            Console.Write("Pickup (dd/MM/yyyy hh:mm): ");
            DateTime start = DateTime.ParseExact(Console.ReadLine(), "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);

            Console.Write("Return (dd/MM/yyyy hh:mm): ");
            DateTime finish = DateTime.ParseExact(Console.ReadLine(), "dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);

            CarRental carRental = new CarRental(start, finish, new Vehicle(model));

            Console.Write("Enter price per day: ");
            double day = double.Parse(Console.ReadLine());

            Console.Write("Enter price per hour: ");
            double hour = double.Parse(Console.ReadLine());

            CarRentalService carRentalService = new CarRentalService(hour, day, new BrazilTaxService());

            carRentalService.ProcessInvoice(carRental);

            Console.WriteLine("INVOICE:");
            Console.WriteLine(carRental.Invoice);
            Console.ReadLine();
        }
        public async void ReturnCar_AddsRentalReturn_ForExistingBookings()
        {
            await using var context = new RentalCarsDbContext(ContextOptions);

            var service  = new CarRentalService(new Mock <IRentalPriceCalculator>().Object, context);
            var customer = context.Customers.First();

            var booking =
                await AddBooking(context, context.Cars.First(), "2021-05-05", "2021-06-05");

            var returnDate   = DateTime.Parse("2021-06-01");
            var rentalReturn =
                await service.ReturnCar(new ReturnCarModel(booking.BookingNumber, customer.Email, returnDate, 100.0f));

            Assert.True(context.Returns.Contains(rentalReturn));
        }
        public async void FindAvailableCars_ExcludesCars_IfDateRangeOverlapsWithAnotherBooking(string bookingStartDate, string bookingEndDate)
        {
            await using var context = new RentalCarsDbContext(ContextOptions);

            var service  = new CarRentalService(new Mock <IRentalPriceCalculator>().Object, context);
            var firstCar = context.Cars.First();

            await AddBooking(context, firstCar, bookingStartDate, bookingEndDate);

            var startDate   = DateTime.Parse("2021-04-10");
            var endDate     = DateTime.Parse("2021-05-10");
            var currentDate = DateTime.Parse("2021-04-10");

            var availableCars =
                await service.FindAvailableCars(firstCar.Category, startDate, endDate, currentDate);

            Assert.DoesNotContain(availableCars, car => car.Id == firstCar.Id);
        }
        public async void FindAvailableCars_ReturnsCars_AvailableForTheBookingRange(string bookingStartDate, string bookingEndDate)
        {
            await using var context = new RentalCarsDbContext(ContextOptions);

            var service  = new CarRentalService(new Mock <IRentalPriceCalculator>().Object, context);
            var firstCar = context.Cars.First();

            await AddBooking(context, firstCar, bookingStartDate, bookingEndDate);

            var startDate   = DateTime.Parse("2021-04-30");
            var endDate     = DateTime.Parse("2021-05-09");
            var currentDate = DateTime.Parse("2021-04-10");

            var availableCars =
                await service.FindAvailableCars(firstCar.Category, startDate, endDate, currentDate);

            Assert.Single(availableCars);
            Assert.Contains(availableCars, car => car.Id == firstCar.Id);
        }
        public async void RentCar_ThrowsNonAvailable_IfDateRangeOverlapsWithAnotherBooking(string bookingStartDate, string bookingEndDate)
        {
            await using var context = new RentalCarsDbContext(ContextOptions);

            var service  = new CarRentalService(new Mock <IRentalPriceCalculator>().Object, context);
            var firstCar = context.Cars.First();
            var customer = context.Customers.First();

            await AddBooking(context, firstCar, bookingStartDate, bookingEndDate);

            var startDate   = DateTime.Parse("2021-04-10");
            var endDate     = DateTime.Parse("2021-05-10");
            var currentDate = DateTime.Parse("2021-04-04");

            async Task RentCarFn() =>
            await service.RentCar(new RentCarModel(
                                      Guid.NewGuid(), firstCar.Id, customer.Email, "ABC", startDate, endDate), currentDate);

            await Assert.ThrowsAsync <CarNotAvailable>(RentCarFn);
        }
        public async void RentCar_RegistersBooking_For_Available_Car()
        {
            await using var context = new RentalCarsDbContext(ContextOptions);

            var service  = new CarRentalService(new Mock <IRentalPriceCalculator>().Object, context);
            var firstCar = context.Cars.First();
            var customer = context.Customers.First();

            var startDate   = DateTime.Parse("2021-05-05");
            var endDate     = DateTime.Parse("2021-06-05");
            var currentDate = DateTime.Parse("2021-05-04");

            await service.RentCar(new RentCarModel(
                                      Guid.NewGuid(), firstCar.Id, customer.Email, "ABC", startDate, endDate), currentDate);

            var booking = context.Bookings.First();

            Assert.Equal(firstCar.Id, booking.CarId);
            Assert.Equal(customer.Id, booking.CustomerId);
            Assert.Equal(startDate, booking.StartDate);
            Assert.Equal(endDate, booking.EndDate);
            Assert.Equal("ABC", booking.BookingNumber);
        }
        public async void ReturnCar_CalculatesReturnPriceCorrectly_ForRentalPeriodAndMileage()
        {
            await using var context = new RentalCarsDbContext(ContextOptions);

            var car = context.Cars.First();

            var priceCalculator = new Mock <IRentalPriceCalculator>();

            priceCalculator
            .Setup(x => x.CalculatePrice(car.Category, 12, 89.3f))
            .Returns(345.0m);

            var service  = new CarRentalService(priceCalculator.Object, context);
            var customer = context.Customers.First();

            var booking =
                await AddBooking(context, car, "2021-05-05", "2021-06-05");

            var returnDate   = DateTime.Parse("2021-05-17");
            var rentalReturn =
                await service.ReturnCar(new ReturnCarModel(booking.BookingNumber, customer.Email, returnDate, 89.3f));

            Assert.Equal(345.0m, rentalReturn.Price);
        }
 public CarRentalController(CarRentalService service)
 {
     this.service = service;
 }