Imagine you have to develop software for a bank to administer the accounts of their customers.
The requirements are to:
- deposit money into a given account.
- deduct money from a given account.
- show the current balance of a given account.
- show the bank statement of a given account.
- transfer money between two different accounts.
A programmer used ChatGPT to generate this code as a possible solution:
class BankAccount:
def __init__(self, owner_name, dob, bank_name, account_no):
self.owner_name = owner_name
self.dob = dob
self.bank_name = bank_name
self.account_no = account_no
self.balance = 0
self.transactions = []
def deposit(self, amount):
self.balance += amount
self.transactions.append(f"Deposit: +{amount}")
def withdraw(self, amount):
if self.balance >= amount:
self.balance -= amount
self.transactions.append(f"Withdrawal: -{amount}")
else:
print("Insufficient funds.")
def transfer(self, amount, recipient_account):
if self.balance >= amount:
self.balance -= amount
self.transactions.append(f"Transfer to {recipient_account.owner_name}: -{amount}")
recipient_account.deposit(amount)
else:
print("Insufficient funds.")
def get_balance(self):
return self.balance
def get_bank_statement(self):
statement = f"Bank Statement for {self.owner_name} ({self.account_no})\n"
statement += f"Bank: {self.bank_name}\n"
statement += "Transactions:\n"
statement += "\n".join(self.transactions)
return statement
sujal = BankAccount("Sujal", "1990-01-01", "ABC Bank", "1234567890")
rashi = BankAccount("Rashi", "1995-05-05", "XYZ Bank", "0987654321")
sujal.deposit(500)
sujal.transfer(200, rashi)
rashi.withdraw(100)
print(sujal.get_bank_statement())
print("Rashi's current balance:", rashi.get_balance())