Hi again all,
I have been tussling with this program all day. I can't seem to get pickle.dumps (or f.write..tried that earlier) to save without saving over itself. If I do a deposit(option 1) and then a withdraw(option 2), and then history(option 3), it only shows the last option ran before history. I am trying to get it to save all transactions. I would also like it to pick up the account balance where it last was saved to file upon opening. I have no idea how to do that. I am running Python 2.7 on Windows 8. Save file is set to f=open('pettycash.txt', 'w'). See deposit lines 29-32 and withdraw lines 45-48: I use str() on the items to make them writeable to a list then make the lists then pickle.dump them to f. See history line 102 for the pickle.load.
Thank you.
from datetime import date
try:
import cPickle as pickle
except:
import pickle
import pprint
now = date.today
class Account:
def __init__(self, initial):
'''Retrieves account data.'''
self.balance = initial
print "User %s requested balance on %s: balance is $%s " % (user, now(),self.balance)
print
def deposit(self, amt):
'''Deposit amount'''
self.balance = self.balance + amt # deposit adds to the balance
print "User %s deposited %s amount on %s: balance is $%s " % (user, amt, now(),self.balance)
print
# convert to strings
user1=str(user)
amt1=str(amt)
now1=str(now())
selfbalance1=str(self.balance)
#create list
log=['Deposit','User :'+user1,' Amount :' +amt1,' Date :'+now1,' Balance :'+selfbalance1]
f =open('pettycash.txt', 'wb')
#dump list
pickle.dump(log, f)
def withdraw(self,amt):
'''Withraw amount'''
self.balance = self.balance - amt # withdrawl subtracts from the balance
print "User %s withdrew %s amount on %s: balance is $%s " % (user, amt, now(),self.balance)
print
# convert to strings
user2=str(user)
amt2=str(amt)
now2=str(now())
selfbalance2=str(self.balance)
#create list
log1=['Withdraw',' User :'+user2, ' Amount :'+amt2, ' Date :'+now2, ' Balance :'+selfbalance2]
f =open('pettycash.txt', 'wb')
#dump list
pickle.dump(log1, f)
def getbalance(self):
'''Return account balance'''
return self.balance
print
def printmenu():
print "Please select an option from menu:"
print
print "1 for Deposit"
print "2 for Withdraw"
print "3 for History"
print "4 to Exit"
print
return input (">")
def showbalance(now,self):
print "Balance as of %s: is $%s " % (now(),self.balance)
print
def getnumberinput(txt):
num = 0
while num == 0:
val = input(txt)
num = float(val)
if num <= 0:
print "Please try again, your input has to be more than zero."
num = 0
return num
print
def pettycashdb(a):
print "Welcome to the Petty Cash Tracker!"
print
choice = 0
choice1 = 0
log={}
while choice <> 4:
choice = printmenu()
if choice == 1:
#deposit
deposit = getnumberinput("Deposit amount is:")
a.deposit(deposit)
elif choice == 2:
# withdraw
withdraw = getnumberinput("Withdraw amount is:")
a.withdraw(withdraw)
elif choice == 3:
#history
f=open('pettycash.txt', 'r')
data=pickle.load(f)
pprint.pprint(data)
elif choice == 4:
print
print 'Thank you!'
showbalance(now,a)
f.close()
print 'done'
print'''
|===============================|
|Testing the petty cash database|
|===============================|
'''
user='default'
a=Account(0.00)
a.deposit(550.23)
a.deposit(100)
a.withdraw(650.23)
print'''
|===============|
| Test complete |
|===============|
'''
if __name__ == '__main__':
print "Please input your name for tracking: (example:'David Green')"
print
user= raw_input ("Please enter name:")
print
while user==0:
user='default user'
print 'Welcome, you are logged in as:', user
print
print "If you'd like to login as someone else, you need to relog"
print
relog=raw_input ("Would you like to close the application and relog? ")
if relog=='y':
print 'Exiting, please reopen application...'
quit()
else:
print
print "Balance as of %s: $%s " % (now(),a.getbalance())
print
foo = pettycashdb(a)
else:
print "Module petty cash tracker imported."