Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ===============================================================================
- # CLASS DEFINITIONS
- # ===============================================================================
- # --------------------------------------------
- # Customer Class
- # Abstract: Contains methods and properties for working with objCustomer
- # --------------------------------------------
- class clsCustomer:
- # create list of accounts for customer
- lstAccounts = []
- # class constructor, creates instance of customer object
- def __init__(self, strFirstName, strLastName, strSSN):
- self.strFirstName = strFirstName
- self.strLastName = strLastName
- self.strSSN = strSSN
- # method to display combined balance of a given customer's accounts
- def displayTotalBalance(self):
- dblTotalBalance = sum(objAccount.dblBalance for objAccount in lstAccounts)
- # --------------------------------------------
- # Account Class
- # Abstract: Contains methods and properties for working with generic Account object
- # --------------------------------------------
- class clsAccount:
- # class constructor, initializes generic account object
- def __init__(self, strOwnerSSN, dblBalance, strAccountType, strAccountName, objCustomer):
- self.strOwnerSSN = strOwnerSSN
- self.dblBalance = dblBalance
- self.strAccountType = strAccountType
- self.strAccountName = strAccountName
- # add account to list of customer's accounts
- objCustomer.lstAccounts.append(self)
- # method to validate that input is numeric
- def isNumeric(self, strInput):
- try:
- float(strInput)
- return True
- except ValueError:
- print("ERROR: INPUT MUST BE NUMBERS ONLY.")
- pass
- # method to display balance of given account
- def displayBalance(self, strCustomerSSN, strAccountName):
- return self.dblBalance
- # method to validate deposits are numeric, positive, not zero
- def validateDeposit(self, dblAmount):
- if clsAccount.isNumeric(self, dblAmount) == True:
- if dblAmount == 0:
- print("ERROR: DEPOSIT AMOUNT CANNOT BE ZERO.")
- return False
- elif dblAmount < 0:
- print("ERROR: DEPOSIT AMOUNT CANNOT BE LESS THAN ZERO.")
- return False
- else:
- return True
- # method to deposit funds in checking or savings account
- def Deposit(self, dblAmount, strAccountName):
- if clsAccount.isNumeric(self, dblAmount) == True:
- if clsAccount.validateDeposit(self, dblAmount) == True:
- self.dblBalance += dblAmount
- # method to validate withdrawal amount is numeric, positive, not zero
- def validateWithdraw(self, dblAmount):
- if clsAccount.isNumeric(self, dblAmount) == True:
- if dblAmount == 0:
- print("ERROR: WITHDRAWAL AMOUNT CANNOT BE ZERO.")
- return False
- elif dblAmount < 0:
- print("ERROR: WITHDRAWAL AMOUNT CANNOT BE LESS THAN ZERO.")
- return False
- else:
- return True
- # method to withdraw funds from checking or savings account
- def Withdraw(self, dblAmount, objFromAccount):
- if clsAccount.isNumeric(self, dblAmount) == True:
- if clsAccount.validateWithdraw(self, dblAmount) == True:
- if objFromAccount.strAccountType == "savings" and objFromAccount.dblBalance - dblAmount < 500:
- print("ERROR: SAVINGS ACCOUNT BALANCE CANNOT BE LESS THAN $500.00")
- elif objFromAccount.strAccountType == "checking" and objFromAccount.dblBalance - dblAmount < 0:
- objFromAccount.dblBalance -= dblAmount
- clsCheckingAccount.applyOverdraftFee(self, 20, objFromAccount)
- else:
- objFromAccount.dblBalance -= dblAmount
- # method to validate transfer amount is positive, not zero
- def validateTransfer(self, dblAmount, strAccountName):
- if clsAccount.isNumeric(self, dblAmount) == True:
- if dblAmount == 0:
- print("ERROR: TRANSFER AMOUNT CANNOT BE ZERO.")
- return False
- elif dblAmount < 0:
- print("ERROR: TRANSFER AMOUNT CANNOT BE LESS THAN ZERO.")
- return False
- else:
- return True
- # method to transfer funds from one account to another
- # can transfer funds among one customer's account or between one
- # customer's account and another customer's account
- def transferFunds(self, dblAmount, objFromAccount, objToAccount, strCustomerSSN):
- if clsAccount.isNumeric(self, dblAmount) == True:
- if clsAccount.validateTransfer(dblAmount) == True:
- if objFromAccount.strAccountType == "savings" and objFromAccount.dblBalance - dblAmount < 500:
- print("ERROR: SAVINGS ACCOUNT BALANCE CANNOT BE LESS THAN $500.00")
- elif objFromAccount.strAccountType == "checking" and objFromAccount.dblBalance - dblAmount < 0:
- objToAccount.dblBalance += dblAmount
- objFromAccount -= dblAmount
- applyOverdraftFee(objFromAccount)
- else:
- objToAccount.dblBalance += dblAmount
- objFromAccount -= dblAmount
- # --------------------------------------------
- # Checking Account Subclass
- # Abstract: Contains methods and properties for working with Checking Account objects,
- # inherits clsAccount
- # --------------------------------------------
- class clsCheckingAccount(clsAccount):
- # class constructor, initializes Checking Account object
- # overrides parent class constructor
- def __init__(self, strOwnerSSN, dblBalance, strAccountType, strAccountName, objCustomer):
- clsAccount.__init__(self, strOwnerSSN, dblBalance, strAccountType, strAccountName, objCustomer)
- self.strOwnerSSN = strOwnerSSN
- self.dblBalance = dblBalance
- self.strAccountType = strAccountType
- self.strAccountName = strAccountName
- # method to apply overdraft fee to accounts meeting criteria
- def applyOverdraftFee(self, dblAmount, objAccount):
- # set value for overdraft fee
- dblOverdraftFee = float(20)
- # apply overdraft fee
- objAccount.dblBalance -= dblOverdraftFee
- # --------------------------------------------
- # Savings Account Subclass
- # Abstract: Contains methods and properties for working with Savings Account objects,
- # inherits clsAccount
- # --------------------------------------------
- class clsSavingsAccount(clsAccount):
- # class constructor, initializes Savings Account object
- # overrides parent constructor
- def __init__(self, strOwnerSSN, dblBalance, strAccountType, strAccountName, objCustomer):
- # validate that opening balance is >= required amount before initializing object
- if clsSavingsAccount.validateOpeningBalance(self, dblBalance) == True:
- clsAccount.__init__(self, strOwnerSSN, dblBalance, strAccountType, strAccountName, objCustomer)
- self.strOwnerSSN = strOwnerSSN
- self.dblBalance = dblBalance
- self.strAccountType = strAccountType
- self.strAccountName = strAccountName
- # method to validate deposit, transfer, and withdrawal amounts
- def validateOpeningBalance(self, dblAmount):
- if clsAccount.isNumeric(self, dblAmount) == True:
- if dblAmount == 0:
- print("ERROR: OPENING BALANCE CANNOT BE ZERO.")
- return False
- elif dblAmount < 0:
- print("ERROR: OPENING BALANCE CANNOT BE LESS THAN ZERO.")
- return False
- elif dblAmount < 500:
- print("ERROR: OPENING BALANCE MUST BE AT LEAST $500.00")
- return False
- else:
- return True
- # ===============================================================================
- # MAIN MODULE
- # ===============================================================================
- # use pretty print for test results
- import pprint
- # --------------------------------------------
- # Test 1:
- # Abstract: Show successful customer creation
- # --------------------------------------------
- print("==========================================================================")
- print("Test 1")
- Bob = clsCustomer("Bob", "Jones", "123456789")
- pprint.pprint(Bob.__dict__)
- print("==========================================================================")
- # --------------------------------------------
- # Test 2:
- # Abstract: Show successful customer creation + opening checking account
- # --------------------------------------------
- print("Test 2")
- Samantha = clsCustomer("Samantha", "Park", "987654321")
- SamanthaCheckingAccount = clsCheckingAccount("987654321", 300.00, "checking", "SamanthaCheckingAcct", Samantha)
- pprint.pprint(Samantha.__dict__)
- pprint.pprint(SamanthaCheckingAccount.__dict__)
- print("==========================================================================")
- # --------------------------------------------
- # Test 3:
- # Abstract: Show successful deposit to checking
- # --------------------------------------------
- print("Test 3")
- print("Samantha checking balance is", SamanthaCheckingAccount.dblBalance)
- SamanthaCheckingAccount.dblBalance += 500.00
- print("Samantha deposited $500.00. Balance is now", SamanthaCheckingAccount.dblBalance)
- print("==========================================================================")
- # --------------------------------------------
- # Test 4:
- # Abstract: Show failed attempt to create customer + open savings account,
- # due to insufficient funds
- # --------------------------------------------
- print("Test 4")
- Jess = clsCustomer("Jess", "Jones", "456789123")
- JessSavingsAccount = clsSavingsAccount("456789123", 200.00, "savings", "JessSavingsAccount", Jess)
- print("==========================================================================")
- # --------------------------------------------
- # Test 5:
- # Abstract: Show successful customer creation + opening savings account
- # --------------------------------------------
- print("Test 5")
- Dave = clsCustomer("Dave", "Malloy", "111223333")
- DaveSavingsAccount = clsSavingsAccount("111223333", 600.00, "savings", "DaveSavingsAccount", Dave)
- pprint.pprint(DaveSavingsAccount.__dict__)
- print("==========================================================================")
- # --------------------------------------------
- # Test 6:
- # Abstract: Show successful deposit into savings account
- # --------------------------------------------
- print("Test 6")
- print("The current balance of DaveSavingsAccount is", DaveSavingsAccount.dblBalance)
- DaveSavingsAccount.Deposit(300, DaveSavingsAccount)
- print("Dave deposited $300. DaveSavingsAccount balance is now", DaveSavingsAccount.dblBalance)
- print("==========================================================================")
- # --------------------------------------------
- # Test 7:
- # Abstract: Show failed withdrawal from savings due to dropping below $500 minimum
- # --------------------------------------------
- print("Test 7")
- intWithdrawalAmount = DaveSavingsAccount.dblBalance - 100
- print("The current balance of DaveSavingsAccount is", DaveSavingsAccount.dblBalance)
- print("Dave will attempt to withdraw", intWithdrawalAmount)
- DaveSavingsAccount.Withdraw(intWithdrawalAmount, DaveSavingsAccount)
- print("==========================================================================")
- # --------------------------------------------
- # Test 8:
- # Abstract: Show successful withdrawal from savings
- # --------------------------------------------
- print("Test 8")
- intWithdrawalAmount = 100
- print("The current balance of DaveSavingsAccount is", DaveSavingsAccount.dblBalance)
- print("Dave will attempt to withdraw", intWithdrawalAmount)
- DaveSavingsAccount.Withdraw(intWithdrawalAmount, DaveSavingsAccount)
- print("The balance of DaveSavingsAccount is now", DaveSavingsAccount.dblBalance)
- print("==========================================================================")
- # --------------------------------------------
- # Test 9:
- # Abstract: Show successful withdrawal from checking,
- # but overdraft fee applied due to balance going below zero
- # --------------------------------------------
- print("Test 9")
- Sarah = clsCustomer("Sarah", "Whitehall", "999887777")
- SarahCheckingAccount = clsAccount("999887777", 500, "checking", "SarahCheckingAccount", Sarah)
- print("Sarah has a checking account balance of $500. She will attempt to withdraw $600.")
- SarahCheckingAccount.Withdraw(600, SarahCheckingAccount)
- print("Sarah successfully withdrew the money. After the overdraft fee, her balance is now", SarahCheckingAccount.dblBalance)
- print("==========================================================================")
- # --------------------------------------------
- # Test 10:
- # Abstract: Show successful withdrawal from checking
- # --------------------------------------------
- print("Test 10")
- SarahCheckingAccount2 = clsAccount("999887777", 500, "checking", "SarahCheckingAccount2", Sarah)
- print("Sarah has a second checking account balance of $500. She will attempt to withdraw $400.")
- SarahCheckingAccount2.Withdraw(400, SarahCheckingAccount2)
- print("Sarah successfully withdrew the money. After the withdrawal, her balance is now", SarahCheckingAccount2.dblBalance)
- print("==========================================================================")
- # --------------------------------------------
- # Test 11:
- # Abstract: Show successful transfer, but overdraft fee applied
- # to checking FromAccount due to balance going below zero
- # --------------------------------------------
- print("Test 11")
- print("Sarah has two checking accounts. She will transfer $200 from the second account to the first.")
- print("The current balance of the first checking account is", SarahCheckingAccount.dblBalance)
- print("The current balance of the second checking account is", SarahCheckingAccount2.dblBalance)
- clsAccount.transferFunds(200, SarahCheckingAccount2, SarahCheckingAccount, "999887777")
- print("The current balance of the first checking account is now", SarahCheckingAccount.dblBalance)
- print("The current balance of the second checking account is now", SarahCheckingAccount2.dblBalance)
- print("==========================================================================")
- # --------------------------------------------
- # Test 12:
- # Abstract: Show failed transfer due to savings FromAccount going below $500 limit
- # --------------------------------------------
- print("Test 12")
- print("==========================================================================")
- # --------------------------------------------
- # Test 13:
- # Abstract: Show successful transfer
- # --------------------------------------------
- print("Test 13")
- print("==========================================================================")
- # --------------------------------------------
- # Test 14:
- # Abstract: Show successful customer creation
- # --------------------------------------------
- print("Test 14")
- print("==========================================================================")
- # --------------------------------------------
- # Test 15:
- # Abstract: Show successful customer creation
- # --------------------------------------------
- print("Test 15")
- print("==========================================================================")
Add Comment
Please, Sign In to add comment