# Justin Cummins # ECS10 - Spring 2009 # banking.py - Simulated banking program with can only create/deposit. No security. #Needed for randomize new account numbers import random #Helper function to make pretty headings def heading(title): width = 50 result = "="*width + "\n" result += title.center(width) result += "\n" + "="*width return result #Creates a new account with name and initial zero balance. #Input: accounts - dictionary mapping account numbers to [name,balance] lists def create_account(accounts): print heading("Creating New Account") #Create random new account number num = random.randint(1, 100000) while num in accounts: #Make sure it's not already in use num = random.randint(1,100000) name = raw_input("Please enter name: ") balance = 0.0 accounts[num] = [name, balance] #Prints a table of all accounts information #Input: accounts - dictionary mapping account numbers to [name,balance] lists def print_accounts(accounts): print heading("Printing All Accounts") if len(accounts) < 1: print "No Accounts!!!" return print "Number | %-20s | %-12s" % ("Name", "Balance") print "-"*44 #Key is account number, value is a list [name, balance] for (key, value) in accounts.items(): name = value[0] balance = value[1] print "%6d | %-20s | $%.2f" % (key,name, balance) print #Empty line #Deposits money into an account #Asks for valid account number, matching name, and amount to add #Input: accounts - dictionary mapping account numbers to [name,balance] lists def deposit(accounts): print heading("Depositing Money") #Ask for account number acc_num = raw_input("Please enter your account number: ") try: acc_num = int(acc_num) except ValueError: print "Please enter a valid account number." return #Check if account number exists if acc_num not in accounts: print "Invalid Account Number." return #Retrieve account information in form [name,balance] account_details = accounts[acc_num] name = account_details[0] #Validate name on account user_name = raw_input("Please enter your name: ") if user_name.strip() != name: print "The name on the account does not match!" return #Ask for amount to deposit amount = raw_input("How much would you like to deposit? ") try: amount = float(amount) except ValueError: print "Please enter a valid amount next time." return #Deposit into account balance account_details[1] += amount #Main function #Handles interactive menu and calls other functions for processing def main(): menu = heading("Bank On Us Bank - Main Menu") menu += """ 1) Create account 2) Print accounts 3) Deposit money Q) Quit the program """ #Initial values option = "" accounts = {} #Continue until told to quit while option.upper() != "Q": print menu option = raw_input("Please select an option: ") print #Blank line #Create account option if option == "1": create_account(accounts) #Print accounts option elif option == "2": print_accounts(accounts) #Deposit option elif option == "3": deposit(accounts) elif option == "999": print "This is a secret option. Go Away!" #Print message when not above options and not quitting elif option.upper() != "Q": print "Invalid option!" print #Blank line print "Thank you!" #Start with the main function main()