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()
class TestElectric(unittest.TestCase): def setUp(self): self.electric = ElectricCar() def test_electric_engineSize(self): self.assertEqual('1', self.electric.getNumberFuelCells()) self.electric.setNumberFuelCells('2') self.assertEqual('2', self.electric.getNumberFuelCells())
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 main(): my_tesla = ElectricCar('tesla', 'model s', 2016) print(my_tesla.get_descriptive_name()) my_tesla.battery.describe_battery() my_tesla.battery.get_range() my_tesla.battery.upgrade_battery() my_tesla.battery.get_range() """导入整个模块,使用语法module_name.class_name访问需要的类""" my_beetle = car.Car('volkswagen', 'beetle', 2016) print(my_beetle.get_descriptive_name())
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 TestElectricCar(TestCar): def setUp(self): TestCar.setUp(self) self.electricCar = ElectricCar() def test_electric_default_constructor(self): self.assertEquals('', self.electricCar.fuelCells) def test_electric_get_method(self): self.assertEquals('', self.electricCar.getFuelCells()) def test_electric_set_method(self): self.electricCar.setFuelCells('Battery') self.assertEquals('Battery', self.electricCar.getFuelCells())
class TestElectricCar(TestCar): def setUp(self): self.car = ElectricCar() # this tests the default constructor def test_electric_default_constructor(self): self.assertEqual(0, self.car.fuel_cells) # this tests the getter functionality def test_electric_getter(self): self.assertEqual(0, self.car.fuel_cells) # this tests the setter functionality def test_electric_setter(self): self.car.setFuelCells(2) self.assertEqual(2, self.car.getFuelCells())
def test_electric_car(self): car = ElectricCar('Volksvagen', 'Passat', 'White', '01 P 1234', 2) 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_number_fuel_cells(), 2) self.assertEqual(car.get_is_rented(), False) self.assertEqual( str(car), 'Electric,Volksvagen,Passat,White,01 P 1234,False,,2,\n')
def test_electric_car_charge(): ec = ElectricCar(100) assert ec.drive(70) == 70 assert ec.drive(50) == 30 assert ec.drive(50) == 0 ec.charge() assert ec.drive(50) == 50
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 prob_9_9(): my_ecar = ElectricCar('tesla', 'model s', 2019, 75) print(f"|{my_ecar.longname()}|") my_ecar.update_odometer(123) my_ecar.print_odometer() my_ecar.battery.upgrade_battery() print(f"|{my_ecar.longname()}|")
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'
#从一个模块中导入多个类 from car import Car, ElectricCar my_beetle = Car("volkswagen", "beetle", 2016) print(my_beetle.get_descriptive_name()) my_tesla = ElectricCar("tesla", "roadster", 2016) print(my_tesla.get_descriptive_name()) print("\n") #导入整个模块 import car my_beetle = car.Car("wolkswagen", "beetle", 2016) print(my_beetle.get_descriptive_name()) my_tesla = car.Car("tesla", "roadster", 2016) print(my_tesla.get_descriptive_name()) print("\n") #导入模块中的所有类 from car import * my_beetle = Car("wolkswagen", "beetle", 2016) print(my_beetle.get_descriptive_name()) my_tesla = ElectricCar("tesla", "roadster", 2016) print(my_tesla.get_descriptive_name())
def test_electric_car_fuel_cells(self): electric_car = ElectricCar() self.assertEqual(1, electric_car.getNumberFuelCells()) electric_car.setNumberFuelCells(4) self.assertEqual(4, electric_car.getNumberFuelCells())
class ElectricCar(Car): def __init__(self, make, model, year): ''' 电动汽车的独特之处 初始化父类的属性,再初始化电动汽车的特有属性 ''' # 注意继承父类的__init__方法时不用self形参 super().__init__(make, model, year) self.battery_size = 70 def describe_battery(self): '''打印描述电瓶容量的消息''' print("This car has a " + str(self.battery_size) + "-kwh battery.") my_tesla = ElectricCar('tesla', 'model s', 2016) print(my_tesla.get_descriptive_name()) my_tesla.describe_battery() # 重写父类的方法:对于父类的方法,只要不符合子类模拟的实物的行为,都可对其进行重写 # 可在子类中定义一个这样的方法,与它要重写的父类中的方法同名 # 将实例用作属性:可将大型类拆分为许多协同工作的小类 class Car(): def __init__(self, make, model, year): '''初始化描述汽车的属性''' self.make = make self.model = model self.year = year self.odometer_reading = 0
# The move function passes the value 15 to the Car class and in the move function adds it to the value held in # mileage this is used to create the new mileage figure and is printed to the screen in kilometers # The default value for engineSize is ' '. For the red_car I am assigning it the value 3.7. The value of # engineSize can be changed to any number without changing the Car class. red_car.move(15) print 'The mileage is ' + str(red_car.getMileage()) + 'kms' red_car.engineSize = '3.7' print 'The engine size is ' + red_car.engineSize # A ElectricCar object is called by car2 and can now use the functions of an ElectricCar class. Similar functionality # as red_car in make function. # Unlike the red_car which used the paint function here I am using the setColour # function which takes in self and colour as variables in this case while. The getColour function returns the value # held in colour. In this case the value of colour was changed from ' ' to White which is displayed to the user. car2 = ElectricCar() car2.setMake('Nissan LEAF') print '\nOur environment friendly option is ' + car2.getMake() car2.setColour('White') print('The colour is ' + car2.getColour()) # Similar functionality in mileage, move and engineSize except in this case Mileage is at 300 instead of the # default value of 0. This results in the mileage starting at 300 and becoming 320 after the move function. # Unlike petrol and diesel cars which have fuel cylinders electric cars require fuel cells to run. The function # setNumberOfFuelCells takes in 2 values self and the value for fuel cells in this case 2. The next line prints # an appropriate message and uses the getNumberOfFuelCells which returns the value held in the variable which is 2. car2.setMileage(300) car2.move(20) print('The mileage is ' + str(car2.getMileage())) + 'kms' car2.engineSize = '2.1'
def create_current_stock(self): for i in range(20): self.electric_cars.append(ElectricCar()) for i in range(10): self.petrol_cars.append(PetrolCar())
def dealership_init(): red_car = Car() print('Colour ' + red_car.getColour()) print('Mileage ' + str(red_car.getMileage())) print('Make ' + red_car.getMake()) red_car.setMake('Ferrari') print('Make ' + red_car.getMake()) print('Getting a paint job - the new colour is ' + red_car.paint('red')) print('Colour ' + red_car.getColour()) print('Car moved' + str(red_car.move(15)) + 'kms') print('Mileage ' + str(red_car.getMileage())) print('Engine Size ' + red_car.engineSize) red_car.engineSize = '3.9' print('Engine Size ' + red_car.engineSize) car3 = ElectricCar() car3.setColour('white') car3.setMileage(500) car3.setNumberFuelCells(2) car3.move(20) print('Colour ' + car3.getColour()) print('Number of fuel cells ' + str(car3.getNumberFuelCells()))
from car import Car, ElectricCar my_beetle = Car('volkswagen','beetle', 2016) print(my_beetle.get_descriptive_name()) my_tesla = ElectricCar('tesla','roadster', 2016) print(my_tesla.get_descriptive_name())
def setUp(self): TestCar.setUp(self) self.electricCar = ElectricCar()
from car import ElectricCar my_tesla = ElectricCar('tesla', 'model s', 2018) print(my_tesla.get_c_name()) my_tesla.battery.describe_battery() my_tesla.battery.get_range()
from car import ElectricCar my_tesla = ElectricCar('tesla', 'models', 2016) print(my_tesla.get_descriptive_name()) my_tesla.battery.describe_battery() my_tesla.fill_gas_tank() my_tesla.battery.get_range()
def setUp(self): self.electric_car = ElectricCar()
#Dushyant Tara(21-06-2020):This program helps you import classes from modules from car import Car, ElectricCar #or import the whole module using #import car then use car.Car or car.ElectricCar syntax my_beetle = Car('volkswagen','beetle',2016) print(my_beetle.get_descriptive_name()) my_tesla = ElectricCar('tesla','roadster',2016) print(my_tesla.get_descriptive_name())
#print my_car.__colour #Will not print "same reason in line 27 print my_car.getMileage() #print my_car.__mileage #Will not print "same reason in line 27 my_car.engineSize = ("2.5L") print my_car.engineSize my_car.setMake("BMW") print my_car.getMake() my_car.setColour("Silver") print my_car.getColour() my_car.setMileage(50) print my_car.getMileage() my_car.move(10) print my_car.getMileage() my_car.paint("Blue") print my_car.getColour() electric_car = ElectricCar() #Class ElectricCar or electric_car print electric_car.getNumberFuelCells() electric_car.move(20) print electric_car.getMileage() #my_car.getNumberFuelCells() #error bc my_car does not have NumberofFuels
"""descriable""" from car import ElectricCar my_tesla = ElectricCar('tesla', 'model s', 2016) print(my_tesla.get_descriptive_name()) my_tesla.read_odometer() my_tesla.battery.describe_battery()
##this module was created to import Car2 from the car.py module. from car import ElectricCar my_tesla = ElectricCar("tesla", "model s", 2016) print(my_tesla.get_descriptive_name()) my_tesla.battery.describe_battery() my_tesla.battery.get_range()
from car import ElectricCar my_tesla = ElectricCar('tesla', "model s", 2016) my_tesla.battery.describe_battery()
#!/usr/bin/env python # -*- coding: utf-8 -*- # # my_electric_car.py # # Copyright 2017 c5220056 <c5220056@GMPTIC> # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. # # from car import ElectricCar my_tesla = ElectricCar('tesla', 'model s', 2016) print(my_tesla.get_descriptive_name()) my_tesla.battery.describe_battery() my_tesla.battery.get_range()
class TestElectricCar(unittest.TestCase): # The ElectricCar calls itself using self. def setUp(self): self.electric_car = ElectricCar() def test_electricCar_fuel_cells(self): # The default value for an electric car is called from the ElectricCar class which is 2 which is tested to # make sure the base class ElectricCar functions are being correctly implemented. The setNumberOfFuelCells # is used to change the default value from 2 to 8, this is tested and it has become 8 so is true. 5 and 7 # are then placed as testing values for getNumberofFuelCells. Note set doesn't change default values held # in base class. self.assertEqual(2, self.electric_car.getNumberOfFuelCells()) self.electric_car.setNumberOfFuelCells(8) self.assertEqual(8, self.electric_car.getNumberOfFuelCells()) self.electric_car.setNumberOfFuelCells(5) self.assertEqual(5, self.electric_car.getNumberOfFuelCells()) self.electric_car.setNumberOfFuelCells(7) self.assertEqual(7, self.electric_car.getNumberOfFuelCells()) def test_electricCar_mileage_and_move(self): # I am testing mileage and move for all cars to ensure correct implementation and because mileage is # an important factor for NCT tests and how many kilometers are on the clock. # As in the test_car_mileage 1st the default value is called this time from the ElectricCar class. # Move is then called using the value 15 is this is tested against the value retrieved from getMileage # which is 15 as well so it's true. Mileage is then increased by 6.5 to 21.5. Next the setMileage function # calls the function from the ElectricCar class which has inherited all the functions of the base class Car. # All the types of cars have inherited all the functions of Car. In this case SetMileage is called and # takes the value 40. This is tested using the getMileage function to ensure it's changed to 40 and it has. # The value 0 is tested and mileage variable remains 40. self.assertEqual(0, self.electric_car.getMileage()) self.electric_car.move(15) self.assertEqual(15, self.electric_car.getMileage()) self.electric_car.move(6.5) self.assertEqual(21.5, self.electric_car.getMileage()) self.electric_car.setMileage(40) self.assertEqual(40, self.electric_car.getMileage()) self.electric_car.move(0) self.assertEqual(40, self.electric_car.getMileage()) def test_electricCar_make(self): # Even though make, engineSize, colour and paint are tested in the base class I feel it's important to # have test cases for the subclass' that implement the functions to ensure they are correctly inherited # with the same functionality so the default value ' ' is tested, a string with 1 word and a string with # 2 words. Also i'm not using car.getMake but electric_car.getMake this will be the case in the other # subclass' where the functions are implemented from the subclass not the superclass/base class. self.assertEqual(' ', self.electric_car.getMake()) self.electric_car.setMake('Charger') self.assertEqual('Charger', self.electric_car.getMake()) self.electric_car.setMake('Super Charger') self.assertEqual('Super Charger', self.electric_car.getMake()) def test_electricCar_engineSize(self): # Testing engineSize for ElectricCar class. self.assertEqual(' ', self.electric_car.getEngineSize()) self.electric_car.setEngineSize(1.2) self.assertEqual(1.2, self.electric_car.getEngineSize()) self.electric_car.setEngineSize(0.8) self.assertEqual(0.8, self.electric_car.getEngineSize()) def test_electricCar_colour_and_paint(self): # Testing colour and paint for ElectricCar self.assertEqual(' ', self.electric_car.getColour()) self.electric_car.paint('pink') self.assertEqual('pink', self.electric_car.getColour()) self.electric_car.paint('orange') self.assertEqual('orange', self.electric_car.getColour()) self.electric_car.setColour('yellow') self.assertEqual('yellow', self.electric_car.getColour())