Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os.path
- import re
- class Bank:
- def __init__(self, name, accountNum, balance):
- self.name = name
- self.accountNum = accountNum
- self.balance = balance
- # self.deposited = {} # wouldn't a list be better?, conflicts with method name
- self.deposits = [] # like this
- self.key = accountNum # why are you storing this twice?
- self.init_bla = 0 # what is this? How is it different to balance?
- @staticmethod
- def init_bal(accounts):
- matches = []
- target = self.accountNum
- with open("Account.txt") as fic:
- for idx, line in enumerate(fic, start=1):
- if re.search(target, line):
- matches.append(idx)
- account_num_str = str(self.accountNum)
- data = {account_num_str:
- {"A/c No": self.accountNum,
- "name": self.name,
- "bal": self.balance
- }
- }
- self.balance = int(data[account_num_str]["bal"])
- self.accountNum = int(data[account_num_str]["A/c No"])
- self.name = data[account_num_str]["name"]
- self.init_bla = data[account_num_str]["bal"]
- return self.balance
- def deposit(self):
- depo = float(input("Enter amount to Deposit: "))
- bal = int(self.init_bla) + depo # why not using balance?
- self.balance = bal
- self.deposited(depo)
- def deposited(self, amount):
- # self.deposited[self.key] = self.balance # makes no sense
- self.deposits.append(amount)
- # print(self.deposited)
- # self.saveInTxt()
- def saveInTxt(self):
- with open("Account.txt", "a+") as f:
- wif = str(self.accountNum) + "," + self.name + "," + str(self.balance) + "\n"
- wif = str(wif)
- f.write(wif)
- def extFromTxt(self):
- matches = []
- target = self.accountNum
- with open("Account.txt") as fic:
- for idx, line in enumerate(fic, start=1):
- if re.search(target, line):
- # print("Found at line {}".format(i))
- matches.append(i)
- g = str(self.accountNum)
- li = {g: {"A/c No": self.accountNum, "name": self.name, "bal": self.balance}}
- self.balance = int(li[g]["bal"])
- self.accountNum = int(li[g]["A/c No"])
- self.name = li[g]["name"]
- print(f"Name: {self.name}\nA/c No.: {self.accountNum}\nBalance: {self.init_bla}")
- if not matches:
- print(f"Name: {self.name}\nA/c No.: {self.accountNum}\nBalance: Max")
- def __str__(self):
- return f'{self.name}, {self.accountNum}: {self.balance}\n{self.deposits}'
- def get_new_account_number(accounts):
- if accounts:
- return max(accounts) + 1
- else:
- return 1001
- def find_account_by_number(accounts, account_number):
- return accounts.get(account_number, None)
- MENU = """
- 1-Create Account
- 2-Make Deposit
- 3-Withdrawl
- 4-Check A/c Details
- 5-Exit\n
- Enter your choice: """
- accounts = {}
- while True:
- inn = input(MENU)
- if inn == '1':
- account_number = get_new_account_number(accounts)
- name = input("What is your name? ")
- opening_balance = float(input('What is you opening deposit? '))
- accounts[account_number] = Bank(name, account_number, opening_balance)
- print(f'Welcome {name}, you account number is: {account_number}')
- elif inn == '2':
- account_number = int(input("[+] Enter your A/c No.:"))
- account = find_account_by_number(accounts, account_number)
- if account:
- account.deposit()
- else:
- print("Account not recognised")
- elif inn == '5':
- break
- elif inn == 'list': # hidden option for testing
- for account in accounts.values():
- print(account)
- else:
- print(f"Option {inn} not available")
- # exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement