Answer to Question #301098 in Python for H A

Question #301098

BankAccount class

In the main code:

  • use a loop to input() four accounts into a list
  • use a loop to print all accounts in the list

In the class, add methods to:

  • calculate and return the totalDeposits() for all accounts
  • find and return the name of the large account
1
Expert's answer
2022-02-22T15:14:17-0500


class BankAccount:
    # Initializing
    def __init__(self,name,balance):
        self.name=name
        self.balance=balance


    def getName(self):
        return self.name


    def getBalance(self):
        return self.balance


    
class BankAccountList:
    # Initializing
    def __init__(self):
        self.listBankAccounts =[]


    def addBankAccountToList(self,bankAccount):
        self.listBankAccounts.append(bankAccount)


    #calculate and return the totalDeposits() for all accounts
    def calculateTotalDeposits(self):
        sumResult=0
        for account in self.listBankAccounts:
            sumResult+=account.getBalance()
        return sumResult
            
    #find and return the name of the large account
    def getnameLargeAccount(self):
        largeAccount=self.listBankAccounts[0]
        for account in self.listBankAccounts:
            if account.getBalance()>largeAccount.getBalance():
                largeAccount=account
        return largeAccount




    def displayAllBankAccounts(self):
        for account in self.listBankAccounts:
             print(f"The BankAccount name: {account.getName()}")
             print(f"The BankAccount balance: {account.getBalance()}")
             
        print(f"The total deposits for all accounts: {self.calculateTotalDeposits()}")
        print(f"The name of the large account: {self.getnameLargeAccount().getName()} balance: {self.getnameLargeAccount().getBalance()}")
               
    


n =int(input("Enter number of bank accounts: "))
ba=BankAccountList()
for i in range(0,n):
    name=input("Enter name: ")
    balance=float(input("Enter balance: "))
    b=BankAccount(name,balance)
    ba.addBankAccountToList(b)
ba.displayAllBankAccounts()
    

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog