menator01
Gold Coder
A little practice script I wrote a couple years ago. I could probably do a little better job of it now. Anyway here is drinks.py for anyone that may want a practice script.
output
Python:
#! /usr/bin/env python3
# Do the imports
from subprocess import call
import os
# Clears the screen of cluttered text
call('clear' if os.name == 'posix' else 'cls')
# Define Menu class and function/method
class Menu:
def __init__(self):
pass
def menu(self):
self.menuitems = {'coffee':1.25, 'tea': 1, 'coke':1.75, 'water':.5}
return self.menuitems
# Define tax class and function/method
class AddTax:
def add_tax(self, total, tax):
return total*tax
# Define discount class and function/method
class Discount:
def discount(self, total, discount_amount=0):
return total*discount_amount
# class and function/method for displaying text
class Display:
def output(self, text):
print(text.title())
# Controller text for various operations
class Controller:
def __init__(self, menu=None, addtax=None, discount=None, display=None):
self.menu = menu or Menu()
self.addtax = addtax or AddTax()
self.discount = discount or Discount()
self.display = display or Display()
# Function/method for displaying menu items
def show_menu(self):
stuff = []
for key, val in self.menu.menu().items():
stuff.append(f'{key}: ${format(val, "0.2f")}')
self.display.output(f'Menu: {", ".join(stuff)}')
# Define main
def main():
# Initiate the controller class
controller = Controller()
# Start the loop
while True:
try:
# Print out message
print('What would you like to drink?')
print('Format is item amount seperated by comma or space.')
print('Example: coke 3, tea 1')
# Add a blank line
print()
# Show the menu
controller.show_menu()
# Ask for order. Can be comma seperated or space
# Examples coke 2, tea 2 or coke 2 tea 2
order = input('>> ').lower().replace(',',' ').split()
# Start a loop for asking if a member. Returns either True or False
while True:
print('Are you a member? (y/n)')
member = input('>>> ')
print()
if member.strip() == 'y' or member.strip() == 'yes':
membership = True
break
else:
membership = False
break
# Prints the top part of receipt
print(f"{''.ljust(25,'-')} Receipt {''.rjust(25,'-')}")
# This check to make sure that an item and amount is entered
# If not throws error and ask for correct input
if order and len(order) >= 2:
# Places the input into a dict for later use
iters = iter(order)
result = dict(zip(iters, iters))
# Iniate a couple of list for storing
total = []
combined_total = []
# Loop through our input and check if items are in our menu
# Display accordingly
for menuitem, amount in result.items():
if menuitem in controller.menu.menu().keys():
# Stores the cost of items .Gets the cost.
# Gets the cost
# Formats totalcost for display
# Stores combined total for item groups
total.append(controller.menu.menu()[menuitem])
cost = controller.menu.menu()[menuitem]
totalcost = f"{format(cost*int(amount.replace(',', ' ')), '0.2f')}"
combined_total.append(float(totalcost))
# Display items ordered
if len(menuitem) < len(max(result)):
npad = (len(max(result))-len(menuitem))+len(min(result))
else:
npad = len(max(result))
controller.display.output(f'Item: {" ":>4} {menuitem} {"".rjust(npad," ")}Amount: {amount.replace(",","")} {" ":>2} Cost: ${format(cost, "0.2f")} {" ":>2} Total: ${totalcost}')
else:
controller.display.output(f'Item: {menuitem} is not on the menu.')
# Print a blank line
print()
# Get a total cost of all ordered items
total = sum(combined_total)
# If a member add the discount
if membership == True:
discount = controller.discount.discount(total, 0.15)
else:
discount = 0
# Get a new total with the discount
newtotal = total-discount
# Get the tax on our total
tax = controller.addtax.add_tax(newtotal, 0.09)
# Add the tax to our total
finaltotal = newtotal+tax
# Display the content
controller.display.output(f'Total: {"".rjust(3," ")} ${format(total,"0.2f")}')
controller.display.output(f'Discount: -${format(discount, "0.2f")}')
controller.display.output(f'Total: {"".rjust(3," ")} ${format(newtotal,"0.2f")}')
controller.display.output(f'Tax: {"".rjust(4," ")} +${format(tax,"0.2f")}')
controller.display.output(f'Total: {"".rjust(3," ")} ${format(finaltotal,"0.2f")}')
print(f'{"".rjust(59, "-")}')
break
else:
print('You will need to enter both item and amout.')
continue
except ValueError as error:
print('Please use the correct format when ordering.')
if __name__ == '__main__':
main()
output
Code:
What would you like to drink?
Format is item amount seperated by comma or space.
Example: coke 3, tea 1
Menu: Coffee: $1.25, Tea: $1.00, Coke: $1.75, Water: $0.50
>> Coffee 3, Coke 2, Water 1
Are you a member? (y/n)
>>> y
------------------------- Receipt -------------------------
Item: Coffee Amount: 3 Cost: $1.25 Total: $3.75
Item: Coke Amount: 2 Cost: $1.75 Total: $3.50
Item: Water Amount: 1 Cost: $0.50 Total: $0.50
Total: $7.75
Discount: -$1.16
Total: $6.59
Tax: +$0.59
Total: $7.18
-----------------------------------------------------------