Day 65 - OOP ATM Simulator - Part 1: Parent and Child Objects

Today, we will start a new project—an object-oriented ATM Simulator. First, we will develop a parent object to represent a generic bank account. Then, we will create child classes that represent Savings accounts and Checking accounts.

Finished Code

class bankAccount:
    def __init__(self, strN):
        self.strName = strN
        self.flBal = 0

    def getBalance(self):
        return self.flBal

    def getName(self):
        return self.strName

    def doDeposit(self):
        strDeposit = input("How much would you like to deposit?").strip()
        strDepositCheck = strDeposit.replace(".", "", 1)
        if strDepositCheck.isnumeric():
            flDep = float(strDeposit)
            self.flBal += flDep
            print(f"You have deposited {flDep} in {self.strName}.\n")
        else:
            print("Please enter a valid decimal number.\n")

    def doWithdrawal(self):
        strWith = input("How much would you like to withdraw?").strip()
        strWithCheck = strWith.replace(".", "", 1)
        if strWithCheck.isnumeric():
            flWith = float(strWith)
            if self.flBal >= flWith:
                self.flBal -= flWith
                print(f"You have withdrawn {flWith:.2f} from {self.strName}.\n")
            else:
                print("You don't have enough money in your account.")
        else:
            print("Please enter a valid decimal number.\n")

class savingsAccount(bankAccount):
    def __init__(self, strN):
        super().__init__(strN)

    def __str__(self):
        return f"{self.strName}".ljust(25,".")+"Savings".ljust(10,".")+f"${self.flBal:.2f}".rjust(25, ".")

class checkingAccount(bankAccount):
    def __init__(self, strN):
        super().__init__(strN)
        self.intCheckNo = 1000

    def __str__(self):
        return f"{self.strName}".ljust(25,".")+"Checking".ljust(10,".")+f"${self.flBal:.2f}".rjust(25, ".")


    def writeCheck(self):
        strWith = input("How much is the check for? ").strip()
        strWithCheck = strWith.replace(".", "", 1)
        if strWithCheck.isnumeric():
            flWith = float(strWith)
            if self.flBal >= flWith:
                self.flBal -= flWith
                print(f"You have written check number {self.intCheckNo} for ${flWith:.2f} from {self.strName}.\n")
                self.intCheckNo += 1
            else:
                print("You don't have enough money in your account.")
        else:
            print("Please enter a valid decimal number.\n")



myAccount = checkingAccount("Main Checking")
print(myAccount)
myAccount.doDeposit()
print(myAccount)
myAccount.writeCheck()
print(myAccount)