Question
With the code given below: A. When the user is prompt to enter the data of an object (from any class in the information system),
With the code given below:
A. When the user is prompt to enter the data of an object (from any class in the information system), there should be an option to write/append the record into a file.
B. Add an option in the starting menu of options to save all the entered data into a file.
C. Add an option in the start menu to read the previously save records from the file and displayed these records in the console.
D. Write into a file and read from a file
Python code:
class car:
#constructor
def __init__(self, name, model, year):
self._name = name
self._model = model
self._year = year
# set name method
def set_name(self, name):
if isinstance(name, str):
self._name = name
# set_model() is overloaded here
def set_model(self, name, model):
set_name(name)
self._model = model
# set model method
def set_model(self, model):
self._model = model
# set year method
def set_year(self, year):
if year > 0 and year < 120:
self._year = year
# get name method
def get_name(self):
return self._name
# get model method
def get_model(self):
return self._model
# get year method
def get_year(self):
return self._year
# print the object of class
def __str__(self):
return "Normal car details: " + self._name + " " + self._model + " " + self._year
# property class
class property:
# constructor
def __init__(self, name, year, area):
self._name = name
self._year = year
self._area = area
# set name method
def setname(self, name):
self._name = name
def getname(self):
return self._name
# set year
def set_year(self, year):
self._year = year
# get year method
def get_year(self):
return self._year
# print class
def __str__(self):
return "property details: {name} {year} {area}".format(name=self._name, year=self._year, area=self._area)
# ferrari class inherit from base class car
class ferrari(car):
# constructor
def __init__(self, name, model, year):
# calling parent class constructor
car.__init__(self, name, model, year)
# user defined function of parent class is used
# __str__ method is overidden
def __str__(self):
return "Ferrari details: {name} {model} {year}".format(name=self.get_name(), model=self.get_model(),
year=self.get_year())
class tesla(car):
# constructor
def __init__(self, name):
# calling parent class constructor
car.__init__(self, name, model, year)
# userdefined function of parent class is used
# __str__ method is overidden
def __str__(self):
return "Tesla details:{name} {model} {year}".format(name=self.get_name(), model=self.get_model(),
year=self.get_year())
# customer class
class customer:
# constructor
def __init__(self, name, age, car=None, property=None):
self._name = name
self._age = age
self._property = property
self._car = car
# getter method
def getname(self):
return self._name
# set name
def setname(self, name):
self._name = name
# print class
def __str__(self):
return "name:{name} age:{age} car={car} property={property} ".format(name=self._name, age=self._age,
car=self._car, property=self._property)
# A Linked List Node
class Node:
def __init__(self, data, left=None, right=None):
# set data in the allocated node and return it
self.data = data
self.next = None
def getname(self):
return self.data.getname()
def setname(self, name):
self.data.setname(name)
def __str__(self):
return "{node} ".format(node=self.data)
# implement queue structure
class Queue:
rear = None
front = None
# it store the count of the element added to the queue
count = 0
def __init__(self, num):
# set data in the allocated node and return it
self.size = num
# Delete from the front of the queue
def dequeue(self):
# exit if there is no element in the queue
if self.front is None:
print("Queue Underflow")
exit(1)
# remove from the front
temp = self.front
print("Removing...", temp.data)
# make next node to front
self.front = self.front.next
# I flist empty then update front and rear
if self.front is None:
self.rear = None
# deallocate the memory of the removed node and
# optionally return the removed item
item = temp.data
self.count = self.count - 1
return item
# This add the node to the end of the queue
def enqueue(self, item): # insertion at the end
# creat an instance of the node
node = Node(item)
print("Inserting...", item)
# if the queue is empty update front and rear pointer and point it to the node
if self.front is None:
# initialize both front and rear
self.front = node
self.rear = node
elif self.isfull():
print(" Queue is full ")
else:
# add node to the rear of the queue and update the rear pointer
self.rear.next = node
self.rear = node
# update the count of the queue
self.count = self.count + 1
# access the element from the first
def peek(self):
# If queue doesn't empty returnt the data of the node
if self.front:
return self.front.data
else:
# else exit
exit(1)
return -1
# check if the queue is empty
def isempty(self):
return self.rear is None and self.front is None
# check if the queue is full
def isfull(self):
if (self.count == self.size):
return True
else:
return False
# return the count of element added to the queue
def Size(self):
return self.count
# make the class iteratable
def __iter__(self):
self.tmp = self.front
return self
# access the next elemnt sequencially through iteration
def __next__(self):
# if queue empty throw the error
if self.tmp == None:
raise StopIteration
else:
tt = self.tmp
self.tmp = self.tmp.next
return tt
if __name__ == "__main__":
# queue objet is created
li = Queue(2)
while True:
# menu
print("1.Insert ")
print("2.search ")
print("3.display ")
print("4.delete ")
print("5.exit ")
f = False
i = int(input("Enter choice:"))
# ask user input
if i == 1:
# ask for customer data
name = input("Customer name: ")
age = input("Customer Age: ")
choice = int(input("1.car insurance 2.property inurance "))
# on the basis of user input perform action
if choice == 1:
# ask car details
car_name = input("Customer car name: ")
car_model = input("Customer car model: ")
car_year = input("Customer car year: ")
if car_name == "ferrari":
# add object to the queue
li.enqueue(customer(name, age, ferrari(car_name, car_model, car_year)))
elif car_name == "tesla":
li.enqueue(customer(name, age, tesla(car_name, car_model, car_year)))
else:
li.enqueue(customer(name, age, car(car_name, car_model, car_year)))
elif choice == 2:
# ask property details
property_name = input("Customer property name: ")
property_year = input("Customer property year: ")
property_area = input("Customer property area: ")
# add object to the queue
li.enqueue(customer(name, age, None, property(property_name, property_year, property_area)))
# search customer
elif i == 2:
# ask customer name
name = input("Customer name: ")
try:
# search the customer name and display the customer details
for i in li:
if i.getname() == name:
print("Customer details :")
print(i)
f = True
if not f:
print("Customer doesn't exist")
f = False
except StopIteration:
pass
# search customer and display it
elif i == 3:
name = input("Customer name: ")
for i in li:
# search the customer name and display the customer details
if i.getname() == name:
print("Customer details :")
print(i)
f = True
if not f:
print("Customer doesn't exist")
f = False
# delete customer
elif i == 4:
# delete the object from the front of the queue
li.dequeue()
# exit
else:
break
Step 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