Assignment 2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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
vuducly151092@gmail.com

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