Download presentation
Presentation is loading. Please wait.
Published byDuane Gallagher Modified over 6 years ago
1
Fundamentals of Programming I More Data Modeling
Computer Science 111 Fundamentals of Programming I More Data Modeling
2
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
3
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
4
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
5
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())
6
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
7
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
8
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
9
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!
10
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
11
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
12
Class and Instance Variables
from account import SavingsAccount a1 = SavingsAccount('Ken', '3322', ) a2 = SavingsAccount('Catherine', '3321', ) print(SavingsAccount.RATE) SavingsAccount SavingsAccount.RATE a1 a2 self.balance self.balance
13
Using Class Variables account = SavingsAccount('Ken', '3322', 1000.00)
print(account.computeInterest()) SavingsAccount.RATE = 0.06
14
Defining a Class Variable
class SavingsAccount(object): """This class represents a savings account.""" RATE = # 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
15
Design patterns with classes
For Friday Design patterns with classes
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.