|
| 1 | +class Category: |
| 2 | + def __init__(self, name): |
| 3 | + # iniitalizing catagory object |
| 4 | + self.name = name |
| 5 | + self.ledger = [] |
| 6 | + |
| 7 | + def deposit(self, amount, description=""): |
| 8 | + # Add a deposit to the ledger |
| 9 | + self.ledger.append({"amount": amount, "description": description}) |
| 10 | + |
| 11 | + def withdraw(self, amount, description=""): |
| 12 | + # Add a withdrawal to the ledger |
| 13 | + if self.check_funds(amount): |
| 14 | + self.ledger.append({"amount": -amount, "description": description}) |
| 15 | + return True |
| 16 | + else: |
| 17 | + return False |
| 18 | + |
| 19 | + def get_balance(self): |
| 20 | + # Calculate the current balance |
| 21 | + return sum(item["amount"] for item in self.ledger) |
| 22 | + |
| 23 | + def transfer(self, amount, category): |
| 24 | + # Transfer funds from this category to another category |
| 25 | + if self.check_funds(amount): |
| 26 | + self.withdraw(amount, f"Transfer to {category.name}") |
| 27 | + category.deposit(amount, f"Transfer from {self.name}") |
| 28 | + return True |
| 29 | + else: |
| 30 | + return False |
| 31 | + |
| 32 | + def check_funds(self, amount): |
| 33 | + # Check if there are enough funds for a given amount |
| 34 | + return amount <= self.get_balance() |
| 35 | + |
| 36 | + def __str__(self): |
| 37 | + # Create a string representation of the Category object for printing |
| 38 | + title = f"{self.name:*^30}\n" # Center the category name |
| 39 | + items = "" |
| 40 | + total = 0 |
| 41 | + # Add each ledger item to the string representation |
| 42 | + for item in self.ledger: |
| 43 | + items += f"{item['description'][:23]:23}{item['amount']:>7.2f}\n" # Format description and amount |
| 44 | + total += item["amount"] # Calculate the total amount |
| 45 | + output = title + items + "Total: " + str(total) # Combine title, items, and total |
| 46 | + return output |
| 47 | + |
| 48 | + |
| 49 | +def create_spend_chart(categories): |
| 50 | + # Function to create a spend chart based on a list of categories |
| 51 | + category_names = [] |
| 52 | + spent = [] |
| 53 | + spent_percentages = [] |
| 54 | + |
| 55 | + # Calculate total spent for each category and store category names |
| 56 | + for category in categories: |
| 57 | + total_spent = 0 |
| 58 | + for item in category.ledger: |
| 59 | + if item["amount"] < 0: |
| 60 | + total_spent -= item["amount"] |
| 61 | + spent.append(round(total_spent, 2)) |
| 62 | + category_names.append(category.name) |
| 63 | + |
| 64 | + # Calculate spent percentages for each category |
| 65 | + for amount in spent: |
| 66 | + spent_percentages.append(round(amount / sum(spent), 2) * 100) |
| 67 | + |
| 68 | + graph = "Percentage spent by category\n" |
| 69 | + labels = range(100, -10, -10) |
| 70 | + |
| 71 | + # Generate the graph based on spent percentages |
| 72 | + for label in labels: |
| 73 | + graph += str(label).rjust(3) + "| " |
| 74 | + for percent in spent_percentages: |
| 75 | + if percent >= label: |
| 76 | + graph += "o " |
| 77 | + else: |
| 78 | + graph += " " |
| 79 | + graph += "\n" |
| 80 | + |
| 81 | + graph += " ----------\n " |
| 82 | + |
| 83 | + # Add category names below the graph |
| 84 | + longest_name_length = max([len(name) for name in category_names]) |
| 85 | + for i in range(longest_name_length): |
| 86 | + for name in category_names: |
| 87 | + if len(name) > i: |
| 88 | + graph += name[i] + " " |
| 89 | + else: |
| 90 | + graph += " " |
| 91 | + if i < longest_name_length - 1: |
| 92 | + graph += "\n " |
| 93 | + |
| 94 | + return graph |
| 95 | + |
| 96 | +# Example usage |
| 97 | +food = Category("Food") |
| 98 | +food.deposit(1000, "deposit") |
| 99 | +food.withdraw(10.15, "groceries") |
| 100 | +food.withdraw(15.89, "restaurant and more food for dessert") |
| 101 | +clothing = Category("Clothing") |
| 102 | +food.transfer(50, clothing) |
| 103 | +print(food) |
| 104 | + |
| 105 | +# Example of create_spend_chart |
| 106 | +categories = [food, clothing] |
| 107 | +print(create_spend_chart(categories)) |
0 commit comments