100 lines
2.5 KiB
Python
100 lines
2.5 KiB
Python
# A classic budgeting app written in Python.
|
|
# Author: Tropii
|
|
import time
|
|
|
|
# Classes
|
|
class Items:
|
|
def __init__(self, name, price):
|
|
self.name = name
|
|
self.price = price
|
|
def __str__(self):
|
|
return f"{self.name}: ${self.price}"
|
|
|
|
user_items = []
|
|
user_budget = 0
|
|
|
|
# Functions
|
|
|
|
def add_item(name: str, price: float) -> None:
|
|
item = Items(name, price)
|
|
|
|
user_items.append(item)
|
|
print("Item added")
|
|
time.sleep(2)
|
|
return
|
|
|
|
def remove_item(name: str) -> None:
|
|
for i in range(len(user_items)):
|
|
if user_items[i].name == name:
|
|
user_items.pop(i)
|
|
print("Item removed")
|
|
time.sleep(2)
|
|
return
|
|
else:
|
|
print("Item could not be found")
|
|
time.sleep(2)
|
|
return
|
|
|
|
def get_all_items() -> None:
|
|
if len(user_items) == 0:
|
|
print("You have no items yet.")
|
|
time.sleep(2)
|
|
return
|
|
|
|
for i in range(len(user_items)):
|
|
print(f"Item Name: {user_items[i].name}, Price: {user_items[i].price}\n")
|
|
|
|
time.sleep(2)
|
|
|
|
def calculate_balance() -> None:
|
|
total_costs = 0
|
|
for i in range(len(user_items)):
|
|
total_costs += user_items[i].price
|
|
|
|
balance = round(user_budget - total_costs, 2)
|
|
|
|
if balance == 0:
|
|
print("You have enough money for all your items.")
|
|
time.sleep(2)
|
|
elif balance > 0:
|
|
print(f"You have ${"%.2f" % balance} left over after purchasing your items")
|
|
time.sleep(2)
|
|
elif balance < 0:
|
|
print(f"You do not have enough money for these items. You are ${"%.2f" % balance} short..")
|
|
time.sleep(2)
|
|
|
|
|
|
while True:
|
|
print("""
|
|
Welcome to the Budgeting App!
|
|
1. Set budget
|
|
2. View budget
|
|
3. Add item
|
|
4. Remove item
|
|
5. View items
|
|
6. Calculate Costs
|
|
7. Exit
|
|
""")
|
|
choice = input("Enter your choice: ")
|
|
if choice == "1":
|
|
user_budget = int(input("Enter a budget (Must be a valid number): "))
|
|
if user_budget <= 0:
|
|
print("Invalid budget number.")
|
|
elif choice == "2":
|
|
print(f"Current budget: {user_budget}")
|
|
elif choice == "3":
|
|
item_name = input("Enter the item name: ")
|
|
item_price = float(input(f"Enter the price for {item_name}: "))
|
|
|
|
add_item(item_name, item_price)
|
|
elif choice == "4":
|
|
item_name = input("Enter the name of the item you want to delete: ")
|
|
|
|
remove_item(item_name)
|
|
elif choice == "5":
|
|
get_all_items()
|
|
elif choice == "6":
|
|
calculate_balance()
|
|
elif choice == "7":
|
|
break
|