PetStore Class Write a class named PetStore with the following attributes and me
ID: 3777853 • Letter: P
Question
PetStore Class Write a class named PetStore with the following attributes and methods. Save this class as PetStore.py
Attributes __animals: a list used to store Animal objects.
Methods __init__: use this method to create an empty list for __animals.
add_animal: this method should receive an Animal object and append it to the __animals list.
#THIS IS EXTRA CREDIT delete_animal: this method should ask the user for a name for a animal and then remove them from the list.
show_animals: this method will print information about each of the Animal objects within __animals. If no Animals have been added to the list, it should print out a message saying that there are no Animals.
PetStoreManager Program Once you have written the Animal and PetStore classes, create another program called petstoremanager.py. This program will use Animal.py and PetSore.py as modules.
In petstoremanager.py, create a PetStore object and print a menu with three options(four with extra credit): Add Animal, Delete Animal, Show Animals, and Exit.
Add Animal: choosing this option should prompt the user to enter the type and name of an Animal. Use that input to create an Animal object and use the Store object’s add_animal method to store the animal.
Delete Animal: choosing this option should trigger the PetStore object’s delete_animal method.
Show Animals: choosing this option should trigger the PetStore object’s show_animals method.
Exit: this should exit the program. If exit is not selected, the program should loop and display the options again.
Sample Program Operation
Pet Store Options
-----------
1. Add Animal
2. Delete Animal
3. Show Animals
4. Exit
What would you like to do? 3
Animal List
-----------
There are no animals in your Pet Store!
Pet Store Options
-----------
1. Add Animal
2. Delete Animal
3. Show Animals
4. Exit
What would you like to do? 1
What type of animal would you like to create? Platypus
What is the animal's name? Penelope
Is the animal available for adoption?(y/n) y
Pet Store Options
-----------
1. Add Animal
2. Delete Animal
3. Show Animals
4. Exit
What would you like to do? 1
What type of animal would you like to create? Fox
What is the animal's name? Frank
Is the animal available for adoption?(y/n) n
Pet Store Options
-----------
1. Add Animal
2. Delete Animal
3. Show Animals
4. Exit
What would you like to do? 1
What type of animal would you like to create? Shark
What is the animal's name? Sally
Is the animal available for adoption?(y/n) y
Pet Store Options
-----------
1. Add Animal
2. Delete Animal
3. Show Animals
4. Exit
What would you like to do? 3
Animal List
-----------
Penelope the Platypus is sleepy and is available
Frank the Fox is hungry and is not available
Sally the Shark is happy and is available
Pet Store Options
-----------
1. Add Animal
2. Delete Animal
3. Show Animals
4. Exit
What would you like to do? 2
What is the animal's name you'd like to remove? Frank
Frank the Fox has been removed
Pet Store Options
-----------
1. Add Animal
2. Delete Animal
3. Show Animals
4. Exit
What would you like to do? 3
Animal List
-----------
Penelope the Platypus is sleepy and is available
Sally the Shark is happy and is available
Pet Store Options
-----------
1. Add Animal
2. Delete Animal
3. Show Animals
4. Exit
What would you like to do? 4
Thank you for visiting the Pet Store!
Explanation / Answer
PetStore.py
import Animal
class PetStore:
animals = None
def __init__(self):
self.animals = []
def add_animal(self, animal):
self.animals.append(animal)
def delete_animal(self, name):
for animal in self.animals:
if animal.name == name:
self.animals.remove(animal)
return True
return False
Animal.py
class Animal:
animal_type = None
name = None
available_for_adoption = None
def __init__(self, animal_type, name, available_for_adoption):
self.animal_type = animal_type
self.name = name
self.available_for_adoption = available_for_adoption
petstoremanager.py
from PetStore import PetStore
from Animal import Animal
petstore = PetStore()
choice = input("Pet Store Options ----------- 1. Add Animal 2. Delete Animal 3. Show Animals 4. Exit What would you like to do? ")
while choice!=4:
if choice == 1:
animal_type = raw_input("What type of animal would you like to create? ")
name = raw_input("What is the animal's name? ")
a = raw_input("Is the animal available for adoption?(y/n) ")
if a=='y':
available_for_adoption = True
else:
available_for_adoption = False
animal = Animal(animal_type, name, available_for_adoption)
petstore.add_animal(animal)
elif choice == 2:
name = raw_input("What is the animal's name you'd like to remove? ")
deleted = petstore.delete_animal(name)
if deleted:
print name+" has been removed."
else:
print name+" could not be found"
elif choice == 3:
print "Animal List ----------- "
for animal in petstore.animals:
if animal.available_for_adoption==True:
avail = "is available"
else:
avail = "is not available"
print animal.name + " is " + animal.animal_type + " and " +avail
choice = input("Pet Store Options ----------- 1. Add Animal 2. Delete Animal 3. Show Animals 4. Exit What would you like to do? ")
print "Thank you for visiting the Pet Store!"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.