Question: Hi this is the code that is written, assignment it pertains to and relevant data below. Could someone 1) review code overall for meeting assignment
Hi this is the code that is written, assignment it pertains to and relevant data below.
Could someone
1) review code overall for meeting assignment requirements and provide recommendations on changes needed (if any)
2) assist me on editing my code to use datetime module per requirements instead of method used here?
Thank you in advance!
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import math
import traceback
import sys
import os
class Person:
def __init__(self, name, address):
self.__name = name
self.__address = address
def get_name(self):
return self.__name
def get_address(self):
return self.__address
def set_name(self, name):
self.__name = name
def set_address(self, address):
self.__address = address
class Pet:
def __init__(self, petType, name, age, weight, person):
self.__pet_type = petType
self.__pet_name = name
self.__pet_age = age
self.__pet_weight = weight
self.__person = person
#accessor methods
def get_pet_type(self):
return self.__pet_type
def get_pet_name(self):
return self.__pet_name
def get_pet_age(self):
return self.__pet_age
def get_pet_weight(self):
return self.__pet_weight
#mutator methods
def set_pet_type(self, typr):
self.__pet_type = typr
def set_pet_name(self, name):
self.__pet_name = name
def set_pet_age(self, age):
self.__pet_age = age
def set_pet_weight(self, weight):
self.__pet_weight = weight
def __str__(self):
pet_details = "{}:{}:{}:{:0.2f}:{}:{}".format(self.get_pet_type(),self.get_pet_name(), self.get_pet_age(), self.get_pet_weight(), self.__person.get_name(), self.__person.get_address())
return pet_details
class Mammal(Pet):
def __init__(self, name, age, weight, litterSize, hasClaws, person):
self.__litter_size = litterSize
self.__has_claws = hasClaws
super(Mammal,self).__init__("Mammal",name, age, weight, person)
def get_litter_size(self):
return self.__litter_size
def get_has_claws(self):
return self.__has_claws
def set_litter_size(self, litterSize):
self.__litter_size = litterSize
def set_has_claws(self, hasClaws):
self.__has_claws = hasClaws
def get_weight(self):
age = super().get_pet_age()
weight = float(super().get_pet_weight())
if(int(age)<60):
weight = weight
elif (int(age)<301):
for i in range(60, int(age)+1, 60):
weight*=1.1
else:
weight = weight * (1.1)**5
return math.floor(weight)
def __str__(self):
return super(Mammal, self).__str__()+":{}:{}".format(self.get_litter_size(),self.get_has_claws())+" "
class Fish(Pet):
def __init__(self, name, age, weight, scaleCondition, length, person):
super(Fish,self).__init__("Fish",name, age, weight, person)
self.__scale_condition = scaleCondition
self.__length = length
def get_scale_condition(self):
return self.__scale_condition
def get_length(self):
return self.__length
def set_scale_condition(self, scaleCondition):
self.__scale_condition = scaleCondition
def set_length(self, length):
self.__length = length
def get_weight(self):
age = super().get_pet_age()
weight = float(super().get_pet_weight())
if(int(age)<80):
weight = weight
elif (int(age)<241):
for i in range(80, int(age)+1, 80):
weight*=1.05
else:
weight = weight * (1.05)**3
return math.floor(weight)
def __str__(self):
return super(Fish, self).__str__()+":{}:{}".format(self.get_scale_condition(),self.get_length())+" "
class Amphibian(Pet):
def __init__(self, name, age, weight, isVenomous, person):
super(Amphibian,self).__init__("Amphibian",name, age, weight, person)
self.__is_venomous = isVenomous
def get_is_venomous(self):
return self.__is_venomous
def set_is_venomous(self, isVenomous):
self.__is_venomous = isVenomous
def get_weight(self):
age = super().get_pet_age()
birthWeight = super().get_pet_weight()
if(int(age)<120):
return birthWeight
weight = float(birthWeight)
currentAge=120
while(currentAge<=int(age) and currentAge <=360):
weight*=1.05
currentAge+=120
while(currentAge<=int(age)):
weight*=1.03
currentAge+=120
return math.floor(weight)
def __str__(self):
return super(Amphibian, self).__str__()+":{}".format(self.get_is_venomous())+" "
class DriverClass:
def writeToFile(self,fileName, pet):
file = open(fileName, "a")
file.write(str(pet))
file.close()
def readFromFile(self,fileName):
pets = []
with open(fileName) as file:
for line in file:
pets.append(line)
return pets
def getWeight(self, pets):
print(" ***Pet name and current weight of all Pets*** ")
for petInfo in pets:
info = petInfo.split(":")
pet_type = info[0]
w1 = 0
if pet_type =="Mammal":
mammal = Mammal(info[1],info[2],info[3],info[4],info[5],None)
w1 = mammal.get_weight()
elif pet_type == "Fish":
fish = Fish(info[1],info[2],info[3],None, None, None)
w1 = fish.get_weight()
else:
amphibian = Amphibian(info[1],info[2],info[3], None, None)
w1 = amphibian.get_weight()
details = info[1]+" weighs "+str(w1)+" pounds."
print(details)
def getAllPetInfo(self, pets):
for petInfo in pets:
info = petInfo.split(":")
pet_type = info[0]
details=""
if pet_type =="Mammal":
print(" Details of this Mammal pet are:")
details = " Pet's Name: "+info[1]+"; Pet's Age: "+info[2]+" days old; Pet's Birth Weight: "+info[3]+" Owner Name: "+info[4]+"; Owner Address: "+info[5]+" Litter Size: "+info[6]+"; Has Claws? "+info[7]
elif pet_type == "Fish":
print(" Details of this Fish pet are:")
details = " Pet's Name: "+info[1]+"; Pet's Age: "+info[2]+" days old; Pet's Birth Weight: "+info[3].format("{:0.2f}")+" Owner Name: "+info[4]+"; Owner Address: "+info[5]+" Scale Condition: "+info[6]+"; Length: "+info[7]
else:
print(" Details of this Amphibian pet are:")
details =" Pet's Name: "+info[1]+"; Pet's Age: "+info[2]+" days old; Pet's Birth Weight: "+info[3].format("{:0.2f}")+" Owner Name: "+info[4]+"; Owner Address: "+info[5]+" Is Venomous: "+info[6]
print(details)
def main(self):
#Display Menu
print(" \t\t\t\t\t*****The Pet Inventory App*****")
loop = 1
while loop == 1:
print(" \t\t ====MENU==== ")
print(" 1. To add a new pet")
print(" 2. To print current weight of all pets")
print(" 3. To print all pets and owners")
print(" 4. To exit this program")
print()
try:
ch = input(" Enter your choice(1-4): ")
print(" Entered choice is:", ch)
fi="petdata.dat"
while str(ch) != '1' and str(ch) != '2' and str(ch) != '3' and str(ch) != '4':
print(" * Invalid input. Please choose from 1 to 4!")
ch = input(" Enter your choice(1-4): ")
if ch == '1':
#pet details
pet_type=input(" Enter the type of pet(1 - Mammal; 2 - Fish; 3 - Amphibian): ")
while ((pet_type != '1') and (pet_type != '2') and (pet_type != '3')):
print("Wrong Selection! Please answer 1 or 2 or 3")
pet_type = input("Enter the type of pet(1 - Mammal; 2 - Fish; 3 - Amphibian): ")
pet_name=input(" Enter the pet's name: ")
pet_age=int(input(" Enter the pet's age: "))
pet_birthweight=float(input(" Enter the pet's birth weight: "))
owner_name=input(" Enter the owner's name: ")
owner_address=input(" Enter the owner's address: ")
person = Person(owner_name, owner_address)
if pet_type == '1':
mammal_litter_size = int(input(" Enter the litter size: "))
mammal_hasClaws = input(" Does the pet have claws? Answer 'yes' or 'no': ").lower()
while (mammal_hasClaws.lower() != 'yes') and (mammal_hasClaws.lower() != 'no'):
print("Invalid input. Please answer 'yes' or 'no'")
mammal_hasClaws = input("Does the pet have claws? Answer 'yes' or 'no': ")
if mammal_hasClaws.lower() == 'yes':
mammal_hasClaws = 'Yes'
elif mammal_hasClaws.lower() == 'no':
mammal_hasClaws = 'No'
m1 = Mammal(pet_name, pet_age, pet_birthweight, mammal_litter_size, mammal_hasClaws, person)
self.writeToFile(fi,m1)
elif pet_type == '2':
fish_scale_condition = input(" Enter the scale condition. Answer 'Rough' or 'Scaly': ")
fish_length = input(" Enter the length: ")
f1= Fish(pet_name, pet_age, pet_birthweight, fish_scale_condition, fish_length, person)
self.writeToFile(fi,f1)
elif pet_type == '3':
amphibian_isVenomous = False if input(" Is venomous? Answer 'Yes' or 'No': ").lower() == 'no' else True
a1= Amphibian(pet_name, pet_age, pet_birthweight, amphibian_isVenomous, person)
self.writeToFile(fi,a1)
else:
print(" Invalid input entered for pet type!!!")
elif ch == '2':
pet_list=self.readFromFile(fi)
self.getWeight(pet_list)
if(len(pet_list)==0):
print(" No pets in inventory!!")
elif ch == '3':
pet_list=self.readFromFile(fi)
self.getAllPetInfo(pet_list)
if(len(pet_list)==0):
print(" No pets in inventory!!")
elif ch == '4':
print(" Your selection is 4. You chose to exit this program")
response = input("Are you sure (Y/N)? ")
if response in ['N', 'n']:
continue
elif response in ['Y' , 'y']:
loop = 0
else:
print(" Invalid response!")
else:
print(" * Invalid input. Please choose from 1 to 4!")
except Exception as e:
print(e)
traceback.print_exc()
driver=DriverClass()
driver.main()
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MIS 6382
Object Oriented Systems
Fall 2020
I.You will produce a pet inventory application by fulfilling the requirements given below.You must first produce the classes described below.Then devise a driver program that will use these classes to build an application for a Pet Store.All pet data is stored in a file called petdata.dat.
II.Each time you run the program, the user is allowed to repeatedly select from the four choices described below:
1.Add a new pet
2.Compute and print current weight together with the age (in days)of all pets in the file
3.Print all pet information for every pet in the file
4.Exit the application
III.The first time you execute the program, your file will have no pets in it. Second time onwards, all pet data from previous executions of the program, should be available
Pet Application
The base class Pet has the following attributes:
opet's name (String)
odate of birth (date),
obirth weight (float), and
oowner (Person).
The child classes Mammal, Fish and Amphibian that inherit from Pet and have the following additional properties
oMammals - litter size (int), hasClaws (boolean)
oFish - scale condition (String), length (float)
oAmphibian - isVenomous (boolean).
All these classes have the __init__ method as well as the get and set methods.In addition, they have additional methods required to complete the application as described below.
Current weight is calculated as follows:
oMammal: Weight increases by 8% every 50 days for the first 300 days. Weight is constant after that. Assume that the increase occurs on the 50th day only.
oFish:Weight increases by 4% every 90 days for the first 360 days and then stays constant. Assume that the increase occurs on the 90th day only.
oAmphibians: Weight increases by 5% every 120 days for the first 360 days and then increases by 3% every 120 days for the next 240 days and then stays constant. Assume that the increase occurs on the 120th day only.
The Person class is as described below:
It contains two instance variables: one each for the name, address of the owner.It has a __init__ method as well as get and set methods for all the instance variables.You may use aggregation or composition to include a Person object to your Pet data.
The driver program does the following:
1.Accepts input for new pets
2.Lists the current weight of all the pets together with pet name. (The weight is rounded down to the nearest integer).
3.Displays all pet information for all the pets in the system. This includes owner details as well as data specific to pet type.
4.Exits the system after writing all information to a file.
Any pet data from earlier executions of the application must be available in subsequent executions.
Additional Requirements:
oYour program should use exception handling to validate user input for date of birth and weight.If the user enters a non-numeric value for either, or if the month or day for the date of birth is an invalid value, print a message and exit the program.
oYour program should also ensure that the user enters valid values for the menu options as well as the choice of pet type.If the user enters incorrect options for either the menu option or the pet type, your program should display an appropriate message and display the menu or the choice of pet types again.
Using dates in Python.
oTo complete, you will need the date class from the datetime module.
oThe user should enter year, month and date (in that order) separated by commas.If the year is not 2 characters long, raise a ValueError.Otherwise prefix '20' to the year.This can then be converted to a 4-digit numeric value later.
oEnsure that both day and month are 2-digits by padding with zeroes if necessary
oCompute the age (in days) by subtracting the dob from today's date.
oCompute the current weight as described earlier.
The screen shots below show how your program should function with correct input.Screenshots for handling incorrect input are not shown. However, as described earlier, your code should use exception handling to validate user input.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
