Lab 4: Polymorphism
Premise
You are designing a virtual flight ticket searching program. The users have virtual bags that they can store their belongings in, and you want to design a method that can check if the object is a type of ticket or not (as opposed to other items). There are three types of tickets: one that goes to Japan, one to France, and one to Germany.
Setup
Open up a new file in VSCode and name it your_bag.py
. Make sure to copy the stencil code shown below into that file you just created!
class BagItem:
def __init__(self):
self.is_item = True
class FlightTicket(BagItem):
def __init__(self, country_name: str, departure_time: int):
self.country_name = country_name
self.departure_time = departure_time
def read_departure_time(self):
print(self.departure_time)
def is_ticket(self, query: str) -> bool:
#TODO: fill in this method to check if the current item is the flight ticket!
pass
class Receipt(BagItem):
def __init__(self, store_name: str, price: int):
self.store_name = store_name
self.price = price
def is_ticket(self, query: str) -> bool:
#TODO: fill in this method to check if the current item is the flight ticket!
pass
class Phone(BagItem):
def __init__(self, brand: str, is_on: bool):
self.brand = brand
self.is_on = False
def turn_on(self):
self.is_on = True
def turn_off(self):
self.is_on = False
def is_ticket(self, query: str) -> bool:
#TODO: fill in this method to check if the current item is the flight ticket!
pass
def search_for_ticket(things_in_bag):
for item in things_in_bag:
if check_item(item, "France"):
print('The flight ticket for France is in the bag!')
elif check_item(item, "Japan"):
print('The flight ticket for Japan is in the bag!')
elif check_item(item, "Germany"):
print('The flight ticket for Germany is in the bag!')
else:
print('This is not the flight ticket')
# checks whether or not the flight ticket with the country name "query" is in the bag
def check_item(item: BagItem, query: str) -> bool:
"""
Here, you should use the polymorphic is_ticket method defined in the classes above
in order to see if the current query item is the flight ticket or not
This function will be called in the for loop below to print the result of your check
"""
pass
search_for_ticket([Receipt("Coffee shop", 13), Phone("Samsung", False), FlightTicket("France", 400),
Receipt("Uniqlo", 200), Receipt("Brown Bookstore", 350)])
In the stencil, you will see that three classes are already defined. These classes represent what kinds of objects populate the user's bag. These classes also have individual methods that simulate what each object does in the real world.
Below the three classes, you will also find things_in_bag
, which is a list of objects you find while looking through your bag. These are actual instances of the three classes defined above. You are later going to define the check_item
function, which will look at an object and see if it is a type of flight ticket.
Part 1: Defining the Polymorphic Method
Once you've opened the bag, we are going to define the polymorphic method that will help us find the ticket easily. You might notice that in each class, there is a method called is_ticket
. This method takes in a string, which is your query, and returns a boolean value (which is the result of checking whether the current object is what we are looking for).
Later in our search function, we are going to call the is_ticket
method on each item contained in the list. So, right now your job is to define the is_ticket
method for each of the three different classes! Doing so will allow our search method to later call this method on an object contained in the list, regardless of what type that object may be. Polymorphism in action!
Hint: You would only want the flight ticket to be able to return True
when called on a ticket. So, the Receipt
and Phone
classes' is_ticket
methods can never return true - they might look very simple!
If you are confused about defining polymorphic methods, call a TA over for help.
Part 2: Defining the check_item Method
Now that we've defined the is_ticket
method for each of the three classes, we are going to define our check_item
method to locate the flight ticket from our list! This is where we'll make use of our polymorphic method. Because we are looping through the list at the end of the program (where our search method will be used), our job right now is to call the is_ticket
to check whether each item is a flight ticket or not.
The item to check will come from the given list, and you want to return a boolean value signifying whether this object is a flight ticket or not.
Do not modify the things_in_bag
list! This will be used to check if your code is functional.
Part 3: Is Your Ticket There?
Hurray! You've filled out all the methods. Now, we want to check if your code works. In your local terminal, run your file by typing in python3 your_bag.py
. With the given list, your program should print out the following:
This is not the flight ticket
This is not the flight ticket
The flight ticket for France is in the bag!
This is not the flight ticket
This is not the flight ticket
This is because the flight ticket to France is the third item in the things_in_bag
list.
If you find a bug(for example, your program only prints out one kind of statement), then try to use print lines to figure out how your code is being executed. If you have any questions, raise your hand and your Lab TA will help!
Success!
You've found the flight ticket, and the user is happy!