0% found this document useful (0 votes)
31 views

Assignment 2

The document contains a Python program to simulate basic banking functions like depositing, withdrawing, and viewing a current balance or transaction history. It defines a BankAccount class with methods to add or subtract money from the balance attribute and record transactions, then uses a loop to prompt a user to choose an action and interact with an account object. The user is able to deposit, withdraw, check their balance, and see a log of changes to the account balance over the session.

Uploaded by

Quốc Hưng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Assignment 2

The document contains a Python program to simulate basic banking functions like depositing, withdrawing, and viewing a current balance or transaction history. It defines a BankAccount class with methods to add or subtract money from the balance attribute and record transactions, then uses a loop to prompt a user to choose an action and interact with an account object. The user is able to deposit, withdraw, check their balance, and see a log of changes to the account balance over the session.

Uploaded by

Quốc Hưng
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Name: Lê Quốc Hưng

Student ID: 10423050

1. Writing a small banking program to do the followings:


a. Adding an amount of money to the current balance.
b. Subtracting an amount from the current balance
c. Viewing the current balance
d. Printing out the list of changes in the balance
i. Example: at 10:00 26/10/2023: Withdraw 10$ from the current balance
ii. Example: at 11:00 26/10/2023: Add 10% to the current balance

Please paste the screenshots of your solution to this file and send the file to the email
[email protected]

import datetime

class BankAccount:
def __init__(self, balance):
self.balance = balance
self.transactions = []

def add_money(self, amount):


self.balance += amount
self.transactions.append(f"{datetime.datetime.now()}: Added
{amount}vnd to the current balance")

def subtract_money(self, amount):


if amount > self.balance:
print("Not enough money. Withdrawal transaction failed.")
return
self.balance -= amount
self.transactions.append(f"{datetime.datetime.now()}: Subtracted
{amount}vnd from the current balance")

def view_balance(self):
return f"Current Balance: ${self.balance}"

def print_transactions(self):
return "\n".join(self.transactions)
account = BankAccount(1000)
while True:
print("1. Deposit money into the balance")
print("2. Withdraw money from the balance")
print("3. View current balance")
print("4. View balance change history")
print("5. Exit")

choice = input("Choose an option (1/2/3/4/5): ")

if choice == '1':
deposit_amount = float(input("Enter the amount you want to deposit
into the balance: "))
print(account.add_money(deposit_amount))
elif choice == '2':
withdraw_amount = float(input("Enter the amount you want to withdraw
from the balance: "))
print(account.subtract_money(withdraw_amount))
elif choice == '3':
print(account.view_balance())
elif choice == '4':
print(account.print_transactions())
elif choice == '5':
print("Thank you for using the banking service.")
break
else:
print("Invalid option. Please choose again.")

You might also like