In Python-- Write a class called Account that has two instance variables, namely
ID: 3771876 • Letter: I
Question
In Python-- Write a class called Account that has two instance variables, namely, accountNumber and balance. Provide the following:
a) An init() method that takes two parameters, accountNumber and initialBalance, and does the following:
i) Sets accountNumber using the accountNumber.
ii) Sets balance using the parameter, initialBalance.
b) A withdraw() method that takes an amount as its parameter and performs the withdraw transaction. Your method should ensure that the balance does not drop below 0.
c) A deposit() method that takes an amount as its parameter and performs the deposit transaction.
d) A __str__() method that returns an appropriate string (i.e., displays account number and balance) that represents the account object. Note: The keyword self has to be used correctly.
Explanation / Answer
Python Code:
class Account:
accountNumber = 0;
balance = 0.0;
def __init__(self,accountNumber, balance):
self.accountNumber = accountNumber;
self.balance = balance;
def withdraw(self,amount):
if self.balance - amount < 0:
print("Insufficient Funds... Transaction can't be processed");
else:
self.balance = self.balance - amount;
def deposit(self,amount):
self.balance = self.balance + amount;
def __str__(self):
return " Account Number: " + str(self.accountNumber) + " Balance: " + str(self.balance);
acc = Account(123456, 6000);
acc.withdraw(2000);
acc.deposit(4000);
print(acc);
-----------------------------------------------------------------------------------------------------------------------------------------------
Sample Run:
C:Python33>python qa_14_12_2.py
Account Number: 123456 Balance: 8000
C:Python33>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.