def test04(self): hc = HybridCar('Toyota', 'Prius', 51.2, 7.8, 4.4, 25.8) self.assertTrue(isinstance(hc, Car)) self.assertTrue(isinstance(hc, HybridCar)) self.assertAlmostEqual(hc.get_battery_range(), 113.52) self.assertAlmostEqual(hc.get_total_range(), 512.88) self.assertEqual(str(hc), 'Toyota Prius, MPG: 51.2, tank capacity: 7.8, battery kWh: 4.4, miles/kWh: 25.8')
def setUp(self): self.car = Car() self.car1 = ElectricCar(1) self.car2 = PetrolCar(2) self.car3 = DieselCar(3) self.car4 = HybridCar(4) self.dealership = Dealership() self.dealership.create_current_stock()
def test_hybrid_car_fuel_cells(self): hybrid_car = HybridCar() self.assertEqual(1, hybrid_car.getNumberFuelCells()) hybrid_car.setNumberFuelCells(2) self.assertEqual(2, hybrid_car.getNumberFuelCells()) self.assertEqual(1, hybrid_car.getNumberCylinders()) hybrid_car.setNumberCylinders(2) self.assertEqual(2, hybrid_car.getNumberCylinders())
def test03(self): hc = HybridCar('Toyota', 'Prius', 50.2, 8.8, 4.4, 25.8) self.assertTrue(isinstance(hc, Car)) self.assertTrue(isinstance(hc, HybridCar)) self.assertAlmostEqual(hc.mpg, 50.2) self.assertAlmostEqual(hc.tank_capacity, 8.8) hc.mpg = 48 hc.tank_capacity = 9.1 self.assertAlmostEqual(hc.mpg, 48) self.assertAlmostEqual(hc.tank_capacity, 9.1) self.assertEqual(str(hc), 'Toyota Prius, MPG: 48, tank capacity: 9.1, battery kWh: 4.4, miles/kWh: 25.8')
class TestCar(unittest.TestCase): def setUp(self): self.car = Car() self.car1 = ElectricCar(1) self.car2 = PetrolCar(2) self.car3 = DieselCar(3) self.car4 = HybridCar(4) self.dealership = Dealership() self.dealership.create_current_stock() def test_car_mileage(self): self.assertEqual(0, self.car.getMileage()) self.car.move(15) # this function exists for all child classes of car self.assertEqual(15, self.car.getMileage()) def test_car_make(self): self.car.setMake( 'Ferrari') # this function exists for all child classes of car self.assertEqual('Ferrari', self.car.getMake()) self.assertEqual('Ferrari', self.car.getMake()) self.assertEqual('Tesla', self.car1.getMake()) self.assertEqual('Toyota', self.car2.getMake()) self.assertEqual('Volkswagon', self.car3.getMake()) self.assertEqual('Mitsubishi', self.car4.getMake()) def test_car_colour(self): self.assertEqual("Aungier Rental Red - Branded", self.car.getColour()) self.car.paint( 'red') # this function exists for all child classes of car self.assertEqual('red', self.car.getColour()) def test_car_engine(self): self.assertEqual('85-kWh', self.car1.getEngineSize()) self.assertEqual('1.6 ltr', self.car2.getEngineSize()) self.assertEqual('1.9 ltr', self.car3.getEngineSize()) self.assertEqual('2.0 ltr', self.car4.getEngineSize()) def test_rentalprocess( self ): # test the rental process by renting out one car and checking it back in. verifies stock levels also self.assertEqual(0, len(self.dealership.rented_cars)) self.assertEqual(8, len(self.dealership.hybrid_cars)) self.dealership.rent(self.dealership.hybrid_cars, 1, "Test") self.assertEqual(1, len(self.dealership.rented_cars)) self.assertEqual("Test", self.dealership.rented_cars[0][1]) self.assertEqual(7, len(self.dealership.hybrid_cars)) original_raw_input = __builtins__.raw_input __builtins__.raw_input = lambda _: "135" self.assertEqual(self.dealership.rented("Test"), None) __builtins__.raw_input = original_raw_input self.assertEqual(0, len(self.dealership.rented_cars)) self.assertEqual(8, len(self.dealership.hybrid_cars))
def process_rental(self): answer = str.lower(raw_input(' Would you like to rent a car? Yes: "Y", No: "N" : ')) if answer == 'y': answer = str.lower(raw_input(' Choose "p" for Petrol, "d" for Diesel, "e" for electric and "h" for Hybrid: ')) amount = int(raw_input(' How many would you like? ')) if answer == 'p': self.rent(self.petrol_cars, amount) choice = raw_input(' Write the model you would like: Toyota, Kia, Volkswagon, Ford: ') petrol_car = PetrolCar() petrol_car.setMake(choice) mileage = int(raw_input(' Record the mileage here before you start driving. ')) petrol_car.setMileage(mileage) print 'Your car is: ', petrol_car.getMake() print 'The current mileage is set at: ', petrol_car.getMileage() return mileage elif answer == 'd': self.rent(self.diesel_cars, amount) choice = raw_input(' Write the model you would like: Nissan or Hyundai: ') diesel_car = DieselCar() diesel_car.setMake(choice) mileage = int(raw_input(' Record the mileage here before you start driving. ')) diesel_car.setMileage(mileage) print 'Your car is: ', diesel_car.getMake() print 'The current mileage is set at: ', diesel_car.getMileage() return mileage elif answer == 'e': self.rent(self.electric_cars, amount) choice = raw_input(' We have a Nissan or a Tesla, please write your choice: ') electric_car = ElectricCar() electric_car.setMake(choice) mileage = int(raw_input(' Record your mileage here before you start driving. ')) electric_car.setMileage(mileage) print 'Your model is', electric_car.getMake() print 'Fuel cells available are', electric_car.getNumberFuelCells() print 'Your current mileage is set to: ', electric_car.getMileage() return mileage else: self.rent(self.hybrid_cars, amount) choice = raw_input(' what hybrid do you want, Prius or Lexus? ') hybrid_car = HybridCar() hybrid_car.setMake(choice) mileage = int(raw_input(' Record your mileage here before you start driving. ')) hybrid_car.setMileage(mileage) print 'Your model is', hybrid_car.getMake() print 'Our hybrid cars come with :', hybrid_car.getNumberFuelCells(), 'fuel cells and ', hybrid_car.getNumberCylinders(), 'cylinders ' print 'Your current mileage is set to: ', hybrid_car.getMileage() return mileage car_fleet = CarFleet() car_fleet.rentCar(amount) if answer == 'n': self.process_returnCar() self.stock_count()
class TestHybridCar(TestPetrolCar, TestElectricCar): def setUp(self): TestPetrolCar.setUp(self) TestElectricCar.setUp(self) self.hybridCar = HybridCar() def test_hybrid_default_constructor(self): self.assertEquals(False, self.hybridCar.regenerativeBrake) def test_hybrid_get_method(self): self.assertEquals(False, self.hybridCar.getHybridBrake()) def test_hybrid_set_method(self): self.hybridCar.setHybridBrake(True) self.assertEquals(True, self.hybridCar.getHybridBrake())
class TestHybridCar(TestPetrolCar, TestElectricCar): def setUp(self): self.car = HybridCar() # this tests the default constructor def test_hybrid_default_constructor(self): self.assertEqual(False, self.car.electric_brake_regeneration) # this tests the getter functionality def test_hybrid_getter(self): self.assertEqual(False, self.car.electric_brake_regeneration) # this tests the setter functionality def test_hybrid_setter(self): self.car.setBrake(True) self.assertEqual(True, self.car.getBrake())
def test_hybrid_car(self): car = HybridCar('Volksvagen', 'Passat', 'White', '01 P 1234', 1.5, 'MHEV') self.assertEqual(car.get_make(), 'Volksvagen') self.assertEqual(car.get_model(), 'Passat') self.assertEqual(car.get_colour(), 'White') self.assertEqual(car.get_registration(), '01 P 1234') self.assertEqual(car.get_engine_size(), 1.5) self.assertEqual(car.get_hybrid_type(), 'MHEV') self.assertEqual(car.get_is_rented(), False) self.assertEqual( str(car), 'Hybrid,Volksvagen,Passat,White,01 P 1234,False,1.5,,MHEV\n')
def create_current_stock(self): for i in range(24): self.petrol_cars.append(PetrolCar()) for i in range(8): self.diesel_cars.append(DieselCar()) for i in range(4): self.electric_cars.append(ElectricCar()) for i in range(4): self.hybrids.append(HybridCar())
def create_current_fleet(self): for i in range(6): self.electric_cars.append(ElectricCar()) for i in range(20): self.petrol_cars.append(PetrolCar()) for i in range(10): self.diesel_cars.append(DieselCar()) for i in range(4): self.hybrid_cars.append(HybridCar())
def __init__(self): self.__petrol_cars = [] self.__diesel_cars = [] self.__electric_cars = [] self.__hybrid_cars = [] self.__csv_header = '' for i in range(1, 21): self.__petrol_cars.append(PetrolCar()) for i in range(1, 11): self.__diesel_cars.append(DieselCar()) for i in range(1, 7): self.__electric_cars.append(ElectricCar()) for i in range(1, 5): self.__hybrid_cars.append(HybridCar())
def create_current_stock(self, typecar, start): #set up the initial stock for each type of cars # it has been called in the main (row 100) if typecar == 'E': for i in range(start): self.electric_cars.append(ElectricCar()) elif typecar == 'P': for i in range(start): self.petrol_cars.append(PetrolCar()) elif typecar == 'D': for i in range(start): self.diesel_cars.append(DieselCar()) elif typecar == 'H': for i in range(start): self.hybrid_cars.append(HybridCar())
def create_current_stock(self): self.overallTotal = 40 # total stock the dealer plans on having initially uniqueID = 100 # each car will have it's own unique ID, this is the starting integer # Making an assumption here that the dealer will maintain the current # ratio of 50% petrol, 20% diesel, 10% electric and 20% hybrid # this could be changed to fixed qtys of each # 'build' the fleet! for i in range(int(self.overallTotal * 0.5)): self.petrol_cars.append(PetrolCar(uniqueID)) uniqueID += 1 for i in range(int(self.overallTotal * 0.2)): self.diesel_cars.append(DieselCar(uniqueID)) uniqueID += 1 for i in range(int(self.overallTotal * 0.2)): self.hybrid_cars.append(HybridCar(uniqueID)) uniqueID += 1 for i in range(int(self.overallTotal * 0.1)): self.electric_cars.append(ElectricCar(uniqueID)) uniqueID += 1
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())
def returnCar(self, carType): # checks car type if carType == 'Petrol': # call checkRentals function to determine whether or not the car type is on rentals index = self.checkRentals('Petrol') # if car type is in rentals then remove it from rentals and add it to the associated array if index != -1: self.rental_cars.pop(index) self.petrol_cars.append(PetrolCar()) return 'You have returned the car' # else car type is not in rentals else: return 'No petrol cars currently on rental' elif carType == 'Diesel': index = self.checkRentals('Diesel') if index != -1: self.rental_cars.pop(index) self.petrol_cars.append(DieselCar()) return 'You have returned the car' else: return 'No diesel cars currently on rental' elif carType == 'Electric': index = self.checkRentals('Electric') if index != -1: self.rental_cars.pop(index) self.petrol_cars.append(ElectricCar()) return 'You have returned the car' else: return 'No electric cars currently on rental' elif carType == 'Hybrid': index = self.checkRentals('Hybrid') if index != -1: self.rental_cars.pop(index) self.petrol_cars.append(HybridCar()) return 'You have returned the car' else: return 'No hybrids currently on rental'
def setUp(self): self.car = HybridCar()
petrol = PetrolCar() petrol.setMake('Ford') petrol.setModel('Focus') petrol.setEngineSize('1.6') diesel = DieselCar() diesel.setMake('Renault') diesel.setModel('Clio') diesel.setEngineSize('1.5') electric = ElectricCar() electric.setMake('Nissan') electric.setModel('Leaf') electric.setNumberFuelCells('62KWH') hybrid = HybridCar() hybrid.setMake('Toyota') hybrid.setModel('Corolla') # the initial stock of cars def initial_cars_stock_csv(csv_file): type = ['Petrol', 'Diesel', 'Electric', 'Hybrid'] color = myCar.getColour() make = [ petrol.getMake(), diesel.getMake(), electric.getMake(), hybrid.getMake() ] model = [
def setUp(self): TestPetrolCar.setUp(self) TestElectricCar.setUp(self) self.hybridCar = HybridCar()
def test_hybrid_car_number_volt_cells(self): hybrid_car = HybridCar() self.assertEqual(1, hybrid_car.getNumberVoltCells()) hybrid_car.setNumberVoltCells(28) self.assertEqual(28, hybrid_car.getNumberVoltCells())
print "_________________________________________" #Electric car details. carE = ElectricCar() carE.setMake("Nissan Leaf") carE.setColour("Green") carE.setNumberFuelCells(4) carE.setMileage(450) print " Make of Electric car is :" + carE.getMake() print " Colour of the car is :" + carE.getColour() print " Number of fuel cells :" + str(carE.getNumberFuelCells()) print " Milegae of Electric car is :" + str(carE.getMileage()) print "_________________________________________" #Hybrid car details. carH = HybridCar() carH.setMake("Toyota Prius") carH.setColour("Yellow") carH.setNumberCylinders_Cells(4) carH.setMileage(1500) print " Make of Hybrid car is :" + carH.getMake() print " Colour of the car is :" + carH.getColour() print " Number of cylinders/ cells :" + str(carH.getNumberCylinders_Cells()) print " Milegae of Hybrid car is :" + str(carH.getMileage()) print "_________________________________________" print #DBS rental has fleet of 40 cars: 20 petrol, 8 diesel, 4 electric and 8 hybrid.
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)
class CarRentalApp(object): ## create car objects global car, 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.50 * fleet) availableDiesel = int(0.20 * fleet) availableElectric = int(0.10 * fleet) availableHybrid = int(0.20 * 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 = raw_input('Enter Customer name please: ') if name == 'q' or name == 'Q': quit() if name.isalpha() == False: print 'Sorry, Please try again.' continue customer.setName(name) print 'Type "H" for hire a car from the fleet.' print 'Type "R" for a car to the fleet.' global hireReturn hireReturn = raw_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 or returned?:' print 'Type "P" for petrol' print 'Type "D" for diesel' print 'Type "E" for electric' print 'Type "H" for hybrid' global carChoice carChoice = raw_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 'Sorry, there are none of this car type currently in the fleet.' print 'Please choose alternative car type.' continue break return carChoice 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 = raw_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 again.' 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 = raw_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 = raw_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 = raw_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 = raw_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 = raw_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 = raw_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 = raw_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): if hirePeriod <= 10: if (car.getModel() == 'Avensis') or (car.getModel() == 'Mondeo'): hireCharge = hirePeriod * 60.00 if (car.getModel() == 'Corolla') or (car.getModel() == 'Focus'): hireCharge = hirePeriod * 50.00 elif (10 < hirePeriod) and (hirePeriod <= 28): if (car.getModel() == 'Avensis') or (car.getModel() == 'Mondeo'): hireCharge = 500 + ((hirePeriod - 10) * 30.00) if (car.getModel() == 'Corolla') or (car.getModel() == 'Focus'): hireCharge = 400 + ((hirePeriod - 10) * 20.00) else: print "we shouldn't be here!" quit() return hireCharge fleetInfo() hireReturn = customerDetails() carInfo() 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() if hireReturn == 'r': print 'Thanks for returning the car.' 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', Car.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
def __load_hybrid_car(self, cols): # Create hybrid car from CSV line and add to hybrid car list. car = HybridCar(cols[1], cols[2], cols[3], cols[4], float(cols[6]), cols[8]) self.__load_car_rental_status(car, cols[5]) self.get_hybrid_cars().append(car)
def setUp(self): # The HybridCar calls itself using self. self.hybrid_car = HybridCar()
# Diesel car car3 = DieselCar() car3.setMake('Ford') car3.setModel('Mondeo') car3.setColour('Black') car3.setMileage(2500) car3.setNumberDieselCylinders(6) print 'Diesel car type: ' + car3.getMake() print 'Model: ' + car3.getModel() print 'Colour: ' + car3.getColour() print 'Number of cylinders: ' + str(car3.getNumberDieselCylinders()) print 'Mileage on the clock: ' + str(car3.getMileage()) print '------------------' # Hybrid car car4 = HybridCar() car4.setMake('Ford') car4.setModel('Fusion Hybrid') car4.setColour('Blue') car4.setMileage(600) car4.setNumberVoltCells(28) print 'Hybrid car type: ' + car4.getMake() print 'Model: ' + car4.getModel() print 'Colour: ' + car4.getColour() print 'Number of volt cells: ' + str(car4.getNumberVoltCells()) print 'Mileage on the clock: ' + str(car4.getMileage()) print '------------------' # DBS Rental Company with a fleet of 40 cars: 8 diesel, 8 hybrid, 4 electric and 20 petrol. class DBS_Car_Rental(object):
def test_hybrid_car_cylinders(self): hybrid_car = HybridCar() self.assertEqual(1, hybrid_car.getNumberCylinders()) hybrid_car.setNumberCylinders(4) self.assertEqual(4, hybrid_car.getNumberCylinders())
def test_hybrid_car_fuel_cells(self): hybrid_car = HybridCar() self.assertEqual(1, hybrid_car.getNumberFuelCells()) hybrid_car.setNumberFuelCells(4) self.assertEqual(4, hybrid_car.getNumberFuelCells())
car2.move(20) print('The mileage is ' + str(car2.getMileage())) + 'kms' car2.engineSize = '2.1' print 'The engine size is ' + car2.engineSize car2.setNumberOfFuelCells(2) print('The number of fuel cells is ' + str(car2.getNumberOfFuelCells())) # A HybridCar Class is called by car3 allowing it to use the functions and variables of the class. A hybrid is # 1 part standard motor car and 1 part electric car so it will use a cylinder and a cell function. The make # takes in the value Toyota Prius and prints a message to the user. The colour is set at blue and printed. # Same functionality as other mileage in this case 100 is the value that is set, next 17 is added using move # resulting in a value of 117kms. The engineSize variable is set as 3.1. Hybrid cars require both cells and # cylinders so it will use both functions. car3 = HybridCar() car3.setMake('Toyota Prius') print '\nOur hybrid option is ' + car3.getMake() car3.setColour('Blue') print('The colour is ' + car3.getColour()) car3.setMileage(100) car3.move(17) print('The mileage is ' + str(car3.getMileage())) + 'kms' car3.engineSize = '3.1' print 'The engine size is ' + car3.engineSize car3.setNumberOfFuelCells(2) print('The number of fuel cells is ' + str(car3.getNumberOfFuelCells())) car3.setNumberOfFuelCylinders(1)
class TestHybridCar(unittest.TestCase): def setUp(self): # The HybridCar calls itself using self. self.hybrid_car = HybridCar() def test_HybridCar_fuel_cylinders(self): # Similar to petrol and diesel except default value is 1. The default value is tested as well as 4, 6 # and 3 which all result in true. self.assertEqual(1, self.hybrid_car.getNumberOfFuelCylinders()) self.hybrid_car.setNumberOfFuelCylinders(4) self.assertEqual(4, self.hybrid_car.getNumberOfFuelCylinders()) self.hybrid_car.setNumberOfFuelCylinders(6) self.assertEqual(6, self.hybrid_car.getNumberOfFuelCylinders()) self.hybrid_car.setNumberOfFuelCylinders(3) self.assertEqual(3, self.hybrid_car.getNumberOfFuelCylinders()) def test_HybridCar_fuel_cells(self): # Similar to ElectricCar test_car_fuel_cells. The default value 1 is tested as well as 4 and 2. self.assertEqual(1, self.hybrid_car.getNumberOfFuelCells()) self.hybrid_car.setNumberOfFuelCells(4) self.assertEqual(4, self.hybrid_car.getNumberOfFuelCells()) self.hybrid_car.setNumberOfFuelCells(2) self.assertEqual(2, self.hybrid_car.getNumberOfFuelCells()) def test_HybridCar_mileage_and_move(self): # As in other cases testing the mileage default value. Then entering a value for move to ensure # it moves that amount to test the mileage of a car. The car is moved by 7.7 to make 19.7 moved 0 # output stays the same and lastly setMileage is tested with a value of 15 which is true. self.assertEqual(0, self.hybrid_car.getMileage()) self.hybrid_car.move(12) self.assertEqual(12, self.hybrid_car.getMileage()) self.hybrid_car.move(7.7) self.assertEqual(19.7, self.hybrid_car.getMileage()) self.hybrid_car.move(0) self.assertEqual(19.7, self.hybrid_car.getMileage()) self.hybrid_car.setMileage(15) self.assertEqual(15, self.hybrid_car.getMileage()) def test_HybridCar_make(self): # Testing make function for HybridCar. self.assertEqual(' ', self.hybrid_car.getMake()) self.hybrid_car.setMake('Chevrolet Malibu') self.assertEqual('Chevrolet Malibu', self.hybrid_car.getMake()) self.hybrid_car.setMake('Chevrolet Cruz') self.assertEqual('Chevrolet Cruz', self.hybrid_car.getMake()) def test_HybridCar_engineSize(self): # Testing engineSize for HybridCar. self.assertEqual(' ', self.hybrid_car.getEngineSize()) self.hybrid_car.setEngineSize(1.6) self.assertEqual(1.6, self.hybrid_car.getEngineSize()) self.hybrid_car.setEngineSize(1.8) self.assertEqual(1.8, self.hybrid_car.getEngineSize()) def test_HybridCar_colour_and_paint(self): # Testing colour and paint for HybridCar. self.assertEqual(' ', self.hybrid_car.getColour()) self.hybrid_car.paint('red') self.assertEqual('red', self.hybrid_car.getColour()) self.hybrid_car.setColour('light blue') self.assertEqual('light blue', self.hybrid_car.getColour())