def enter_card(): while True: entry = input("Enter your card (number): ") if len(entry) == 8 and entry.isnumeric(): if ldb.sql_query_fetchall( my_local_db, f"SELECT number FROM cards WHERE number = {entry}"): return entry else: print("Sorry your card is not compatible with this ATM")
def balance(self): total = 0.0 transactions = ldb.sql_query_fetchall( my_local_db, f"SELECT amount, type FROM transactions WHERE account_id = {self.number}" ) for transaction in transactions: if transaction[1] == 'credit': total += float(transaction[0]) else: total -= float(transaction[0]) return total
def enter_pin(atm_card_number): tries = 0 while tries < 3: pin_entry = input("Enter your card pin: ") if len(pin_entry) == 4 and pin_entry.isnumeric(): if pin_entry == ldb.sql_query_fetchall( my_local_db, f"SELECT pin FROM cards WHERE number = {atm_card_number}" )[0][0]: return True tries += 1 print("Sorry, you have exceeded your maximum attempts. ") return False
def retrieve_customer_data(atm_card_number): card_holder = ldb.sql_query_fetchall( my_local_db, f"SELECT holder_firstname, holder_lastname, pin FROM cards WHERE number = {atm_card_number}" )[0] customer_record = ldb.sql_query_fetchall( my_local_db, f"SELECT * FROM customers WHERE firstname = '{card_holder[0]}' AND lastname = '{card_holder[1]}'" )[0] current_account_records = ldb.sql_query_fetchall( my_local_db, f"SELECT number, currency, overdraft FROM current_accounts WHERE holder_firstname = '{customer_record[0]}' AND holder_lastname = '{customer_record[1]}'" ) savings_account_records = ldb.sql_query_fetchall( my_local_db, f"SELECT number, currency, overdraft FROM savings_accounts WHERE holder_firstname = '{customer_record[0]}' AND holder_lastname = '{customer_record[1]}'" ) business_account_records = ldb.sql_query_fetchall( my_local_db, f"SELECT number, currency, overdraft FROM business_accounts WHERE holder_firstname = '{customer_record[0]}' AND holder_lastname = '{customer_record[1]}'" ) customer = acc.Customer(customer_record[0], customer_record[1], customer_record[2]) customer.add_card(acc.Card(atm_card_number, card_holder[2])) for record in current_account_records: customer.add_account( acc.CurrentAccount(record[0], record[1], record[2]), "Current Account") for record in savings_account_records: customer.add_account( acc.SavingsAccount(record[0], record[1], record[2]), 'Savings Account') for record in business_account_records: customer.add_account( acc.BusinessAccount(record[0], record[1], record[2]), 'Business Account') del card_holder, customer_record, current_account_records, savings_account_records, business_account_records return customer, customer.cards[0]
total += selected_account.balance() print(f"Your balance is {total}") @staticmethod def print_transactions(selected_account, trans_num): transaction_list = selected_account.return_transaction(trans_num) for transaction in transaction_list: print( f"{transaction[1]} {transaction[2]} {transaction[3].upper()} on {transaction[5].strftime('%d-%b-%Y')}" ) if __name__ == "__main__": # Connect to local MySql server and pull customer list to display in console my_local_db = ldb.initialise_local_db_connection('bank') for card in ldb.sql_query_fetchall(my_local_db, "SELECT * FROM cards"): print( f"Customer {card[4]} {card[5]} has a card with number {card[0]} and pin {card[1]}" ) # Create new ATM object atm = Atm("Ealing Broadway") # Run ATM program -> user must enter card number as proxy for swiping card network_card = atm.enter_card() verified = atm.enter_pin(network_card) while True: if not verified: break else: verified_card_holder, verified_card = atm.retrieve_customer_data(
def return_transaction(self, max_num): transactions = ldb.sql_query_fetchall( my_local_db, f"SELECT * FROM transactions WHERE account_id = {self.number}") return sorted(transactions, key=lambda item: item[5], reverse=True)[:max_num]