In: Computer Science
NEED TO BE IN PYTHON!!!
Make sure to put the main section of your code in the following if
block:
# Type code for classes here
if __name__ == "__main__":
# Type main section of code here
(1) Build the Account class with the following specifications:
Attributes
Create a constructor that has 3 parameters (in addition to self) that will be passed in from the user. (no default values)
Define a __str__() method to print an Account like the following
Account Name: Trish Duce
Account Number: 90453889
Account Balance: $100.00
Define a deposit() method that has 1 parameter (in addition to self) that will be passed in from the user. (no default values) The method deposits the specified amount in the account.
Define a withdraw() method that has 2 parameters (in addition to self) that will be passed in from the user. (no default values) The method withdraws the specified amount (1st parameter) and fee (2nd parameter) from the account.
(2) In the main section of your code, create 3 accounts.
(3) Print the 3 accounts using print(acct1)…
(4) Deposit 25.85, 75.50 and 50 into accounts 1, 2 and 3.
(5) Print the 3 accounts.
(6) Withdraw 25.85 (2.50 fee), 75.50 (1.50 fee), 50.00 (2.00 fee).
(7) Print the 3 accounts.
Output should look like the following:
Account Name: Trish Duce
Account Number: 90453889
Account Balance: $100.00
Account Name: Donald Duck
Account Number: 83504837
Account Balance: $100.00
Account Name: Joe Smith
Account Number: 74773321
Account Balance: $100.00
Account Name: Trish Duce
Account Number: 90453889
Account Balance: $125.85
Account Name: Donald Duck
Account Number: 83504837
Account Balance: $175.50
Account Name: Joe Smith
Account Number: 74773321
Account Balance: $150.00
Account Name: Trish Duce
Account Number: 90453889
Account Balance: $97.50
Account Name: Donald Duck
Account Number: 83504837
Account Balance: $98.50
Account Name: Joe Smith
Account Number: 74773321
Account Balance: $98.00
Answer:
class BankAccount:
#constructor with 3 parameters for account name,number and balance
def __init__(self, name,number,balance):
self.name = name
self.number=number
self.balance = balance
#function to deposit an amount
def deposit(self, amount):
"""make a deposit"""
self.balance += amount
#function to withdraw the amount with small fee deduction
def withdraw(self, amount,fee):
"""make a withdraw"""
if amount > self.balance:
raise ValueError("Insufficient funds")
self.balance -= amount
self.balance -= fee
def get_accname(self):
return self.name
def get_accnumber(self):
return self.number
def get_accbalance(self):
"""check the balance"""
return self._balance
#__str__ prints the statement to console a string
def __str__(self):
return 'Account Name: {} \nAccount Number: {} \nAccount Balance: ${:,.2f}'.format(self.name,self.number,self.balance)
if __name__ == "__main__":
#create 3 accounts and print them
acct1=BankAccount('Trish Duce', 90453889, 100)
print(acct1)
acct2 = BankAccount('Donald Duck', 83504837, 100)
print(acct2)
acct3 = BankAccount('Joe Smith', 74773321, 100)
print(acct3)
#deposit to 3 accounts
acct1.deposit(25.85)
acct2.deposit(75.50)
acct3.deposit(50)
#print the 3 accounts
print(acct1)
print(acct2)
print(acct3)
#withdraw from the 3 accounts
acct1.withdraw(25.85,2.50)
acct2.withdraw(75.50,1.50)
acct3.withdraw(50,2.00)
# print the 3 accounts
print(acct1)
print(acct2)
print(acct3)
Output:
