Question
I used the following codes and having a problem. When I update the vehicle information, the number I input into vehicle mileage goes to vehicle
I used the following codes and having a problem. When I update the vehicle information, the number I input into vehicle mileage goes to vehicle manufacturing year on the updates. How can I fix this issue?
p.s image is attached
class Vehicle:
id = 1
'''
Constructor Function
Name: Name of the vehicle
Model: Model of the vehicle
Color: Color of the vehicle
Year: Manufacturing year of the vehicle
Mileage: Mileage of the vehicle
'''
def __init__(self, make, model, color, year, mileage) -> None:
self.__make = make
self.__model = model
self.__color = color
self.__year = year
self.__mileage = mileage
self.__id = Vehicle.id
Vehicle.id += 1
# Getter method to access the vehicle id
def getVehicleId(self):
return self.__id
def __str__(self) -> str:
return f" Vehicle Id: {self.__id} Vehicle Make: {self.__make} Vehicle Model: {self.__model} Vehicle Color: {self.__color} Vehicle Manufacturing Year: {self.__year} Vehicle Mileage: {self.__mileage}"
class Automobile:
# Constructor function that creates an empty inventory
def __init__(self) -> None:
self.vehicles = []
# Method to add a new vehicle in the inventory
def addNewVehicle(self, vehicleObject):
print(" Adding a new vehicle with id ", vehicleObject.getVehicleId())
# Check whether with the given id is already available or not
# if already available then print appropriate message and return
# Otherwise add the vehicle to the vehicle list
for vehicle in self.vehicles:
if vehicle.getVehicleId() == vehicleObject.getVehicleId():
print("Vehicle is already there in the inventory")
return
self.vehicles.append(vehicleObject)
print("Vehicle with id " + str(vehicleObject.getVehicleId()) + " has been added to the inventory successfully.")
# Method to remove a vehicle from the inventory
def removeVehicle(self, id):
print(" Removing the vehicle with id ", id)
# Iterate each vehicle from the list
for v in self.vehicles:
# if the id of the current vehicle matches with the given id
# then remove the vehicle and return
# otherwise print appropriate message
if v.getVehicleId() == id:
self.vehicles.remove(v)
print("Vehicle with id " + str(id) + " has been deleted successfully")
return
print("Vehicle with id " + str(id) + " is not present in the inventory.")
# Method to update the attributes of a particular vehicle
def updateVehicle(self, id):
print(" Updating the vehicle with id ", id)
for v in self.vehicles:
if v.getVehicleId() == id:
v._Vehicle__make = input("Enter new Vehicle make: ")
v._Vehicle__model = input("Enter new Vehicle model: ")
v._Vehicle__color = input("Enter new Vehicle color: ")
v._Vehicle__year = input("Enter new Vehicle manufacturing year: ")
v._Vehicle__year = input("Enter new Vehicle mileage: ")
print("Details updated.. ")
return
print("Vehicle with id " + str(id) + " is not present in the inventory. ")
# Method to print each vehicle details in the inventory
def displayVehicles(self):
print(" Vehicles available in the inventory are: ")
for vehicle in self.vehicles:
print(vehicle)
print("------------------------------------------")
def writeToFile(self):
print(" Writing inventory data to the vehicle.txt file")
try:
with open("vehicle.txt", 'w') as file:
for v in self.vehicles:
file.write(v.__str__())
print("Data has been successfully written to the vehicle.txt file")
except:
print("Some error occurred.")
# Creating vehicle objects
v1 = Vehicle('Maruti', 'Alto', 'Black', 2021, 21)
v2 = Vehicle('Maruti', 'Baleno', 'White', 2020, 18)
# Creating Automobile class object
a = Automobile()
# Adding vehicle to the inventory
a.addNewVehicle(v1)
a.addNewVehicle(v2)
a.addNewVehicle(v2)
# Displaying vehicle inventory
a.displayVehicles()
a.removeVehicle(2)
print(" Updated inventory")
a.displayVehicles()
a.updateVehicle(1)
a.displayVehicles()
a.writeToFile()
Output:
Vehicle Make: Maruti Vehicle Model: Alto Vehicle Color: Black Vehicle Manufacturing Year: 2021 Vehicle Mileage: 21 Updating the vehicle with id 1 Enter new Vehicle make: Toyoto Enter new Vehicle model: Fortuner Enter new Vehicle color: White Enter new Vehicle manufacturing year: 2021 Enter new Vehicle mileage: 7 Details updated.. Vehicles available in the inventory are: Vehicle Id: 1 Vehicle Make: Toyoto Vehicle Model: Fortuner Vehicle Color: White Vehicle Manufacturing Year: 7 Vehicle Mileage: 21Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started