Skip to content

Instantly share code, notes, and snippets.

@radzhome
Last active June 3, 2018 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radzhome/da0f5a81f7856da92c9de183e4be116b to your computer and use it in GitHub Desktop.
Save radzhome/da0f5a81f7856da92c9de183e4be116b to your computer and use it in GitHub Desktop.
class TicketMachine(object):
def __init__(self, route1_name, route1_child, route1_adult, route2_name, route2_child, route2_adult):
self.routes = [
{'name': route1_name, 'child_fare': route1_child, 'adult_fare': route1_adult},
{'name': route2_name, 'child_fare': route2_child, 'adult_fare': route2_adult}
]
self.selected_route = None
self.select_route1() # Defaults to route 1 (from his screenshots)
# Credit
self.total_credits = 0.0 # Keeps track of sum of coins inserted by user
# Number of tix for child, adult selected
self.child_quantity = 0
self.adult_quantity = 0
def insert_quarter(self):
self.total_credits += 0.25
def insert_dollar(self):
self.total_credits += 1.0
def insert_two_dollar(self):
self.total_credits += 2.0
def select_route1(self):
self.selected_route = self.routes[0]
def select_route2(self):
self.selected_route = self.routes[1]
def plus_child_quantity(self):
self.child_quantity += 1
def plus_adult_quantity(self):
self.adult_quantity += 1
def minus_child_quantity(self):
if self.child_quantity:
self.child_quantity -= 1
def minus_adult_quantity(self):
if self.adult_quantity:
self.adult_quantity -= 1
def print_ticket(self):
"""
buy or order ticket
:return: bool, success
"""
total_due = self.child_quantity * self.selected_route['child_fare'] + \
self.adult_quantity * self.selected_route['adult_fare']
# No change returned so just take all the money they inserted, if it was enough
if total_due and total_due < self.total_credits:
# Reset all the things (ticket printed, now)
self.child_quantity = self.adult_quantity = 0
# self.total_credits = 0.0
# TODO: If indeed this does not go to 0 but leave amount
self.total_credits = self.total_credits - total_due
self.select_route1() # reset route to 1st route
return True
else:
return False
class TrainStation(object):
"""
Train class has 2 ticket classes initialized, its the view class, gives following options above
This is your view class
"""
def __init__(self):
route1_name = 'Main St'
route1_child = 1.0
route1_adult = 2.0
route2_name = 'King St'
route2_child = 1.25
route2_adult = 1.50
self.ticket_machine1 = TicketMachine(route1_name, route1_child, route1_adult, route2_name, route2_child,
route2_adult)
route1_name = 'Burlington'
route1_child = 3.0
route1_adult = 5.0
route2_name = 'Hamilton'
route2_child = 7.0
route2_adult = 10.0
self.ticket_machine2 = TicketMachine(route1_name, route1_child, route1_adult, route2_name, route2_child,
route2_adult)
self.selected_machine = None
def show_selected_machine_menu(self):
# Interacting with selected machine
# Wouldn't have to do this in python, since you can just get self.selected_machine.child_quantity etc..
# child_quantity, adult_quantity, _, credit = self.selected_machine.get_status()
print """
--------------------------------------------------------------------
Selected Route: {} Child: ${} Adult: ${}
Children: {} Adults: {}
Credit: ${}
--------------------------------------------------------------------
""".format(self.selected_machine.selected_route['name'],
self.selected_machine.selected_route['child_fare'],
self.selected_machine.selected_route['adult_fare'],
self.selected_machine.child_quantity,
self.selected_machine.adult_quantity,
self.selected_machine.total_credits,
)
print """
1. Select Route '{}'
2. Select Route '{}'
3. Add Adult
4. Remove Adult
5. Add Child
6. Remove Child
7. Insert Quarter
8. Insert Loonie
9. Insert Toonie
10. Print Ticket
11. Step away from machine""".format(self.selected_machine.routes[0]['name'],
self.selected_machine.routes[1]['name'])
choice = int(input('Your choice: '))
if choice == 1:
self.selected_machine.select_route1()
elif choice == 2:
self.selected_machine.select_route2()
elif choice == 3:
self.selected_machine.plus_adult_quantity()
elif choice == 4:
self.selected_machine.minus_adult_quantity()
elif choice == 5:
self.selected_machine.plus_child_quantity()
elif choice == 6:
self.selected_machine.minus_child_quantity()
elif choice == 7:
self.selected_machine.insert_quarter()
elif choice == 8:
self.selected_machine.insert_dollar()
elif choice == 9:
self.selected_machine.insert_two_dollar()
elif choice == 10:
if self.selected_machine.print_ticket():
print("Your ticket was printed!")
else:
print("No ticket for you.")
elif choice == 11:
self.selected_machine = None
def show_select_machine_menu(self):
# Selecting the machine first
print """Welcome to the Ticket Counter!
1. Approach first machine.
2. Approach second machine.
3. Exit.
"""
choice = int(input('Your choice: '))
if choice == 3:
print("Good bye")
exit()
elif choice == 1:
self.selected_machine = self.ticket_machine1
elif choice == 2:
self.selected_machine = self.ticket_machine2
def main_loop(self):
"""
No validation here, assuming buttons
handle valid input
:return:
"""
while True:
if self.selected_machine:
self.show_selected_machine_menu()
else:
self.show_select_machine_menu()
# Start up the train station
ts = TrainStation()
ts.main_loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment