Skip to content

Instantly share code, notes, and snippets.

@andrewflash
Created May 4, 2023 14:28
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 andrewflash/2317a25057e67a0da5d83db6d94e001b to your computer and use it in GitHub Desktop.
Save andrewflash/2317a25057e67a0da5d83db6d94e001b to your computer and use it in GitHub Desktop.
Supermarket Simulation in Python
# Function to display greetings and show some promotional items and its price
def display_greetings(name):
print(f"Welcome to {name} supermarket! Today's promotional items are:")
print("----------------------------------------------------------")
print()
# Function to display items
def display_items(msg, items):
print(f"{msg}")
print()
print("No.\tNama\tHarga")
print("----------------------------")
for idx, item_dic in enumerate(items):
price = format_currency(item_dic['harga'])
print(f"{idx+1}\t{item_dic['item']}\t{price:>12}")
print()
# Function to get the user input for either promotional or all items
def get_user_input():
user_input = input("Do you want to see all items or promotional items only? Type 'all' or 'promotional': ")
while user_input not in ["all", "promotional"]:
user_input = input("Invalid input. Please type 'all' or 'promotional': ")
return user_input
# Function to get the user input what they want to purchase
def get_user_purchase(items):
user_purchase = {}
while True:
item_input = input("\nEnter the item number you want to purchase (press 'q' to finish): ")
if item_input.lower() == "q":
break
elif not item_input.isnumeric() or int(item_input) < 1 or int(item_input) > len(items):
print("Invalid input. Please enter a valid item number or press 'q' to finish.")
continue
item_index = int(item_input) - 1
item_name = items[item_index]['item']
item_quantity = input(f"How many {item_name} do you want to buy? ")
while not item_quantity.isnumeric():
item_quantity = input(f"Invalid input. Please enter a valid quantity for {item_name}: ")
item_quantity = int(item_quantity)
user_purchase[item_name] = user_purchase.get(item_name, 0) + item_quantity
return user_purchase
# Function to calculate the total price of the user's purchase
def calculate_total_price(user_purchase, all_items):
total_price = 0
purchase_details = []
for item, quantity in user_purchase.items():
item_dic = {}
item_price = [x['harga'] for x in all_items if x['item'] == item][0]
item_total_price = item_price * quantity
total_price += item_total_price
item_dic['item'] = item
item_dic['quantity'] = quantity
item_dic['item_price'] = item_price
purchase_details.append(item_dic)
return total_price, purchase_details
# Function to display total price and details
def display_checkout(total_price, purchase_details):
print("\nHere are the items you want to buy and its total price:")
print("No.\tNama\tSatuan Harga\tQty\tTotal Harga")
print("----------------------------------------------------")
for idx, item_dic in enumerate(purchase_details):
item_price = format_currency(item_dic['item_price'])
item_total_price = format_currency(item_dic['item_price']*item_dic['quantity'])
print(f"{idx+1}\t{item_dic['item']}\t{item_price:>12}\t{item_dic['quantity']}\t{item_total_price:>12}")
total_price = format_currency(total_price)
print(f"\nTotal price:\t\t\t\t{total_price:>12}")
# Function to format currency
def format_currency(price, decimal_separator = ".", currency = "Rp."):
if decimal_separator == '.':
return "{}{:,}".format(currency, price).replace(',', '.')
else:
return "{}{:,}".format(currency, price)
# Define the main function
def main():
# Define list of all items
all_items = [
{"item": "susu", "harga": 50000},
{"item": "daging", "harga": 20000},
{"item": "lampu", "harga": 15000},
{"item": "masker", "harga": 25000},
{"item": "apel", "harga": 30000}
]
# Define list of promotional items
promotional_items= [
{"item": "susu", "harga": 50000},
{"item": "masker", "harga": 25000}
]
supermarket_name = "ABC"
display_greetings(supermarket_name)
display_items("Today's promotional items are:", promotional_items)
user_input = get_user_input()
if user_input == "promotional":
display_items("Here are our promotional items:", promotional_items)
user_purchase = get_user_purchase(promotional_items)
else:
display_items("Here are all the items available in our supermarket:", all_items)
user_purchase = get_user_purchase(all_items)
total_price, purchase_details = calculate_total_price(user_purchase, all_items)
display_checkout(total_price, purchase_details)
# Call the main function
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment