I'll guide you through implementing this BankAccount class and its subclass SavingsAccount in Python. The BankAccount class will be fairly simple, just tracking a balance and a history of transactions. The SavingsAccount class will add functionality for managing allotments as described, including methods to initialize, plan savings, show the distribution of savings in different categories through a bar chart, and for extra credit, present the distribution as a pie chart.
Let's start with the implementation of the base BankAccount class:
class BankAccount:
def __init__(self):
self.balance = 0
self.history = []
def deposit(self, amount):
if amount <= 0:
return "Invalid amount"
self.balance += amount
self.history.append(('Deposit', amount))
def withdraw(self, amount):
if amount <= 0:
return "Invalid amount"
if amount > self.balance:
return "Insufficient funds"
self.balance -= amount
self.history.append(('Withdrawal', amount))
def get_balance(self):
return self.balance
def get_history(self):
return self.history
Now, let's move to the SavingsAccount class which will extend BankAccount. We will also include the visualization functionality using matplotlib for plotting the distributions as bar and pie charts:
import matplotlib.pyplot as plt
class SavingsAccount(BankAccount):
def __init__(self):
super().__init__() # Initialize balance and history from parent class
self.allotment = {} # For storing the (category, value) pairs
def initialize(self):
self.balance = 0
self.history = []
self.allotment = {}
def plan_savings(self, factor: int, reason: str):
self.allotment[reason] = factor
def show_distribution(self):
labels = list(self.allotment.keys())
factors = list(self.allotment.values())
total_factor = sum(factors)
amounts = [(self.balance * factor / total_factor) for factor in factors]
plt.figure(figsize=(10, 5))
plt.bar(labels, amounts, color='skyblue')
plt.xlabel('Categories')
plt.ylabel('Amount ($)')
plt.title('Distribution of Savings')
plt.show()
def show_percentage_distribution(self):
labels = list(self.allotment.keys())
factors = list(self.allotment.values())
total_factor = sum(factors)
sizes = [(factor / total_factor) * 100 for factor in factors]
fig1, ax1 = plt.subplots()
ax1.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.title('Percentage Distribution of Savings')
plt.show()
To use the SavingsAccount class, make sure you have matplotlib installed in your Python environment. You can install it using pip:
pip install matplotlib
Here is a quick example demonstrating how to create a SavingsAccount instance, plan for some savings, and visualize the distribution:
# Create a new savings account
account = SavingsAccount()
# Add some balance
account.deposit(1000)
# Plan savings
account.plan_savings(10, 'education')
account.plan_savings(30, 'marriage')
# Show the distribution of funds towards different goals
account.show_distribution()
# For extra credit: Show the percentage distribution
account.show_percentage_distribution()
Remember, the visualization will work if you're using this code in an environment that supports displaying graphical output, such as Jupyter notebooks or directly within a Python interactive shell that has GUI support.