由于某些原因,我无法概念化如何使用支票簿.class对象而不是“ledger[list]”试图保持我的方法OOP。实现本准则目标的最佳实践方法是什么?(是的,我也有其他的“新手”问题,但我很高兴能得到一些意见。)
import sys
import time
transNum=0
ledger = []
debitInput=['Check Number: ', 'Transaction Date: ',
'Payee: ', 'Amount: ', 'Memo (or Enter to skip): ']
creditInput=['Check Number: ', 'Transaction Date: ',
'Source: ', 'Amount: ', 'Memo (or Enter to skip): ']
# a Payee is a persistent record/lookup of current, past and new payees
class Payee:
pass
# a Source is a persistent record/lookup of current, past and new sources
class Source:
pass
# a Checkbook is a collection of Transaction objects upon which queries may be performed
class Checkbook:
def __init__(self, newTrans):
pass
# a Transaction is a collection of Debit and Credit objects
class Transaction:
def __init__(self, chkNum, transDate, thirdParty, amount, memo=''):
self.chkNum = chkNum
self.transDate = transDate
self.memo=memo
self.thirdParty=thirdParty
self.amount=amount
self.transNum = transNum
class Debit(Transaction):
def __init__(self, *args):
Transaction.__init__(self, *args)
self.payee=self.thirdParty
del self.thirdParty
self.amount=int(self.amount)*-1
class Credit(Transaction):
def __init__(self, *args):
Transaction.__init__(self, *args)
self.source=self.thirdParty
del self.thirdParty
self.amount=int(self.amount)
while True:
transact = []
transNum += 1
choice=input('Posting debit [d], credit [c] or [x] to exit: ')
if choice == 'x': break
elif choice == 'd':
for field in debitInput:
field = input(field)
transact.append(field)
trans = Debit(transact[0], transact[1], transact[2], transact[3], transact[4])
ledger.append(trans)
elif choice == 'c':
for field in creditInput:
field = input(field)
transact.append(field)
trans = Credit(transact[0], transact[1], transact[2], transact[3], transact[4])
或者你可以subclass ^{} 。你知道吗
这是做作业用的吗?在Python中指定这个值是愚蠢的。You'd never really want to subclass ^{} 。我在大学里做了一个更好的练习,我必须使用模板、动态数组(带有
malloc()
)和操作符重载来重新实现一个std::vector
。。。多有趣啊!std::map
练习更好:)相关问题 更多 >
编程相关推荐