tmcopeland

Simple Python Bank

Feb 28th, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.77 KB | None | 0 0
  1. # This is a comment.
  2.  
  3. # Note: underscored variables are private. Well, they aren't really, but people
  4. # will tend to not mess with them if they're smart.
  5.  
  6. # `class' denotes a class. Simple enough.
  7. # The colon begins the structure (this is not only for classes, but also with
  8. # functions, if/elif/else, for, while).
  9. # A code block indented by a uniform number of spaces or tabs is one
  10. # structure/construct. Every structure/construct in the program must match the
  11. # size and type (space/tab) of indentation. As soon as a line of at least one
  12. # less indentation is reached, the structure/construct is ended.
  13. class Account:
  14.     # `__init__' is a built-in function (as are all underscored functions).
  15.     # It is the constructor. `self' is like `this', but a little different.
  16.     # If you need to access member variables from a function, you need `self'.
  17.     # It's usually put as the first argument but it doesn't really matter.
  18.     def __init__(self, name, account_number, ssn, starting_balance = 0):
  19.         self._name = name
  20.         self._account_number = account_number
  21.         self._ssn = ssn
  22.         self._balance = starting_balance
  23.     def deposit(self, amount):
  24.         self._balance += amount
  25.     def withdraw(self, amount):
  26.         self._balance -= amount
  27.     def get_account_number(self):
  28.         return self._account_number
  29.     def get_balance(self):
  30.         return self._balance
  31.     def set_balance(self, amount):
  32.         self._balance = amount
  33.     def get_name(self):
  34.         return self._name
  35.     def set_name(self, name):
  36.         self._name = name
  37.     def get_ssn(self):
  38.         return self._ssn
  39.     # `__str__' is also a built-in function in Python. It's like `toString' in Java.
  40.     def __str__(self):
  41.         description = self._name + "\n    "
  42.         description += "Account Number: " + str(self._account_number) + "\n    "
  43.         description += "Social Security Number: " + str(self._ssn) + "\n    "
  44.         description += "Balance: " + str(self._balance)
  45.         return description
  46. class AccountCollection:
  47.     def __init__(self):
  48.         self._collection = [30]
  49.         for i in range(0, 30):
  50.             #self._collection[i] = Account
  51.             self._collection.append(Account)
  52.         self._count = 0
  53.     def add_account(self, name, account_number, ssn, balance):
  54.         if count is len(self._collection):
  55.             increase_size()
  56.         self._collection[count] = Account(name, account_number, ssn, balance)
  57.         self._count += 1
  58.     def deposit(self, name, amount):
  59.         index = self.search(name)
  60.         if index >= 0:
  61.             self._collection[index].deposit(amount)
  62.         else:
  63.             raise Exception("Name '" + name + "' not found.")
  64.     def withdraw(self, name, amount):
  65.         index = self.search(name)
  66.         if index >= 0:
  67.             self._collection[index].withdraw(amount)
  68.         else:
  69.             raise Exception("Name '" + name + "' not found.")
  70.     def get_length(self):
  71.         return len(self._collection)
  72.     def increase_size(self):
  73.         temp = []
  74.         for i in range(0, (len(self._collection ) * 2)):
  75.             temp[i] = Account
  76.         for account in range(0, len(self._collection)):
  77.             temp[account] = self._collection[account]
  78.         self._collection = temp
  79.     def search(self, name):
  80.         match = -1
  81.         for i in range(0, len(self._collection)):
  82.             if self._collection[i].get_name() == name:
  83.                 match = i
  84.                 break
  85.             else:
  86.                 match = -1
  87.         return match
  88.     def show_account(self, index):
  89.         return self._collection[index]
  90.     # Three single quotes is a multi-line comment for some reason.
  91.     '''
  92.    def sort(self):
  93.        highest = 0.0
  94.        sorted = []
  95.        for i in range(0, len(self._collection)):
  96.            for j in range(0, len(self._collection)):
  97.                balance = self._collection[j].get_balance()
  98.                if balance > highest:
  99.                    highest = balance
  100.                else:
  101.                    highest = highest
  102.        return sorted
  103.    '''
  104.     def __str__ (self):
  105.         report = ""
  106.         report += "Account List\n\n"
  107.         report += "    Number of Accounts: " + str(self._count) + "\n"
  108.         for i in range(0, self._count):
  109.             report += str(self._collection[i]) + "\n"
  110.         return report
  111. if __name__ == "__main__":
  112.     import sys
  113.     list = AccountCollection()
  114.     print """\
  115. Welcome to  WeWillNotStealYourMoney bank, brought to you by Gamefly - the
  116. service that allows you to rent used games for the low price of $7.95 per
  117. month.
  118. """
  119.     menu = ""
  120.     menu += "To add an account press 1\n"
  121.     menu += "To deposit money into an existing account press 2\n"
  122.     menu += "To withdrawal money from an existing account press 3\n"
  123.     menu += "To view all accounts press 4\n"
  124.     menu += "To view one account press 5\n"
  125.     #menu += "To sort by balance press 6\n"
  126.     menu += "To exit press 7\n"
  127.     option = 0
  128.     count = 0
  129.     while True:
  130.         option = int(input(menu))
  131.         if option is 1:# Create an account
  132.             name = raw_input("Enter your name: ")
  133.             account_number = count
  134.             ssn = int(input("Enter your social security number: "))
  135.             starting_balance = long(input("Enter your starting balance: "))
  136.             list.add_account(name, account_number, ssn, starting_balance)
  137.             print str(list)
  138.         elif option is 2:# Deposit
  139.             name = raw_input("Enter the name under which your account was created: ")
  140.             amount = long(input("Enter the amount to deposit into your account: "))
  141.             try:
  142.                 list.deposit(name, amount)
  143.             except:
  144.                 print "Unexpected error:", sys.exc_info()[0]
  145.                 raise
  146.             print list.show_account(list.search(name))
  147.         elif option is 3:# Withdraw
  148.             name = raw_input("Enter the name under which your account was created: ")
  149.             amount = long(input("Enter the amount that you wish to withdraw: "))
  150.             try:
  151.                 list.withdraw(name, amount)
  152.             except:
  153.                 print "Unexpected error:", sys.exc_info()[0]
  154.                 raise
  155.             print list.show_account(list.search(name))
  156.         elif option is 4:
  157.             print str(list)
  158.         elif option is 5:
  159.             name = raw_input("Enter then name under which the account you would like to view was created: ")
  160.             index = list.search(name)
  161.             output = list.show_account(index)
  162.             print str(output)
  163.             del output
  164.             del index
  165.         elif option is 6:
  166.             print "Not yet implemented"
  167.         elif option is 7:
  168.             print "exit"
  169.             break
  170.         else:
  171.             continue
  172.         count += 1
Add Comment
Please, Sign In to add comment