Professional Documents
Culture Documents
Cars' Inventory
Cars' Inventory
Cars’ Inventory
___________________________________________________________
Report by
ENTER YOUR NAME HERE
Date
SELECT DATE OF SUBMISSION HERE
DECLARATION STATEMENT
We, the undersigned students, confirm that the work submitted in this project report is entirely
our own and has not been copied from any other source. Any material that has been used from
other sources has been properly cited and acknowledged in the report.
We are fully aware that any copying or improper citation of references/sources used in this report
will be considered plagiarism, which is a clear violation of the Code of Ethics of Qatar University.
In addition, we have read and understood the legal consequences of committing any violation of
the Qatar University’s Code of Ethics.
TABLE OF CONTENTS
Introduction iv
Project Plan iv
Python’s Code v
Output and validation xxiv
Conclusion xxvii
General Engineering GENG 106 – Computer Programming Lab Project Report iii
Cars’ Inventory
Introduction
This program is an inventory of car and its details. Due to the difficult to manage the manual data
while it’s getting larger we develop a program for it, which allow to add new car, update some
levels of information and print several reports.
This application was built in python and it’s designed to run in the terminal windows. The data of
the car is processed from a file given “cars.txt” and the application stored them in lists that allow a
fast access using the index of the car. Otherwise we can upload data to the file when we add a car,
or when we delete a specific car based on a characteristic. We can also print reports about the top
profit car, best performance between Highway miles per gallon and engine size, or a regression
line related to the engine size and the average miles per gallon.
Project Plan
Python’s Code
def validate_f(N):
#This Function validate that the input float number is neither negative nor zero
while (float(N)<=0.0):
return N
def validate_i(N):
#This Function validate that the input integer number is neither negative nor zero
while (int(N)<=0):
return N
def validate_b(N):
N = input("\nInput the number again. Must be zero (0) or one (1) \n")
return N
try:
F.seek(0)
return len(F.readlines())
except AttributeError:
return -1
def get_data(FILE,N):
#This Function gets the data from the file and split it based on the index of the character
Sports_Car = []
Sports_Utility = []
Wagon = []
Minivan = []
Pickup = []
All_Wheel = []
Rear_Wheel = []
Retail_Price = []
Dealer_Cost = []
Engine_Size = []
Number_Cylinders = []
HP = []
City_MpG = []
Highway_MpG = []
Weight = []
Wheel_Base = []
Length = []
Width = []
f.seek(0)
line = f.readline()
Vehicle_Name.append(line[0:45])
Sports_Car.append(line[46])
Sports_Utility.append(line[48])
Wagon.append(line[50])
Minivan.append(line[52])
Pickup.append(line[54])
All_Wheel.append(line[56])
Rear_Wheel.append(line[58])
Retail_Price.append(line[60:65])
Dealer_Cost.append(line[67:72])
Number_Cylinders.append(line[78:79])
HP.append(line[81:84])
City_MpG.append(line[85:87])
Highway_MpG.append(line[88:90])
Weight.append(line[91:95])
Wheel_Base.append(line[96:99])
Length.append(line[100:103])
Width.append(line[104:106])
return Vehicle_Name, Sports_Car, Sports_Utility, Wagon, Minivan, Pickup, All_Wheel, Rear_Wheel, Retail_Price, Dealer_Cost, Engine_Size,
Number_Cylinders , HP, City_MpG, Highway_MpG, Weight, Wheel_Base, Length, Width
def download_data(FILE):
#This function update the data for the program based on the data downloaded from the file
f.seek(0)
Total_File = f.read()
Vehicle_Name, Sports_Car, Sports_Utility, Wagon, Minivan, Pickup, All_Wheel, Rear_Wheel, Retail_Price, Dealer_Cost, Engine_Size, Number_Cylinders ,
HP, City_MpG, Highway_MpG, Weight, Wheel_Base, Length, Width = get_data(f,Num_Lines)
return Total_File, Num_Lines, Vehicle_Name, Sports_Car, Sports_Utility, Wagon, Minivan, Pickup, All_Wheel, Rear_Wheel, Retail_Price, Dealer_Cost,
Engine_Size, Number_Cylinders , HP, City_MpG, Highway_MpG, Weight, Wheel_Base, Length, Width
#This function shows in screen the data of certain car, using its index to select the car
print(index)
print(Vehicle_Name[index])
print(Sports_Car[index])
General Engineering GENG 106 – Computer Programming Lab Project Report vii
Cars’ Inventory
print("\nThe vehicle is a Sport Utility Vehicle? \n")
print(Sports_Utility [index])
print(Wagon[index])
print(Minivan[index])
print(Pickup [index])
print(All_Wheel [index])
print(Rear_Wheel [index])
print(Retail_Price [index])
print(Dealer_Cost [index])
print(Engine_Size [index])
print("\nHow many cylinders has the engine? If has a rotary engine appears -1 \n")
print(Number_Cylinders [index])
print(HP [index])
print(City_MpG [index])
General Engineering GENG 106 – Computer Programming Lab Project Report viii
Cars’ Inventory
print(Highway_MpG [index])
print(Weight [index])
print(Wheel_Base [index])
print(Length [index])
print(Width [index])
return 0
def bubblesort(List):
#This function is based on the algorithm bubblesort, which rearrange the list in growing order
n = len(List)
return List
def add_a_car(f,New_Car_Input):
#Calculate the length to then fill the missing spaces for the alignment
Len_Name = len(New_Car_Input[0])
Len_Retail = len(New_Car_Input[8])
Len_Engine = len(New_Car_Input[10])
Len_HP = len(New_Car_Input[12])
Len_City_MpG = len(New_Car_Input[13])
Len_Highway_MpG = len(New_Car_Input[14])
Len_Weight = len(New_Car_Input[15])
Len_Wheel_Base = len(New_Car_Input[16])
Len_Length = len(New_Car_Input[17])
Len_Width = len(New_Car_Input[18])
f.seek(0,2)
f.write(New_Car_Input[0])
f.write(" "*(45-Len_Name))
f.write(" ")
f.write(New_Car_Input[1])
f.write(" ")
f.write(New_Car_Input[2])
f.write(" ")
f.write(New_Car_Input[3])
f.write(" ")
f.write(New_Car_Input[4])
f.write(" ")
f.write(New_Car_Input[5])
f.write(" ")
f.write(New_Car_Input[6])
f.write(" ")
f.write(New_Car_Input[7])
f.write(" ")
f.write(New_Car_Input[8])
f.write(" "*(6-Len_Retail))
f.write(" ")
f.write(New_Car_Input[9])
f.write(" "*(6-Len_Dealer))
f.write(" ")
f.write(New_Car_Input[10])
f.write(" "*(3-Len_Engine))
f.write(" ")
f.write(New_Car_Input[11])
f.write(" "*(2-Len_Cylinders))
f.write(" ")
f.write(New_Car_Input[12])
f.write(" "*(3-Len_HP))
f.write(" ")
f.write(New_Car_Input[13])
f.write(" "*(2-Len_City_MpG))
f.write(" ")
f.write(" "*(2-Len_Highway_MpG))
f.write(" ")
f.write(New_Car_Input[15])
f.write(" "*(4-Len_Weight))
f.write(" ")
f.write(" "*(3-Len_Wheel_Base))
f.write(New_Car_Input[16])
f.write(" ")
f.write(New_Car_Input[17])
f.write(" "*(3-Len_Length))
f.write(" ")
f.write(New_Car_Input[18])
f.write(" "*(2-Len_Width))
f.write("\n")
return 0
def Input_a_Car():
#This function read a car and its info from the user and upload it to the file
print ("Input the car data requested \n For dicotomics questions 1 represent YES, and 0 represent NO \n\n")
General Engineering GENG 106 – Computer Programming Lab Project Report xii
Cars’ Inventory
New_Rear_Wheel = validate_b(input ("Has The vehicle Rear-Wheel drive? \n"))
New_Number_Cylinders = input ("How many cylinders has the engine? If has a rotary engine input -1 \n")
add_a_car(f,New_Car_Input) # third parameter in zero because this one is not the first car
return 0
def delete_car(index):
#This function delete a car based on its index, also deletes all of its info
Vehicle_Name.pop(index)
Sports_Car.pop(index)
Sports_Utility.pop(index)
Wagon.pop(index)
Minivan.pop(index)
Pickup.pop(index)
All_Wheel.pop(index)
Rear_Wheel.pop(index)
Retail_Price.pop(index)
Dealer_Cost.pop(index)
Engine_Size.pop(index)
General Engineering GENG 106 – Computer Programming Lab Project Report xiii
Cars’ Inventory
Number_Cylinders.pop(index)
HP, City_MpG.pop(index)
Highway_MpG.pop(index)
Weight.pop(index)
Wheel_Base.pop(index)
Length.pop(index)
Width.pop(index)
return 0
#This Function upload the data from the program to the file, rewritting it since the beginning
f.close()
New_Car = []
for i in range(len(Total_Data[0])):
for j in range(len(Total_Data)):
New_Car.append(Total_Data[j][i])
add_a_car(f,New_Car)
New_Car = []
f.close()
f = open("cars.txt","r+") #Finally we reopen the file in the r+ mode to be able to read and write in it
return f
def Slo_Int_Corr(Engine_Size,Highway_MpG,City_MpG):
#This function calculates the slope, the interception and the correlative from 2 info list
General Engineering GENG 106 – Computer Programming Lab Project Report xiv
Cars’ Inventory
X = []
Y = []
for i in range(len(Engine_Size)):
X.append(float(Engine_Size[i]))
Y.append(Average)
N = len(X)
for i in range(N):
f = open("cars.txt","r+")
Total_File, Num_Lines, Vehicle_Name, Sports_Car, Sports_Utility, Wagon, Minivan, Pickup, All_Wheel, Rear_Wheel, Retail_Price, Dealer_Cost, Engine_Size,
Number_Cylinders , HP, City_MpG, Highway_MpG, Weight, Wheel_Base, Length, Width = download_data (f)
f.seek(0)
print (Total_File)
option = 0
option_seven = False #This command allows us to know if the program has done the section 7 and its calculations
while(option != 9):
print("""
Menu
3. Display all sport cars that has Rear-Wheel Drive, and engine zise is greater than 3
6. Display Top 3 cars has best Highway Miles per Gallon and smallest Engine size
7. Calculate the Slope and intercept of a linear regression and correlation between the x and the y data
9. Exit
""")
option = int (input (" \nPlease select an option from above \n"))
option = validate_i(option)
if (option == 1):
General Engineering GENG 106 – Computer Programming Lab Project Report xvi
Cars’ Inventory
#We recieve a car and its info from the user and writr it in the file and then update the lists in the program
Input_a_Car()
Total_File, Num_Lines, Vehicle_Name, Sports_Car, Sports_Utility, Wagon, Minivan, Pickup, All_Wheel, Rear_Wheel, Retail_Price, Dealer_Cost,
Engine_Size, Number_Cylinders , HP, City_MpG, Highway_MpG, Weight, Wheel_Base, Length, Width = download_data (f)
print(Total_File);
elif (option == 2) :
Total_File, Num_Lines, Vehicle_Name, Sports_Car, Sports_Utility, Wagon, Minivan, Pickup, All_Wheel, Rear_Wheel, Retail_Price, Dealer_Cost,
Engine_Size, Number_Cylinders , HP, City_MpG, Highway_MpG, Weight, Wheel_Base, Length, Width = download_data (f)
#Ask for a name and complete its empty spaces to try to find a match with the existent car´s list
SN = input ("What is the name of the car you are looking for? \n");
if (Search_Name in Vehicle_Name):
if (Search_Name == Vehicle_Name[i]):
index = i
print_info_car (index) #If we find a match, we print the car and its info
else:
General Engineering GENG 106 – Computer Programming Lab Project Report xvii
Cars’ Inventory
Total_File, Num_Lines, Vehicle_Name, Sports_Car, Sports_Utility, Wagon, Minivan, Pickup, All_Wheel, Rear_Wheel, Retail_Price, Dealer_Cost,
Engine_Size, Number_Cylinders , HP, City_MpG, Highway_MpG, Weight, Wheel_Base, Length, Width = download_data (f)
#We try to find the top cars with Rear Wheel Drive and Engine size biggest than 3.0
#Save the index of each car that fulfill each condition and then compare
#to get the match index and print the cars and its info
if ("1" in Rear_Wheel):
index_Rear = []
index_Engine = []
index_P = []
for i in range(len(Rear_Wheel)):
if ("1" == Rear_Wheel[i]):
index_Rear.append(i)
print(index_Rear)
for i in range(len(Engine_Size)):
index_Engine.append(i)
for j in range(len(index_Rear)):
index_P.append(index_Rear[j])
for k in range(len(index_P)):
General Engineering GENG 106 – Computer Programming Lab Project Report xviii
Cars’ Inventory
print_info_car (index_P[k])
print("\n\n\n\n\n\n\n\n\n\n")
else:
Total_File, Num_Lines, Vehicle_Name, Sports_Car, Sports_Utility, Wagon, Minivan, Pickup, All_Wheel, Rear_Wheel, Retail_Price, Dealer_Cost,
Engine_Size, Number_Cylinders , HP, City_MpG, Highway_MpG, Weight, Wheel_Base, Length, Width = download_data (f)
profit = []
profit_Og = []
profit_Og.append(float(prof))
profit.append(float(prof))
profit = bubblesort(profit)
index_top_profit = [];
General Engineering GENG 106 – Computer Programming Lab Project Report xix
Cars’ Inventory
for j in range(len(profit_Og)):
if (float(profit_Og[j]) == (float(profit[-1-i]))): #we run profit[] from the end to the beginning to get the top 10
index_top_profit.append(j);
print(index_top_profit);
for k in range(10):
print_info_car(index_top_profit[k]);
print("\n\n\n\n\n\n\n\n\n\n");
elif (option == 5 ):
Total_File, Num_Lines, Vehicle_Name, Sports_Car, Sports_Utility, Wagon, Minivan, Pickup, All_Wheel, Rear_Wheel, Retail_Price, Dealer_Cost,
Engine_Size, Number_Cylinders , HP, City_MpG, Highway_MpG, Weight, Wheel_Base, Length, Width = download_data (f)
index_minivan = []
#We get the index of the minivan cars to delete it an reorder the data lists
for i in range(len(Minivan)):
index_minivan.append(i)
for j in range(len(index_minivan)):
delete_car(index_minivan[j])
Total_Data_Car = [Vehicle_Name, Sports_Car, Sports_Utility, Wagon, Minivan, Pickup, All_Wheel, Rear_Wheel, Retail_Price, Dealer_Cost, Engine_Size,
Number_Cylinders , HP, City_MpG, Highway_MpG, Weight, Wheel_Base, Length, Width]
#Upload the new data to the file and then update the data in the program
Total_File, Num_Lines, Vehicle_Name, Sports_Car, Sports_Utility, Wagon, Minivan, Pickup, All_Wheel, Rear_Wheel, Retail_Price, Dealer_Cost,
Engine_Size, Number_Cylinders , HP, City_MpG, Highway_MpG, Weight, Wheel_Base, Length, Width = download_data (f)
print (Total_File)
Total_File, Num_Lines, Vehicle_Name, Sports_Car, Sports_Utility, Wagon, Minivan, Pickup, All_Wheel, Rear_Wheel, Retail_Price, Dealer_Cost,
Engine_Size, Number_Cylinders , HP, City_MpG, Highway_MpG, Weight, Wheel_Base, Length, Width = download_data (f)
Top_Highway = []
Smallest_Engine = []
#We proceed to reorder the Highway and the Engine Size Lists
Top_Highway = Highway_MpG[:]
Smallest_Engine = Engine_Size[:]
Top_Highway = bubblesort(Top_Highway)
Top_Highway.reverse()
Smallest_Engine = bubblesort(Smallest_Engine)
index_top_HW = []
index_smallest_engine = []
#We find the index of the Car with Top Highway Miles per Gallon
General Engineering GENG 106 – Computer Programming Lab Project Report xxi
Cars’ Inventory
for i in range(len(Top_Highway)):
for j in range(len(Highway_MpG)):
index_top_HW.append(j)
for i in range(len(Smallest_Engine)):
for j in range(len(Engine_Size)):
index_smallest_engine.append(j)
sum_index = []
#We took the sum of the index in the top to know which car has best performance
for i in range(len(index_top_HW)):
S = index_top_HW.index(i) + index_smallest_engine.index(i)
sum_index.append(S)
top_sum = sorted(sum_index)
index_top_sum = []
for i in range(len(top_sum)):
for j in range(len(sum_index)):
index_top_sum.append(j)
#Finally we print the top 3 cars with best performance in both aspects
for i in range(0,3):
print_info_car (index_top_sum[i])
print("\n\n\n\n\n\n\n\n\n")
General Engineering GENG 106 – Computer Programming Lab Project Report xxii
Cars’ Inventory
option_seven = True #This Command indicates that we got in the section 7 at least one time
Total_File, Num_Lines, Vehicle_Name, Sports_Car, Sports_Utility, Wagon, Minivan, Pickup, All_Wheel, Rear_Wheel, Retail_Price, Dealer_Cost,
Engine_Size, Number_Cylinders , HP, City_MpG, Highway_MpG, Weight, Wheel_Base, Length, Width = download_data (f)
X = []
Y = []
Total_File, Num_Lines, Vehicle_Name, Sports_Car, Sports_Utility, Wagon, Minivan, Pickup, All_Wheel, Rear_Wheel, Retail_Price, Dealer_Cost,
Engine_Size, Number_Cylinders , HP, City_MpG, Highway_MpG, Weight, Wheel_Base, Length, Width = download_data (f)
if (option_seven == False):
print("\n\nYou need to calculate the slope and the intercept of the linear regression in section 7\n\n")
else:
import numpy as np
X = []
Y = []
General Engineering GENG 106 – Computer Programming Lab Project Report xxiii
Cars’ Inventory
#Create the lists x,y of points to plot from the lists X,Y
x = np.array(X)
y = np.array(Y)
m,b = np.polyfit(x,y,1)
plt.plot(x,y,"o")
plt.plot(x, m*x + b)
plt.title("Linear Regression")
plt.xlabel("Engine Size")
plt.show()
General Engineering GENG 106 – Computer Programming Lab Project Report xxiv
Cars’ Inventory
If we select option 1 it allow us to input a new car, asking for each characteristics to include it in
the file.
For the option 2, ask us for the name of the car, and then complete the blank spaces to find a
match, and then return all the info of that specific car if it exists in the inventory. Then show us the
menu again, to select a new option.
General Engineering GENG 106 – Computer Programming Lab Project Report xxv
Cars’ Inventory
In the option 3, the application print tall the cars that has Rear-Wheel Drive and engine size
greater than 3. For that the program verify the condition about Rear-Wheel Drive and verify
engine size greater than 3.0, then compare both results to evaluate which cars fulfill both
condition to finally print these cars.
To the option 4, the program calculates the profit of each cars, (Retail Price minus Dealer Cost) ,
reorder the list to get the top 10 by its index and then return the cars corresponding to each index
in the order pre-established. Then the menu is shown.
When the option 5 is selected, the program evaluate the condition “Minivan” to see if the car is a
minivan, and if fulfill the condition, save the index to further use. Finally, delete all the cars
corresponding to the previous index and then show the menu again
General Engineering GENG 106 – Computer Programming Lab Project Report xxvi
Cars’ Inventory
In the option 6, The application reorder the lists “Highway MpG” in descending order and “Engine
size” in growing order. Then take each car´ sum of the indexes of both list, to get the best
performance in both list. The top 3 cars with the best performance are print on the screen.
For the option 7, the program use the data of the list “Engine Size” and “Average MpG”. The
program calculate the average miles per gallon (MpG) for each car ((Highway MpG + City MpG)/2),
and reorder the index of the cars based on the top average MpG´s list order. Then use these data
as X and Y to calculate the slope, the interception and the correlation of the regression line.
In the option 8, is necessary to go first through the section 7, to get the information for the graph.
We use the matplotlib python´s library to plot the data and the regression line and show it on the
screen. Also show the correlation factor.
General Engineering GENG 106 – Computer Programming Lab Project Report xxvii
Cars’ Inventory
Conclusion
This project handle very well all the steps asked, and show the information clearly and easy to
read, because before each information show the name of that info or the relation with a main
subject.
Is important to accurate that the use of list and function ease the development of the program,
due to the easy access and organization of the information, accessing to them using the name of
the list and the index of the car we want to access. Also the facility given by the function to do
several action only by call them in a line, or the fast process to make one function which
implement another or avoiding us to rewrite a process by making it a function, another plus of the
use of both elements is that these make the code more readable for developers.
General Engineering GENG 106 – Computer Programming Lab Project Report xxviii