python课的小程序~ subclass子类

Question-1:
题目描述:创建一个银行帐户的类模型,表示银行的所有客户。所有的客户都能在他们的银行帐户中存钱、取钱。任务主要如下:
创建 Account类,作为基类。Account类应该包含帐户余额,并提供构造函数;向账户中余额加钱的方法;减少账户中余额的方法;获取当前账户余额的方法。
创建 Account类的派生类:储蓄账户SavingAccount类,除去基类Account的所有功能外,储蓄账户还应该具有新的特性。储蓄账户应该提供一个计算利息的成员函数和将利息加到账户余额上的操作。
创建 Account类的派生类:支票账户CheckingAccount类,与储蓄账户不同的是,它没有利率的计算,但是他会在每笔交易后收取一定的交易费用。它需要重新定义基类Account类中的成员函数,当每笔交易完成时,在账户余额中减去交易金额。
提示:在提供上述方法时应注意出错情况的处理,比如账户余额不足的情况。

需补充代码——template.py

类的继承和多态机制

参考结果:
lab4example

Answer-1:
bank_account.py

# 2014.1.12 by Alex YU

class Account:

	def __init__(self, name):
		self.name = name
		self.balance = 0
	def deposit(self, count):
		self.balance += count
	def withdraw(self, count):
		if self.balance < count:
			return False
		else:
			self.balance -=count
			return True
	def get_balance(self):
		return self.balance

# Here are subclasses
class SavingAccount(Account):
	
	def __init__(self, name, interest_rate):
		Account.__init__(self,name)
		self.interest_rate = interest_rate
	def get_rate_of_interest(self):
		return self.interest_rate
	def get_interest(self):
		return self.interest_rate*self.balance
	def add_interest(self):
		self.balance *= (1+self.interest_rate)

class CheckingAccount(Account):

	def __init__(self, name, poundage):
		Account.__init__(self,name)
		self.poundage = poundage
	def deposit(self, count):
		print 'Want to save %d'%count
		self.balance += count*(1-self.poundage)
	def withdraw(self, count):
		maxwithdraw = self.balance/(1+self.poundage)
		if count > maxwithdraw:
			return False
		else:
			self.balance -= count*(1+self.poundage)
			return True


###########################################################
#														  #
# Do not edit any code from here to the end of this file. #
#														  #
###########################################################

def test_withdraw(acnt):
	''' Test the withdraw method in class'''
	withdraw_list = [105, 101, 100, 99, 98]

	for i in withdraw_list:
		rst = acnt.withdraw(i)
		if rst:
			print 'Withdraw %d Success.\t' % i
		else:
			print 'Withdraw %d fail.\t' % i

	print '\n'
	return

def test_Account(money):
	''' Test the Account class'''
	print ' **********  Test Account ********* '

	acnt = Account('leaf')

	print 'Before deposit, balance: %.02lf' % acnt.get_balance()
	acnt.deposit(money)
	print 'After deposit, balance: %.02lf' % acnt.get_balance()

	test_withdraw(acnt)

	return 

def test_SavingAccount(money, rate_of_interest):
	''' Test SavingAccount class'''
	print ' **********  Test SavingAccount ********* '

	saving_acnt = SavingAccount('lotus', rate_of_interest)

	print 'Before deposit, balance: %.02lf' % saving_acnt.get_balance()
	saving_acnt.deposit(money)
	print 'After deposit, balance: %.02lf' % saving_acnt.get_balance()

	print 'The rate of interest is %.02lf' % saving_acnt.get_rate_of_interest()
	print 'The interest is %.02lf' % saving_acnt.get_interest()

	draw_money = money * (rate_of_interest + 1)
	draw_rst = saving_acnt.withdraw(draw_money)
	print 'Withdraw %d' % draw_money,
	if draw_rst:
		print 'Success.'
	else:
		print 'Fail.'

	saving_acnt.add_interest()

	print 'After adding interest, the balance is %.02lf' % \
	saving_acnt.get_balance()

	test_withdraw(saving_acnt)

	return 

def test_CheckingAccount(money, rate_of_poundage):
	''' Test CheckingAccount class'''
	print ' **********  Test CheckingAccount ********* '

	checking_acnt = CheckingAccount('pepper', rate_of_poundage)

	print 'Before deposit, balance: %.02lf' % checking_acnt.get_balance()
	checking_acnt.deposit(money)
	print 'After deposit, balance: %.02lf' % checking_acnt.get_balance()
		
	test_withdraw(checking_acnt)

	return 

def test():
	money = 100
	rate_of_interest = 0.05
	rate_of_poundage = 0.01

	test_Account(money)
	test_SavingAccount(money, rate_of_interest)
	test_CheckingAccount(money, rate_of_poundage)

	return 

if __name__ == "__main__":
	test()

发表评论

电子邮件地址不会被公开。 必填项已用*标注