Fundamentals of Programming I More Data Modeling Computer Science 111 Fundamentals of Programming I More Data Modeling
More Modeling: a Bank Manages a set of accounts Operations Add a new account Remove an account Access an account for deposits and withdrawals Compute the interest on all accounts
Class Diagram Describes the relationship between two classes Bank * SavingsAccount Describes the relationship between two classes A Bank object contains zero or more SavingsAccount objects
The Interface of the Bank Class Bank() # Returns a new bank add(account) # Adds account to bank remove(name, pin) # Removes an account from bank get(name, pin) # Returns an account or None computeInterest() # Computes the interest and # deposits it in each account str(aBank) # Returns string rep of bank
Example Use of the Bank Class from bank import Bank wellsFargo = Bank() for pinNumber in range(1000, 1005): wellsFargo.add(SavingsAccount('Ken', str(pinNumber))) print(wellsFargo) account = wellsFargo.get('Ken', '1001') account.deposit(100) account.withdraw(20) print(wellsFargo.computeInterest())
Defining a Bank Class Set up a data structure for the data Define methods to Add a new account Remove an account Access an account for deposits and withdrawals Compute the interest on all accounts
Defining the Bank Class class Bank(object): """This class represents a bank.""" def __init__(self): self.accounts = {} # Other methods go here Use a dictionary for the accounts, keyed by the name + pin
Defining the Bank Class class Bank(object): """This class represents a bank.""" def __init__(self): self.accounts = {} def __str__(self): result = "" for account in self.accounts.values(): result += str(account) + "\n" return result Always define __init__ and __str__ first
Defining the Bank Class class Bank(object): """This class represents a bank.""" def __init__(self): self.accounts = {} def __str__(self): return "\n".join(map(str, self.accounts.values())) Simplify, simplify!
Adding an Account class Bank(object): """This class represents a bank.""" def __init__(self): self.accounts = {} def add(self, account): key = account.name + account.pin self.accounts[key] = account An account is a value in the dictionary, keyed by its name and pin The remaining methods are similar
Class Variables An instance variable refers to storage owned by a single instance A class variable refers to storage owned by the class, and thus available to all of its instances For example, each savings account owns a separate balance, so that should be an instance variable But all savings accounts have the same interest rate, so that should be a class variable
Class and Instance Variables from account import SavingsAccount a1 = SavingsAccount('Ken', '3322', 1000.00) a2 = SavingsAccount('Catherine', '3321', 10000.00) print(SavingsAccount.RATE) SavingsAccount SavingsAccount.RATE a1 a2 self.balance self.balance
Using Class Variables account = SavingsAccount('Ken', '3322', 1000.00) print(account.computeInterest()) SavingsAccount.RATE = 0.06
Defining a Class Variable class SavingsAccount(object): """This class represents a savings account.""" RATE = 0.06 # Class variable def __init__(self, name, pin, balance = 0.0): self.name = name self.pin = pin self.balance = balance def computeInterest(self): interest = SavingsAccount.RATE * self.balance self.deposit(interest) return interest Class variables are picked out using the class name as a prefix Their names are usually in uppercase
Design patterns with classes For Friday Design patterns with classes