How do I extract class object's attribute values and write a file with each object's values in a new line?

Viewed 9

I have created a bank system with three different accounts. I want to write the ending values of each object's attribute's values to a file. Each object's values goes into a new line. I cannot get my head around it. Can anybody please provide some insight?

class Bank:
    bankAccounts=[]
    _acFind=False
    def withdrawFromAccount(_acNum):
        for i in range(len(Bank.bankAccounts)):
            _tempAc=Bank.bankAccounts[i]
            if _tempAc.acNum==_acNum:
                _tempAc.withdraw()
                Bank._acFind=True
        if Bank._acFind==False:
            print("Account Not Found")

# Creation of SuperClass: Account with common attributtes of all the SubClasses
class Account:
    def __init__(self,_acNum,_acName,_acType,_balance=0):
        self.acNum=str(_acNum)
        self.acName=str(_acName)
        self.acType=str(_acType)
        self.acBalance=float(_balance)
    
    def deposit(self):
        _amount=float(input("Enter the Amount to Deposit: "))
        self.acBalance+=_amount
        print(f"Amount Deposited: {_amount}\nTotal Balance: {self.acBalance}")
    
    def withdraw(self):
        _amount=float(input("Enter the Amount to withdraw: "))
        if _amount<=self.acBalance:
            self.acBalance-=_amount
            print (f"Amount Withdrawn: {_amount}\nRemaining Balance: {self.acBalance}")
        else:
            print ("Insufficient Balance")
    
    def getInfo(self):
        print(f"Account Number: {self.acNum}\nAccount Name: {self.acName}\nAccount Type: {self.acType.upper()}\nAccount Balance: {round(self.acBalance,2)}")
    
# Creation of Subclass: Deposit Account
class DepositAccount(Account):
    # This subclass has additional attribute: Interest Rate and Withdrawal Limit
    def __init__(self, _acNum, _acName, _acType, _intRate,_acBalance=0):
        super().__init__(_acNum, _acName, _acType, _acBalance)
        self.intRate=_intRate
    
    def deposit(self):
        return super().deposit()

    def withdraw(self):
        # Withdrawal Limit is set to 2 per account for Deposit Account Types
        _timesWd=int(input("Enter the number of times you want to withdraw: "))
        if _timesWd<3:
            for i in range(_timesWd):
                super().withdraw()
                print("----------------------------------------------------------")
        else:
            print("Withdraw Limit Exceeded for Deposit Account.\nThe Limit is 2.")
    
    def getInfo(self):
        print(f"Account Number: {self.acNum}\nAccount Name: {self.acName}\nAccount Type: {self.acType.upper()}\nAccount Balance: {round(self.acBalance,2)}\nInterest Rate: {self.intRate}")

# Creation of SubClass: Current Account
# Current Account is a Super Class for Restricted Account and Overdraft Account
class CurrentAccount(Account):
    def __init__(self, _acNum, _acName, _acType, _chqBook=20, _balance=0):
        super().__init__(_acNum, _acName, _acType, _balance)
        self.chqBook=_chqBook
    
    def deposit(self):
        return super().deposit()
    
    def withdraw(self):
        print("Withdrawal Denied for this Account Type.")

class RestrictedAccount(CurrentAccount):
    def __init__(self, _acNum, _acName, _acType="R", _wdLimit=10000, _chqBook=20, _balance=0):
        super().__init__(_acNum, _acName, _acType, _chqBook, _balance)
        self.wdLimit=float(_wdLimit)
    
    def deposit(self):
        return super().deposit()
    
    def withdraw(self):
        _numchq=int(input("Enter the Number of Cheques to Withdraw: "))
        if _numchq<=self.chqBook:
            for i in range(_numchq):
                _amount=float(input("Enter the amount to be withdrawn: "))
                if _amount<=self.wdLimit:
                    if _amount<=self.acBalance:
                        self.acBalance-=_amount
                        self.chqBook-=1
                        print("----------------------------------------------------------")
                        print("Cheque Approved.")
                        print(f"Amount Withdrawn: {_amount}\nCheques Left: {self.chqBook}\nRemaining Balance: {self.acBalance}")
                        print("----------------------------------------------------------")
                    else:
                        print("----------------------------------------------------------")
                        print("Insufficient Balance.")
                else:
                    print("----------------------------------------------------------")
                    print("---------------------INVALID CHEQUE-----------------------")
                    print("----------------------------------------------------------")
                    print("\n")
                    print("Withdraw Amount Greater Than the Withdraw Limit.\n\n")
                    print("----------------------------------------------------------")
        else:
            print("----------------------------------------------------------")
            print("Insufficient Cheques for the Account.")
    
    def getInfo(self):
        print(f"Account Number: {self.acNum}\nAccount Name: {self.acName}\nAccount Type: {self.acType.upper()}\nAccount Balance: {round(self.acBalance,2)}\nNumber of Cheques: {self.chqBook}\nWithdraw Limit: {round(self.wdLimit,2)}")
    
class OverdraftAccount(Account):
    def __init__(self, _acNum, _acName, _chqBook=20, _acType="O",_odLimit=-20000, _balance=0):
        super().__init__(_acNum, _acName, _acType, _balance)
        self.chqBook=_chqBook
        self.odLimit=_odLimit
    
    def deposit(self):
        return super().deposit()

    def withdraw(self):
        _numchq=int(input("Enter the Number of Cheques to Withdraw: "))
        if _numchq<=self.chqBook:
            for i in range(_numchq):
                _amount=float(input("Enter the amount to be withdrawn: "))
                if (self.acBalance-_amount)>=-20000:
                    self.acBalance-=_amount
                    self.chqBook-=1
                    print("----------------------------------------------------------")
                    print("Cheque Approved.")
                    if self.acBalance<0:
                        print(f"Account Overdrawn. Maximum Overdraw Left: {-20000-(self.acBalance)}")
                        print(f"Amount Withdrawn: {_amount}\nCheques Left: {self.chqBook}\nAccount Balance: {self.acBalance}")
                    else:
                        print(f"Amount Withdrawn: {_amount}\nCheques Left: {self.chqBook}\nAccount Balance: {self.acBalance}")
                    print("----------------------------------------------------------")
                else:
                    print("----------------------------------------------------------")
                    print("---------------------INVALID CHEQUE-----------------------")
                    print("---------------Overdraft Limit is -20,000.00--------------")
                    print("----------------------------------------------------------")
        else:
            print("----------------------------------------------------------")
            print("Insufficient Cheques for the Account.")        


print("************************************************************************************")
print("**************************EXAMPLE OF DEPOSIT ACCOUNT********************************")
d1=DepositAccount("001","Ranjan","d",2.49)
print("----------------------------------------------------------")
d1.getInfo()
print("----------------------------------------------------------")
d1.deposit()
print("----------------------------------------------------------")
d1.withdraw()
print("----------------------------------------------------------")
d1.getInfo()
print("************************************************************************************")
print("\n\n")
Bank.bankAccounts.append(d1)

print("************************************************************************************")
print("**************************EXAMPLE OF RESTRICTED ACCOUNT*****************************")
r1=RestrictedAccount("002","Tripti","R")
r1.getInfo()
print("----------------------------------------------------------")
r1.deposit()
print("----------------------------------------------------------")
r1.getInfo()
print("----------------------------------------------------------")
r1.withdraw()
print("----------------------------------------------------------")
r1.getInfo()
print("************************************************************************************")
print("\n\n")
Bank.bankAccounts.append(r1)


print("************************************************************************************")
print("***************************EXAMPLE OF OVERDRAFT ACCOUNT*****************************")
o1=OverdraftAccount("003","Deepesh")
print("----------------------------------------------------------")
o1.getInfo()
print("----------------------------------------------------------")
o1.deposit()
print("----------------------------------------------------------")
o1.getInfo()
print("----------------------------------------------------------")
o1.withdraw()
print("----------------------------------------------------------")
print("************************************************************************************")
print("\n\n")
Bank.bankAccounts.append(o1)

_acNum=input("Enter the Account Number to Perform Withdrawal: ")
Bank.withdrawFromAccount(_acNum)

I want to write values of d1,r1 and o1 into a file with each object's values in a new line.

0 Answers
Related