Advertisement
gruntfutuk

fix will be deleted

Jul 22nd, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.15 KB | None | 0 0
  1. import os.path
  2. import re
  3.  
  4.  
  5. class Bank:
  6.    
  7.     def __init__(self, name, accountNum, balance):
  8.         self.name = name
  9.         self.accountNum = accountNum
  10.         self.balance = balance
  11.         # self.deposited = {}  # wouldn't a list be better?, conflicts with method name
  12.         self.deposits = []   # like this
  13.         self.key = accountNum  # why are you storing this twice?
  14.         self.init_bla = 0  # what is this? How is it different to balance?
  15.    
  16.     @staticmethod
  17.     def init_bal(accounts):
  18.         matches = []
  19.         target = self.accountNum
  20.         with open("Account.txt") as fic:
  21.             for idx, line in enumerate(fic, start=1):
  22.                 if re.search(target, line):
  23.                     matches.append(idx)
  24.                     account_num_str = str(self.accountNum)
  25.                     data = {account_num_str:
  26.                             {"A/c No": self.accountNum,
  27.                              "name": self.name,
  28.                              "bal": self.balance
  29.                             }
  30.                            }
  31.                     self.balance = int(data[account_num_str]["bal"])
  32.                     self.accountNum = int(data[account_num_str]["A/c No"])
  33.                     self.name = data[account_num_str]["name"]
  34.                     self.init_bla = data[account_num_str]["bal"]
  35.         return self.balance
  36.  
  37.     def deposit(self):
  38.         depo = float(input("Enter amount to Deposit: "))
  39.         bal = int(self.init_bla) + depo  # why not using balance?
  40.         self.balance = bal
  41.         self.deposited(depo)
  42.        
  43.     def deposited(self, amount):
  44.         # self.deposited[self.key] = self.balance  # makes no sense
  45.         self.deposits.append(amount)
  46.         # print(self.deposited)
  47.         # self.saveInTxt()
  48.  
  49.     def saveInTxt(self):
  50.         with open("Account.txt", "a+") as f:
  51.             wif = str(self.accountNum) + "," + self.name + "," + str(self.balance) + "\n"
  52.             wif = str(wif)
  53.             f.write(wif)
  54.  
  55.     def extFromTxt(self):
  56.         matches = []
  57.         target = self.accountNum
  58.         with open("Account.txt") as fic:
  59.             for idx, line in enumerate(fic, start=1):
  60.                 if re.search(target, line):
  61.                     # print("Found at line {}".format(i))
  62.                     matches.append(i)
  63.                     g = str(self.accountNum)
  64.                     li = {g: {"A/c No": self.accountNum, "name": self.name, "bal": self.balance}}
  65.                     self.balance = int(li[g]["bal"])
  66.                     self.accountNum = int(li[g]["A/c No"])
  67.                     self.name = li[g]["name"]
  68.                     print(f"Name: {self.name}\nA/c No.: {self.accountNum}\nBalance: {self.init_bla}")
  69.  
  70.         if not matches:
  71.             print(f"Name: {self.name}\nA/c No.: {self.accountNum}\nBalance: Max")
  72.            
  73.     def __str__(self):
  74.         return f'{self.name}, {self.accountNum}: {self.balance}\n{self.deposits}'
  75.  
  76. def get_new_account_number(accounts):
  77.     if accounts:
  78.         return max(accounts) + 1
  79.     else:
  80.         return 1001
  81.  
  82. def find_account_by_number(accounts, account_number):
  83.     return accounts.get(account_number, None)
  84.  
  85.  
  86. MENU = """
  87. 1-Create Account
  88. 2-Make Deposit
  89. 3-Withdrawl
  90. 4-Check A/c Details
  91. 5-Exit\n
  92. Enter your choice: """
  93.  
  94. accounts = {}
  95. while True:
  96.     inn = input(MENU)    
  97.     if inn == '1':
  98.         account_number = get_new_account_number(accounts)
  99.         name = input("What is your name? ")
  100.         opening_balance = float(input('What is you opening deposit? '))
  101.         accounts[account_number] = Bank(name, account_number, opening_balance)
  102.         print(f'Welcome {name}, you account number is: {account_number}')
  103.     elif inn == '2':
  104.         account_number = int(input("[+] Enter your A/c No.:"))
  105.         account = find_account_by_number(accounts, account_number)
  106.         if account:
  107.             account.deposit()
  108.         else:
  109.             print("Account not recognised")
  110.     elif inn == '5':
  111.         break
  112.     elif inn == 'list':  # hidden option for testing
  113.         for account in accounts.values():
  114.             print(account)
  115.     else:
  116.         print(f"Option {inn} not available")
  117.  
  118. # exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement