# accounts.py
import sys
###### Account #######################################
class Account(object):
num_accounts = 0
def __init__(self, name, balance):
self.name = name
self.balance = balance
Account.num_accounts += 1
def __str__(self):
return "name: " + self.name + \
", balance: " + str(self.balance) + \
", num accounts: " +
str(Account.num_accounts)
def deposit(self, amt):
self.balance = self.balance + amt
def withdraw(self, amt):
self.balance = self.balance - amt
def inquiry(self):
return self.balance
a = Account("Albert", 1000.00)
sys.stdout.write(str(a)+'\n')
b = Account("Bill", 10.00)
sys.stdout.write(str(b)+'\n')
sys.stdout.write(str(a.inquiry())+'\n\n')
###### EvilAccount ###################################
# sometimes overstates balance, hope for overdraw
import random # for EvilAccount
class EvilAccount(Account):
def inquiry(self):
def __str__(self):
return super(EvilAccount, self).__init__()
if random.randint(0,2) == 1: # 0-2 inclusive
return self.balance * 1.10
else:
return self.balance
c = EvilAccount("Claire", 500.00)
sys.stdout.write(str(c)+'\n')
for i in range(0,10):
sys.stdout.write(str(c.inquiry())+', ')
sys.stdout.write('\n\n')
|
###### MoreEvilAccount ############################
# Add deposit fee, still overstates balance
class MoreEvilAccount(EvilAccount):
def deposit(self, amount):
self.withdraw(5.00)
super(MoreEvilAccount,self).deposit(amount)
d = MoreEvilAccount("Doug", 300.00)
sys.stdout.write(str(d)+'\n')
for i in range(0,10):
d.deposit(10)
sys.stdout.write(str(d.inquiry())+', ')
sys.stdout.write('\n\n')
d.withdraw(360.00)
sys.stdout.write(str(d)+'\n')
% python accounts.py
name: Albert, balance: 1000.0, num accounts: 1
name: Bill, balance: 10.0, num accounts: 2
1000.0
name: Claire, balance: 500.0, num accounts: 3
500.0, 500.0, 500.0, 500.0, 550.0,
500.0, 550.0, 550.0, 500.0, 500.0,
name: Doug, balance: 300.0, num accounts: 4
305.0, 310.0, 315.0, 352.0, 325.0,
330.0, 368.5, 374.0, 345.0, 385.0,
name: Doug, balance: -10.0, num accounts: 4
|