In: Computer Science
Python Programming
This assignment will give you practice with
interactive programs, if/else statements, collections, loops and
functions.
Problem Description
A small car yard dealing in second hand cars needs an application
to keep records of cars in stock. Details of each car shall include
registration(rego), model, color, price paid for the car (i.e.
purchase price) and selling price. Selling price is calculated as
purchased price plus mark-up of 30%. For example, Toyota Corolla
bought for $20,000 will have the selling price of 20000 * (1 + 0.3)
= 26000.
Task Requirements
Imagine you have been invited to develop a menu driven python
application to manage records of cars in stock. Based on the
problem description, starter program (code template) has been
provided. Investigate the provided starter program. There are four
scripts(application.py, caryard.py, cars.py and vehicle.py). Driver
function(main) is defined in the appliction.py, which imports
caryard module(caryard.py). Caryard declares all the core functions
(buy, sell, search, etc. …) for supporting the car yard business.
The caryard module imports cars module(cars.py), and the cars
module in turn imports the vehicle that include a definition of a
Car class.
Program is executed by loading and running the application.py. The
important work of interconnecting and wiring of modules and
functions have been already been done. However, most of these
functions, although declared and wired up in the program are not
yet implemented. Your task is to implement these functions. There
are eleven (11) tasks to be completed described next within the
modules:
1. application.py
• Task 1: implement menu ()
2. caryard.py
• Task 2: implement buy_car ()
• Task 3: implement sell_car ()
• Task 4: implement search_car ()
• Task 5: implement list_all_cars ()
3. cars.py
• Task 6: implement addCar (rego, model, color, price)
• Task 7: implement removeCar (rego)
• Task 8: implement all_cars ()
• Task 9: implement search(rego)
4. vehicle.py
• Task 10: implement the Car class
• Task 11: UML model of a Car class
Description of Program functions:
Program displays a menu on execution. Five menu options are
available to allow the user to buy, sell, search, show all cars,
and exit program respectively. Program functionality and screen
output for each of the menu option when selected by the user
follows:
Program menu on execution:
Option 1 (Buy a car): User enters 1. Note the error message for
duplicate car rego
Option 2 (Sell a car): User enters 2. Note the error message for
invalid car rego, and the sell price which is a (30%) mark-up of
purchase price.
Option 3 (Search): User enters 3
Option 4 (Show all): User enters 4
Option 5 (Exit Program): User enters 5
'''
Python version : 2.7
Python program to create a car class
'''
class Car:
def __init__(self, rego, model, color, purchase_price):
self.rego = rego
self.model = model
self.color = color
self.purchase_price = purchase_price
self.selling_price = self.purchase_price*(1+0.3)
def __str__(self):
return('Registration : %s Model: %s Color : %s Purchase Price : $%.2f Selling Price : $%.2f' %(self.rego, self.model, self.color, self.purchase_price, self.selling_price))
def __repr__(self):
return('Registration : %s Model: %s Color : %s Purchase Price : $%.2f Selling Price : $%.2f' %(self.rego, self.model, self.color, self.purchase_price, self.selling_price))
def get_rego(self):
return self.rego
def get_model(self):
return self.model
def get_color(self):
return self.color
def get_purchase_price(self):
return self.purchase_price
def get_selling_price(self):
return self.selling_price
def set_rego(self, rego):
self.rego = rego
def set_model(self):
self.model = model
def set_color(self,color):
self.color = color
def set_purchase_price(self,purchase_price):
self.purchase_price = purchase_price
self.selling_price = self.purchase_price(1+0.3)
#end of vehicle.py
Code Screenshot:
'''
Python version : 2.7
Python program to create functions to manage the list of cars
'''
from vehicle import Car
def addCar(rego, model, color, price, carList):
idx = search(rego,carList)
if(idx == -1):
carList.append(Car(rego,model,color,price))
return True
return False
def removeCar(rego,carList):
idx = search(rego,carList)
if(idx != -1):
car = carList.pop(idx)
return car
else:
return None
def all_cars(carList):
if(len(carList) == 0):
print('No cars present')
else:
for car in carList:
print(car)
def search(rego,carList):
for i in range(len(carList)):
if carList[i].get_rego() == rego:
return i
return -1
#end of cars.py
Code Screenshot:
'''
Python version : 2.7
Python program to create menu options for the main
'''
import cars
def buy_car(carList):
rego = raw_input('Registration number : ')
model = raw_input('Model : ')
color = raw_input('Color : ')
purchase_price = float(raw_input('Purchase price :
$'))
if(cars.addCar(rego,model,color,purchase_price,carList)):
print('Car with registration : %s
already exists' %(rego))
def sell_car(carList):
rego = raw_input('Registration Number : ')
car = cars.removeCar(rego,carList)
if(car == None):
print("Car with registration : %s
doesn't exists" %(rego))
else:
print("Car sold at price : %.2f"
%(car.get_selling_price()))
def search_car(carList):
rego = raw_input('Registration Number : ')
idx = cars.search(rego,carList)
if(idx != -1):
print(carList[idx])
else:
print("Car with registration : %s
doesn't exists" %(rego))
def list_all_cars(carList):
cars.all_cars(carList)
#end of caryard.py
Code Screenshot:
'''
Python version : 2.7
Python program to create the main application
'''
import caryard
def main():
carList = []
choice = 1
while choice != 5:
print('1. Buy a Car')
print('2. Sell a Car')
print('3. Search for a Car')
print('4. Show all cars')
print('5. Exit')
choice = int(raw_input('Enter your choice : '))
if choice == 1:
caryard.buy_car(carList)
elif choice == 2:
caryard.sell_car(carList)
elif choice == 3:
caryard.search_car(carList)
elif choice == 4:
caryard.list_all_cars(carList)
elif choice != 5:
print('Invalid choice')
print('')
print('Bye!')
main()
#end of application.py
Code Screenshot:
Output:
Car UML: