'Add a pickle to a program with loop
I'm trying to run a program of a bank account (it's not the whole program). I want that the data of the users and the transactions will be saved for the next time the user ton the program. how can I do the pickling here on the two top dictionaries?
import pickle
from datetime import datetime
import collections
all_bank_accounts = {}
all_bank_accounts_actions = {}
class BankAccount():
def __init__(self, first_name, last_name, personal_number,currency, amount = 0):
self.first_name = first_name
self.last_name = last_name
self.personal_number = personal_number
self.currency = currency
self.amount = amount
self.counter_of_transactions = 0
def balance_check_in_the_bank_account(self):
print('The amount of money in the bank is:\n', self.amount)
def cash_deposit(self, amount_of_deposit):
self.amount_of_deposit = amount_of_deposit
self.amount += amount_of_deposit
self.now = datetime.now()
self.dt_string = self.now.strftime("%d/%m/%Y, %H:%M:%S")
self.date_and_amount = [self.dt_string, self.amount_of_deposit]
cd = f"You have just deposited an amount of {self.amount_of_deposit}{self.currency}. \nyou currently have {self.amount}{self.currency} in your account."
print(cd)
self.counter_of_transactions += 1
return self.date_and_amount
def file_of_transactions(self, the_list_of_transactions2):
self.the_list_of_transactions = the_list_of_transactions2
self.the_string_data =f"{self.first_name} {self.last_name} {self.personal_number}, balance: {self.amount}{self.currency}\n"
with open(f"{self.personal_number}.txt", "w") as the_file:
the_file.write(self.the_string_data)
for a,b in zip(self.the_list_of_transactions[::2], self.the_list_of_transactions[1::2]):
the_string_transaction = f"{a} {b}{self.currency}\n"
print(the_string_transaction)
the_file.write(the_string_transaction)
while True:
cmd = int(input(" Press 1 to create a bank account or Press 2 to other options:\n"))
if cmd == 1:
first_namee = input("What is your first name? ")
last_namee = input("What is your last name? ")
personal_numberr = input("What is your ID? ")
currencyy = input("What type of currncey would you like to use? ")
amountt = int(input("What is the amount of money would you like to deposit? "))
the_account = BankAccount(first_namee, last_namee, personal_numberr, currencyy, amountt)
all_bank_accounts[personal_numberr] = the_account
print(all_bank_accounts)
else:
personal_numberr = input("What is your ID? ")
if personal_numberr in all_bank_accounts.keys():
cmd = int(input(" Press 2 to check in the bank account.\n Press 3 to deposit cash.\n Press 7 to print an account details page .\n"))
if cmd == 2:
all_bank_accounts[personal_numberr].balance_check_in_the_bank_account()
elif cmd == 3:
input_deposit = int(input('How much would you like to deposit?\n'))
return_deposit = all_bank_accounts[personal_numberr].cash_deposit(input_deposit)
if personal_numberr in all_bank_accounts_actions:
all_bank_accounts_actions[personal_numberr].extend(return_deposit)
else:
all_bank_accounts_actions[personal_numberr] = return_deposit
elif cmd == 7:
all_bank_accounts[personal_numberr].file_of_transactions(all_bank_accounts_actions[personal_numberr][:])
else:
print("The bank account not exist")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
