示例#1
0
    def test_returnCar_for_invalid_rentalTime(self):
        # create a shop and a customer
        shop = CarRental(10)
        customer = Customer()

        # let the customer not rent a bike a try to return one.
        request = customer.returnCar()
        self.assertIsNone(shop.returnCar(request))

        # manually check return function with error values
        self.assertIsNone(shop.returnCar((0, 0, 0)))
    def test_returnCar_for_valid_credentials(self):

        # create a shop and a various customers
        shop = CarRental(50)
        customer1 = Customer()
        customer2 = Customer()
        customer3 = Customer()
        customer4 = Customer()
        customer5 = Customer()
        customer6 = Customer()

        # create valid rentalBasis for each customer
        customer1.rentalBasis = 1  # hourly
        customer2.rentalBasis = 1  # hourly
        customer3.rentalBasis = 2  # daily
        customer4.rentalBasis = 2  # daily
        customer5.rentalBasis = 3  # weekly
        customer6.rentalBasis = 3  # weekly

        # create valid bikes for each customer
        customer1.cars = 1
        customer2.cars = 5  # eligible for family discount 30%
        customer3.cars = 2
        customer4.cars = 8
        customer5.cars = 15
        customer6.cars = 30

        # create past valid rental times for each customer

        customer1.rentalTime = datetime.now() + timedelta(hours=-4)
        customer2.rentalTime = datetime.now() + timedelta(hours=-23)
        customer3.rentalTime = datetime.now() + timedelta(days=-4)
        customer4.rentalTime = datetime.now() + timedelta(days=-13)
        customer5.rentalTime = datetime.now() + timedelta(weeks=-6)
        customer6.rentalTime = datetime.now() + timedelta(weeks=-12)

        # make all customers return their bikes
        request1 = customer1.returnCar()
        request2 = customer2.returnCar()
        request3 = customer3.returnCar()
        request4 = customer4.returnCar()
        request5 = customer5.returnCar()
        request6 = customer6.returnCar()

        # check if all of them get correct bill
        self.assertEqual(shop.returnCar(request1), 20)
        self.assertEqual(shop.returnCar(request2), 402.5)
        self.assertEqual(shop.returnCar(request3), 160)
        self.assertEqual(shop.returnCar(request4), 2080)
        self.assertEqual(shop.returnCar(request5), 5400)
        self.assertEqual(shop.returnCar(request6), 21600)
示例#3
0
    def test_returnCar_for_invalid_rentalBasis(self):
        # create a shop and a customer
        shop = CarRental(10)
        customer = Customer()

        # create valid rentalTime and cars
        customer.rentalTime = datetime.now()
        customer.cars = 3

        # create invalid rentalbasis
        customer.rentalBasis = 8

        request = customer.returnCar()
        self.assertEqual(shop.returnCar(request), 0)
示例#4
0
    def test_returnCar_for_invalid_numOfCars(self):

        # create a shop and a customer
        shop = CarRental(10)
        customer = Customer()

        # create valid rentalTime and rentalBasis
        customer.rentalTime = datetime.now()
        customer.rentalBasis = 1

        # create invalid cars
        customer.cars = 0

        request = customer.returnCar()
        self.assertIsNone(shop.returnCar(request))
def main():
    shop = CarRental(100)
    customer = Customer()

    while True:
        print("""
        ====== Car Rental Shop =======
        1. Display available cars
        2. Request a car on hourly basis $5
        3. Request a car on daily basis $20
        4. Request a car on weekly basis $60
        5. Return a car
        6. Exit
        """)
        
        choice = input("Enter choice: ")
        
        try:
            choice = int(choice)
        except ValueError:
            print("That's not an int!")
            continue
        
        if choice == 1:
            shop.displaystock()
        
        elif choice == 2:
            customer.rentalTime = shop.rentCarOnHourlyBasis(customer.requestCar())
            customer.rentalBasis = 1

        elif choice == 3:
            customer.rentalTime = shop.rentCarOnDailyBasis(customer.requestCar())
            customer.rentalBasis = 2

        elif choice == 4:
            customer.rentalTime = shop.rentCarOnWeeklyBasis(customer.requestCar())
            customer.rentalBasis = 3

        elif choice == 5:
            customer.bill = shop.returnCar(customer.returnCar())
            customer.rentalBasis, customer.rentalTime, customer.cars = 0,0,0        
        elif choice == 6:
            break
        else:
            print("Invalid input. Please enter number between 1-6 ")        
    print("Thank you for using the car rental system.")
示例#6
0
 def test_rentCarOnWeeklyBasis_for_invalid_positive_number_of_cars(self):
     shop = CarRental(10)
     self.assertEqual(shop.rentOnWeeklyBasis(11), None)
示例#7
0
 def test_rentCarOnWeeklyBasis_for_valid_positive_number_of_cars(self):
     shop = CarRental(10)
     hour = datetime.now().hour
     self.assertEqual(shop.rentOnWeeklyBasis(2).hour, hour)
示例#8
0
 def test_rentCarOnWeeklyBasis_for_zero_number_of_cars(self):
     shop = CarRental(10)
     self.assertEqual(shop.rentOnWeeklyBasis(0), None)
示例#9
0
 def test_rentCarOnDailyBasis_for_negative_number_of_cars(self):
     shop = CarRental(10)
     self.assertEqual(shop.rentOnDailyBasis(-1), None)
示例#10
0
    def test_returnCar_for_valid_credentials(self):

        # create a shop and a various customers
        shop = CarRental(50)
        customer1 = Customer()
        customer2 = Customer()
        customer3 = Customer()
        customer4 = Customer()
        customer5 = Customer()
        customer6 = Customer()
        customer7 = Customer()
        customer8 = Customer()

        # create valid rentalBasis for each customer
        customer1.rentalBasis = 1  # hourly
        customer2.rentalBasis = 1  # hourly
        customer3.rentalBasis = 2  # daily
        customer4.rentalBasis = 2  # daily
        customer5.rentalBasis = 3  # weekly
        customer6.rentalBasis = 3  # weekly
        customer7.rentalBasis = 4  # monthly
        customer8.rentalBasis = 4  # monthly

        # create valid cars for each customer
        customer1.cars = 1
        customer2.cars = 5
        customer3.cars = 2
        customer4.cars = 8
        customer5.cars = 15
        customer6.cars = 30
        customer7.cars = 4
        customer8.cars = 10

        # create past valid rental times for each customer

        customer1.rentalTime = datetime.now() + timedelta(hours=-4)
        customer2.rentalTime = datetime.now() + timedelta(hours=-23)
        customer3.rentalTime = datetime.now() + timedelta(days=-4)
        customer4.rentalTime = datetime.now() + timedelta(days=-13)
        customer5.rentalTime = datetime.now() + timedelta(weeks=-6)
        customer6.rentalTime = datetime.now() + timedelta(weeks=-12)
        customer5.rentalTime = datetime.now() + timedelta(months=-2)
        customer5.rentalTime = datetime.now() + timedelta(months=-4)

        # make all customers return their cars
        request1 = customer1.returnCar()
        request2 = customer2.returnCar()
        request3 = customer3.returnCar()
        request4 = customer4.returnCar()
        request5 = customer5.returnCar()
        request6 = customer6.returnCar()
        request7 = customer7.returnCar()
        request8 = customer8.returnCar()

        # check if all of them get correct bill
        self.assertEqual(shop.returnCar(request1), 80)
        self.assertEqual(shop.returnCar(request2), 2300)
        self.assertEqual(shop.returnCar(request3), 1200)
        self.assertEqual(shop.returnCar(request4), 15600)
        self.assertEqual(shop.returnCar(request5), 58500)
        self.assertEqual(shop.returnCar(request6), 234000)
        self.assertEqual(shop.returnCar(request7), 58500)
        self.assertEqual(shop.returnCar(request8), 234000)
示例#11
0
 def __init__(self):
     self.__car_rental = CarRental()
示例#12
0
class CarRentalApp(object):

    ## create car objects
    global car, engineCar, petrolCar, dieselCar, electricCar, hybridCar, carRental
    car = Car()
    engineCar = EngineCar()
    petrolCar = PetrolCar()
    dieselCar = DieselCar()
    electricCar = ElectricCar()
    hybridCar = HybridCar()
    carRental = CarRental()

    ## create customer object
    global customer
    customer = Customer()

    def fleetInfo():
        ## construct car fleet
        carRental.setFleet(40)
        fleet = carRental.getFleet()
        availablePetrol = int(0.60 * fleet)
        availableDiesel = int(0.20 * fleet)
        availableElectric = int(0.10 * fleet)
        availableHybrid = int(0.10 * fleet)

        carRental.setAvailablePetrolCars([])
        carRental.setAvailableDieselCars([])
        carRental.setAvailableElectricCars([])
        carRental.setAvailableHybridCars([])
        for p in range(1, availablePetrol + 1):
            carRental.availablePetrolCars.append(petrolCar)
        for d in range(1, availableDiesel + 1):
            carRental.availableDieselCars.append(dieselCar)
        for e in range(1, availableElectric + 1):
            carRental.availableElectricCars.append(electricCar)
        for h in range(1, availableHybrid + 1):
            carRental.availableHybridCars.append(hybridCar)

    def customerDetails():

        ## print quit message
        print('Type "q" to quit this program')

        ## obtain input form user
        while True:
            name = input('Please enter Customer name: ')
            if name == 'q' or name == 'Q':
                quit()
            ## check user did not just press return button
            ## and all characters are alphabetic
            ## note: unfortunately the isalpha method will not work in cases
            ## whereby some african names contain an exclamation mark
            if name.isalpha() == False:
                print('Sorry, name not recognised.  Please try again.')
                continue
            customer.setName(name)
            licence = input('Please enter Customer driving licence number: ')
            if licence == 'q' or customer.licence == 'Q':
                quit()
            if licence == '':
                print('Sorry, licence not valid.  Please try again.')
                continue
            customer.setLicence(licence)
            print('Type "H" if Customer wishes to hire a car from the fleet.')
            print('Type "R" if Customer is returning a car to the fleet.')
            global hireReturn
            hireReturn = input('Hire or Return?: ')
            if hireReturn == 'q':
                quit()
            hireReturn = hireReturn.lower()
            if (hireReturn != 'h') and (hireReturn != 'r'):
                print('Invalid input.  Please try again.')
                continue
            break
        return hireReturn

    def carInfo():
        ## obtain car choice
        while True:
            print('What type of car is being hired/returned?:')
            print('Type "P" for petrol')
            print('Type "D" for diesel')
            print('Type "E" for electric')
            print('Type "H" for hybrid')
            global carChoice
            carChoice = input('Enter car type now: ')
            carChoice = carChoice.lower()
            if carChoice == 'q':
                quit()
            carChoiceOptions = ['p', 'd', 'e', 'h']
            ## check for valid carChoice
            if carChoice not in carChoiceOptions:
                print('Invalid car choice.  Please try agsin.')
                continue
            ## check if car choice available
            if hireReturn == 'h':
                if ( (carChoice == 'p') and (len(carRental.getAvailablePetrolCars()) <= 0) ) \
                or ( (carChoice == 'd') and (len(carRental.getAvailableDieselCars()) <= 0) ) \
                or ( (carChoice == 'e') and (len(carRental.getAvailableElectricCars()) <= 0) ) \
                or ( (carChoice == 'h') and (len(carRental.getAvailableHybridCars()) <= 0) ):
                    print(
                        'Apologies, there are none of this car type currently in the fleet.'
                    )
                    print('Please choose alternative car type.')
                    continue
            break
        return carChoice

    def transmissionInfo():
        ##obtain transmission info
        while True:
            if hireReturn == 'h':
                print(
                    'Does Customer prefer an Automatic or a Manual transmission?:'
                )
                print('Type "A" for Automatic')
                print('Type "M" for Manual')
                transmissionChoice = input('Enter transmission preference: ')
                transmissionChoice = transmissionChoice.lower()
                if transmissionChoice == 'q':
                    quit()
                transmissionChoiceOptions = ['a', 'm']
                ## check for valid transmissionChoice
                if transmissionChoice not in transmissionChoiceOptions:
                    print('Invalid transmission choice.  Please try agsin.')
                    continue
                if transmissionChoice == 'a':
                    engineCar.setTransmission('Automatic')
                if transmissionChoice == 'm':
                    engineCar.setTransmission('Manual')
            break

    def carMakeInfo():
        ##obtain car make info
        while True:
            if hireReturn == 'h':
                print('Does Customer prefer a Ford or a Toyota?:')
                print('Type "F" for Ford')
                print('Type "T" for Toyota')
                makeChoice = input('Enter Make preference: ')
                makeChoice = makeChoice.lower()
                if makeChoice == 'q':
                    quit()
                makeChoiceOptions = ['f', 't']
                ## check for valid makeChoice
                if makeChoice not in makeChoiceOptions:
                    print('Invalid Make choice.  Please try agsin.')
                    continue
                if makeChoice == 'f':
                    car.setMake('Ford')
                if makeChoice == 't':
                    car.setMake('Toyota')
            break

    def carModelInfo():
        ##obtain car model info
        while True:
            if car.getMake() == 'Ford':
                print('Does Customer prefer a Focus or a Mondeo?:')
                print('Type "F" for Focus')
                print('Type "M" for Mondeo')
                modelChoice = input('Enter Model preference: ')
                modelChoice = modelChoice.lower()
                if modelChoice == 'q':
                    quit()
                modelChoiceOptions = ['f', 'm']
                ## check for valid modelChoice
                if modelChoice not in modelChoiceOptions:
                    print('Invalid Model choice.  Please try agsin.')
                    continue
                if modelChoice == 'f':
                    car.setModel('Focus')
                if modelChoice == 'm':
                    car.setModel('Mondeo')
            break
        while True:
            if car.getMake() == 'Toyota':
                print('Does Customer prefer a Corolla or an Avensis?:')
                print('Type "C" for Corolla')
                print('Type "A" for Avensis')
                modelChoice = input('Enter Model preference: ')
                modelChoice = modelChoice.lower()
                if modelChoice == 'q':
                    quit()
                modelChoiceOptions = ['c', 'a']
                ## check for valid modelChoice
                if modelChoice not in modelChoiceOptions:
                    print('Invalid make choice.  Please try agsin.')
                    continue
                if modelChoice == 'c':
                    car.setModel('Corolla')
                if modelChoice == 'a':
                    car.setModel('Avensis')
            break

    def petrolCarInfo():
        ##obtain fuel info
        while True:
            if carChoice == 'p':
                print('Does Customer have a fuel preference?:')
                print('Type "L" for leaded')
                print('Type "U" for unleaded')
                fuelChoice = input('Enter Customer choice now: ')
                fuelChoice = fuelChoice.lower()
                if fuelChoice == 'q':
                    quit()
                fuelChoiceOptions = ['l', 'u']
                ## check for valid fuelChoice
                if fuelChoice not in fuelChoiceOptions:
                    print('Invalid fuel choice.  Please try agsin.')
                    continue
                if fuelChoice == 'l':
                    petrolCar.setPetrolType('Leaded')
                if fuelChoice == 'u':
                    petrolCar.setPetrolType('Unleaded')
            break

    def dieselCarInfo():
        ##obtain turbo info
        while True:
            if carChoice == 'd':
                print('Does Customer prefer standard or turbo?:')
                print('Type "S" for standard')
                print('Type "T" for turbo')
                turboChoice = input('Enter Customer choice now: ')
                turboChoice = turboChoice.lower()
                if turboChoice == 'q':
                    quit()
                turboChoiceOptions = ['s', 't']
                ## check for valid turboChoice
                if turboChoice not in turboChoiceOptions:
                    print('Invalid turbo choice.  Please try agsin.')
                    continue
                if turboChoice == 's':
                    dieselCar.setDieselTurbo(False)
                if turboChoice == 't':
                    dieselCar.setDieselTurbo(True)
            break

    def electricCarInfo():
        ##obtain fuelCells info
        while True:
            if carChoice == 'e':
                print('Does Customer prefer battery or capacitor?:')
                print('Type "B" for battery')
                print('Type "C" for capacitor')
                fuelCellsChoice = input('Enter Customer choice now: ')
                fuelCellsChoice = fuelCellsChoice.lower()
                if fuelCellsChoice == 'q':
                    quit()
                fuelCellsChoiceOptions = ['b', 'c']
                ## check for valid fuelCellsChoice
                if fuelCellsChoice not in fuelCellsChoiceOptions:
                    print('Invalid fuel cell choice.  Please try agsin.')
                    continue
                if fuelCellsChoice == 'b':
                    electricCar.setFuelCells('Battery')
                if fuelCellsChoice == 'c':
                    electricCar.setFuelCells('Capacitor')
            break

    def hybridCarInfo():
        ##obtain brake info
        while True:
            if carChoice == 'h':
                print(
                    'Does Customer prefer standard or regenerative braking system?:'
                )
                print('Type "S" for standard brake system')
                print('Type "R" for regenerative brake system')
                brakeChoice = input('Enter Customer choice now: ')
                brakeChoice = brakeChoice.lower()
                if brakeChoice == 'q':
                    quit()
                brakeChoiceOptions = ['s', 'r']
                ## check for valid brakeChoice
                if brakeChoice not in brakeChoiceOptions:
                    print('Invalid brake choice.  Please try agsin.')
                    continue
                if brakeChoice == 's':
                    hybridCar.setHybridBrake(False)
                if brakeChoice == 'r':
                    hybridCar.setHybridBrake(True)
            break

    def updateFleet(carChoice, hireReturn):
        ## remove/add one (1) car to/from fleet
        fleet = carRental.getFleet()
        if carChoice == 'p':
            if (hireReturn == 'h'):
                carRental.availablePetrolCars.pop()
                fleet -= 1
            elif (hireReturn == 'r'):
                carRental.availablePetrolCars.append(petrolCar)
                fleet += 1
            else:
                print('Hell has just frozen over.  Petrol debug necessary!')
                quit()
        elif carChoice == 'd':
            if (hireReturn == 'h'):
                carRental.availableDieselCars.pop()
                fleet -= 1
            elif (hireReturn == 'r'):
                carRental.availableDieselCars.append(dieselCar)
                fleet += 1
            else:
                print('Hell has just frozen over.  Diesel debug necessary!')
                quit()
        elif carChoice == 'e':
            if (hireReturn == 'h'):
                carRental.availableElectricCars.pop()
                fleet -= 1
            elif (hireReturn == 'r'):
                carRental.availableElectricCars.append(electricCar)
                fleet += 1
            else:
                print('Hell has just frozen over.  Electric debug necessary!')
                quit()
        elif carChoice == 'h':
            if (hireReturn == 'h'):
                carRental.availableHybridCars.pop()
                fleet -= 1
            elif (hireReturn == 'r'):
                carRental.availableHybridCars.append(hybridCar)
                fleet += 1
            else:
                print('Hell has just frozen over.  Hybrid debug necessary!')
                quit()
        else:
            print('Woah ... back up the apple-cart.  Debug required now!')
            quit()

    def calculateHirePeriod(hireReturn):
        while True:
            print('How many days does Customer require?')
            print('Number must be between 1 and 28 (inclusive).')
            hirePeriod = input('Please enter number of days: ')
            if hirePeriod == 'q':
                quit()
            try:
                hirePeriod = int(hirePeriod)
                if (1 <= hirePeriod) and (hirePeriod <= 28):
                    break
                else:
                    print('Hire period must be between 1 and 28.')
            except:
                print("Invalid input.  Try again.")
        return hirePeriod

    def calculateHireCharge(hireReturn, hirePeriod):
        ## hire rates for large cars (i.e. Avensis & Mondeo):
        ## are 50 euro per day for the first 10 days
        ## and 35 euro per day thereafter
        ## hire rates for small cars (i.e. Corolla & Focus):
        ## are 40 euro per day for the first 10 days
        ## and 25 euro per day thereafter
        if hirePeriod <= 10:
            if (car.getModel() == 'Avensis') or (car.getModel() == 'Mondeo'):
                hireCharge = hirePeriod * 50.00
            if (car.getModel() == 'Corolla') or (car.getModel() == 'Focus'):
                hireCharge = hirePeriod * 40.00
        elif (10 < hirePeriod) and (hirePeriod <= 28):
            if (car.getModel() == 'Avensis') or (car.getModel() == 'Mondeo'):
                hireCharge = 500 + ((hirePeriod - 10) * 35.00)
            if (car.getModel() == 'Corolla') or (car.getModel() == 'Focus'):
                hireCharge = 400 + ((hirePeriod - 10) * 25.00)
        else:
            print("Oops! ... we shouldn't be here!")
            quit()
        return hireCharge

    fleetInfo()
    hireReturn = customerDetails()
    carInfo()
    transmissionInfo()
    carMakeInfo()
    carModelInfo()
    petrolCarInfo()
    dieselCarInfo()
    electricCarInfo()
    hybridCarInfo()
    availableCars = updateFleet(carChoice, hireReturn)

    if hireReturn == 'h':
        customer.hirePeriod = calculateHirePeriod(hireReturn)
        customer.hireCharge = calculateHireCharge(hireReturn,
                                                  customer.hirePeriod)

    print('Customer name:\t\t\t', customer.getName())
    print('Customer licence:\t\t', customer.getLicence())
    if hireReturn == 'r':
        print('Thanks for returning the car.')
        print('Hope to see you again soom.')
    if hireReturn == 'h':
        print('Make of car hired:\t\t', car.getMake())
        print('Model of car hired:\t\t', car.getModel())
        print('Transmission of car hired:\t', engineCar.getTransmission())
        if carChoice == 'p':
            print('Petrol type:\t\t\t', petrolCar.getPetrolType())
        if carChoice == 'd':
            print('Has turbo?:\t\t\t', dieselCar.getDieselTurbo())
        if carChoice == 'e':
            print('Fuel cell type:\t\t\t', electricCar.getFuelCells())
        if carChoice == 'h':
            print('Regenerative braking system:\t', hybridCar.getHybridBrake())
        print('Hire period (days):\t\t', customer.hirePeriod)
        print('Hire charge (euro):\t\t', customer.hireCharge)
示例#13
0
 def test_car_rental(self):
     # Load
     car_rental = CarRental()
     car_rental.load_current_stock('test_CarRentalStock.csv')
     self.assertEqual(len(car_rental.get_diesel_cars()), 10)
     self.assertEqual(len(car_rental.get_electric_cars()), 6)
     self.assertEqual(len(car_rental.get_hybrid_cars()), 4)
     self.assertEqual(len(car_rental.get_petrol_cars()), 20)
     # Renting
     car_rental.rent_cars('p', 3)
     rented_cars = len([
         1 for car in car_rental.get_petrol_cars()
         if car.get_is_rented() == True
     ])
     self.assertEqual(
         rented_cars, 3,
         'The petrol cars list has not been updated correctly with the # of rented cars.'
     )
     # Returns validation
     self.assertTrue(car_rental.is_rented_car_registration(
         'p', '01 P 1002'))
     self.assertFalse(
         car_rental.is_rented_car_registration('p', '06 G 1002'))
     # Returns
     car_rental.return_validated_cars('p', ['01 P 1002', '01 P 1003'])
     rented_cars = len([
         1 for car in car_rental.get_petrol_cars()
         if car.get_is_rented() == True
     ])
     self.assertEqual(
         rented_cars, 1,
         'The returned cars have not been updated correctly in the petrol cars list.'
     )
 def setUp(self):
     self.carRental = CarRental()
class TestCarRental(unittest.TestCase):

    def setUp(self):
        self.carRental = CarRental()

    def test_default_constructor(self):
        self.assertEquals(0, self.carRental.fleet)
        self.assertEquals([], self.carRental.availablePetrolCars)
        self.assertEquals([], self.carRental.availableDieselCars)
        self.assertEquals([], self.carRental.availableElectricCars)
        self.assertEquals([], self.carRental.availableHybridCars)
        
    def test_car_rental_get_methods(self):
        self.assertEquals(0, self.carRental.getFleet())
        self.assertEquals([], self.carRental.getAvailablePetrolCars())
        self.assertEquals([], self.carRental.getAvailableDieselCars())
        self.assertEquals([], self.carRental.getAvailableElectricCars())
        self.assertEquals([], self.carRental.getAvailableHybridCars())

    def test_car_rental_set_methods(self):
        self.carRental.setFleet(40)
        self.assertEquals(40, self.carRental.getFleet())
        petrolCar = PetrolCar()
        dieselCar = DieselCar()
        electricCar = ElectricCar()
        hybridCar = HybridCar()
        self.carRental.setAvailablePetrolCars([petrolCar, petrolCar])
        self.assertEquals([petrolCar, petrolCar], self.carRental.getAvailablePetrolCars())
        self.carRental.setAvailableDieselCars([dieselCar, dieselCar, dieselCar])
        self.assertEquals([dieselCar, dieselCar, dieselCar], self.carRental.getAvailableDieselCars())
        self.carRental.setAvailableElectricCars([electricCar, electricCar])
        self.assertEquals([electricCar, electricCar], self.carRental.getAvailableElectricCars())
        self.carRental.setAvailableHybridCars([hybridCar, hybridCar])
        self.assertEquals([hybridCar, hybridCar], self.carRental.getAvailableHybridCars())
示例#16
0
 def test_Car_Rental_displays_correct_stock(self):
     shop1 = CarRental()
     shop2 = CarRental(10)
     self.assertEqual(shop1.displaystock(), 0)
     self.assertEqual(shop2.displaystock(), 10)
示例#17
0
class TestCarRental(unittest.TestCase):
    def setUp(self):
        self.carRental = CarRental()

    def test_default_constructor(self):
        self.assertEquals(0, self.carRental.fleet)
        self.assertEquals([], self.carRental.availablePetrolCars)
        self.assertEquals([], self.carRental.availableDieselCars)
        self.assertEquals([], self.carRental.availableElectricCars)
        self.assertEquals([], self.carRental.availableHybridCars)

    def test_car_rental_get_methods(self):
        self.assertEquals(0, self.carRental.getFleet())
        self.assertEquals([], self.carRental.getAvailablePetrolCars())
        self.assertEquals([], self.carRental.getAvailableDieselCars())
        self.assertEquals([], self.carRental.getAvailableElectricCars())
        self.assertEquals([], self.carRental.getAvailableHybridCars())

    def test_car_rental_set_methods(self):
        self.carRental.setFleet(40)
        self.assertEquals(40, self.carRental.getFleet())
        petrolCar = PetrolCar()
        dieselCar = DieselCar()
        electricCar = ElectricCar()
        hybridCar = HybridCar()
        self.carRental.setAvailablePetrolCars([petrolCar, petrolCar])
        self.assertEquals([petrolCar, petrolCar],
                          self.carRental.getAvailablePetrolCars())
        self.carRental.setAvailableDieselCars(
            [dieselCar, dieselCar, dieselCar])
        self.assertEquals([dieselCar, dieselCar, dieselCar],
                          self.carRental.getAvailableDieselCars())
        self.carRental.setAvailableElectricCars([electricCar, electricCar])
        self.assertEquals([electricCar, electricCar],
                          self.carRental.getAvailableElectricCars())
        self.carRental.setAvailableHybridCars([hybridCar, hybridCar])
        self.assertEquals([hybridCar, hybridCar],
                          self.carRental.getAvailableHybridCars())
示例#18
0
 def setUp(self):
     self.carRental = CarRental()
示例#19
0
class CarRentalApp():
    def __init__(self):
        self.__car_rental = CarRental()

    def __show_banner(self):
        # Display the application banner.
        print('#' * 30)
        print('Welcome to DBS Car Rental')
        print('#' * 30, '\n')

    def __show_main_options(self):
        # Show the first level user options
        print('Select from the following options ...')
        option_format = '\t{}\t{}'
        print(option_format.format(1, 'Rent'))
        print(option_format.format(2, 'Return'))
        print(option_format.format('q', 'Quit'))

    def __show_car_type_options(self):
        # Show the options of car types for selection.
        print('Select from the following options ...')
        option_format = '\t{}\t{}'
        print(option_format.format('d', 'Diesel'))
        print(option_format.format('e', 'Electric'))
        print(option_format.format('h', 'Hybrid'))
        print(option_format.format('p', 'Petrol'))
        print(option_format.format('q', 'Quit'))

    def __get_user_options(self, valid_options):
        # Validate user input is from a allowed set of characters.
        input_invalid = True
        valid_options = list(valid_options)
        while input_invalid:
            user_input = input('Enter your option: ').lower()
            if user_input in valid_options:
                input_invalid = False
            else:
                print("'{}' is not a valid option.".format(user_input))
        return user_input

    def __get_cars_amount(self, car_type):
        # Get from user how many cars are rented.
        rentable_amount = self.__car_rental.get_rentable_amount_by_type(
            car_type)
        print('The # of cars available for rent - {}'.format(rentable_amount))
        input_invalid = True
        while input_invalid:
            amount = input(
                'Please enter a number for the amount or 0 to quit: ')
            if amount.isdigit():
                amount = int(amount)
                if rentable_amount < amount:
                    print('There are not enough cars to rent.')
                else:
                    input_invalid = False
            else:
                print("'{}' is not a valid amount.".format(amount))
        return amount

    def __show_registrations(self, registrations):
        # Show the list of rented/returned cars.
        for reg in registrations:
            print('\t{}'.format(reg))

    def __do_renting(self):
        # Manages the rental process.
        print('Which type of cars do you want to rent?')
        self.__show_car_type_options()
        car_type = self.__get_user_options('dehpq')
        if car_type == 'q':
            return
        else:
            amount = self.__get_cars_amount(car_type)
            if amount == 0:
                return
            else:
                registrations = self.__car_rental.rent_cars(car_type, amount)
                print('The following cars have been rented.')
                self.__show_registrations(registrations)

    def __get_return_registrations(self, car_type):
        # Gets the registrations of returned cars from the user.
        # Registrations are checked for validity on input.
        print('Please enter the returned car registrations.')
        print('\tEnter d when Done or q to Quit.')
        registrations = []
        get_registrations = True
        while get_registrations == True:
            registration = input('Car registration: ')
            if registration.lower() == 'q':
                registrations.clear()
                get_registrations = False
            elif registration.lower() == 'd':
                get_registrations = False
            elif self.__car_rental.is_rented_car_registration(
                    car_type, registration):
                registrations.append(registration)
            else:
                print("'{}' is not a valid car registration.".format(
                    registration))
        return registrations

    def __do_returns(self):
        # Manages the returns process.
        print('Which type of cars do you want to return?')
        self.__show_car_type_options()
        car_type = self.__get_user_options('dehpq')
        if car_type == 'q':
            return
        else:
            registrations = self.__get_return_registrations(car_type)
            if len(registrations) > 0:
                self.__car_rental.return_validated_cars(
                    car_type, registrations)
                print('The following cars have been returned.')
                self.__show_registrations(registrations)

    def __run_user_interface(self):
        # Run the inteeractive application.
        self.__show_banner()
        keep_running = True
        while keep_running:
            self.__show_main_options()
            option = self.__get_user_options('12q')
            if option == '1':
                self.__do_renting()
            elif option == '2':
                self.__do_returns()
            else:
                keep_running = False
        print('Sorry to see you leave. Goodbye.')

    def run_application(self):
        # Loads the CarRental class and on completion of application run
        # saves current status of stock back to the CSV file.
        csv_file_name = 'CarRentalStock.csv'
        self.__car_rental.load_current_stock(csv_file_name)
        self.__run_user_interface()
        self.__car_rental.save_current_stock(csv_file_name)