#!/usr/bin/env/ python __author__ = 'Monica Rosenberg' __email__ = 'monica@ischool.berkeley.edu' __python_version = '3.2' __can_anonymously_use_as_example = True '''OOP demo of a Bank Account Program''' class BankAccount(object): '''represents a generic banking account''' def __init__(self, accountName, accountNumber, accountBalance): self.name = accountName self.accountNumber = accountNumber self.balance = accountBalance def deposit(self, amount): self.balance = self.balance + amount #this is the solution I posted in class, see below for a more modular (better!) solution '''def interest(self, numberMonths, regularDeposit = 0): i=1 while i <= numberMonths: if self.balance <= 500: self.balance = self.balance*1.05 + regularDeposit else: self.balance = self.balance*1.1 + regularDeposit print ("Balance for month", i, "is $"+ str(self.balance)) i+=1''' #more modular solution def interest(self): #this just calculates the interest if self.balance <= 500: self.balance = self.balance*1.05 else: self.balance = self.balance*1.1 def savings(self, numberMonths, regularDeposit = 0): #setting regularDeposit to 0 in the definition statement means that if no parameter is passed when calling the method, then the regularDeposit will be assumed to be 0. i=1 #use this as your counter while i <= numberMonths: self.interest() self.deposit(regularDeposit) print ("Balance for month", i, "is $"+ str(self.balance)) i+=1 ##main personalAccount = BankAccount('Monica Rosenberg', '12345', 0.0) print(personalAccount.name, personalAccount.balance) personalAccount.deposit(50.0) print(personalAccount.name, personalAccount.balance) personalAccount.savings(6, 200.0) '''Output should look like this. Let Alex or me know if you have questions Monica Rosenberg 0.0 Monica Rosenberg 50.0 Balance for month 1 is $252.5 Balance for month 2 is $465.125 Balance for month 3 is $688.38125 Balance for month 4 is $957.2193750000001 Balance for month 5 is $1252.9413125000003 Balance for month 6 is $1578.2354437500005'''