Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This is a comment.
- # Note: underscored variables are private. Well, they aren't really, but people
- # will tend to not mess with them if they're smart.
- # `class' denotes a class. Simple enough.
- # The colon begins the structure (this is not only for classes, but also with
- # functions, if/elif/else, for, while).
- # A code block indented by a uniform number of spaces or tabs is one
- # structure/construct. Every structure/construct in the program must match the
- # size and type (space/tab) of indentation. As soon as a line of at least one
- # less indentation is reached, the structure/construct is ended.
- class Account:
- # `__init__' is a built-in function (as are all underscored functions).
- # It is the constructor. `self' is like `this', but a little different.
- # If you need to access member variables from a function, you need `self'.
- # It's usually put as the first argument but it doesn't really matter.
- def __init__(self, name, account_number, ssn, starting_balance = 0):
- self._name = name
- self._account_number = account_number
- self._ssn = ssn
- self._balance = starting_balance
- def deposit(self, amount):
- self._balance += amount
- def withdraw(self, amount):
- self._balance -= amount
- def get_account_number(self):
- return self._account_number
- def get_balance(self):
- return self._balance
- def set_balance(self, amount):
- self._balance = amount
- def get_name(self):
- return self._name
- def set_name(self, name):
- self._name = name
- def get_ssn(self):
- return self._ssn
- # `__str__' is also a built-in function in Python. It's like `toString' in Java.
- def __str__(self):
- description = self._name + "\n "
- description += "Account Number: " + str(self._account_number) + "\n "
- description += "Social Security Number: " + str(self._ssn) + "\n "
- description += "Balance: " + str(self._balance)
- return description
- class AccountCollection:
- def __init__(self):
- self._collection = [30]
- for i in range(0, 30):
- #self._collection[i] = Account
- self._collection.append(Account)
- self._count = 0
- def add_account(self, name, account_number, ssn, balance):
- if count is len(self._collection):
- increase_size()
- self._collection[count] = Account(name, account_number, ssn, balance)
- self._count += 1
- def deposit(self, name, amount):
- index = self.search(name)
- if index >= 0:
- self._collection[index].deposit(amount)
- else:
- raise Exception("Name '" + name + "' not found.")
- def withdraw(self, name, amount):
- index = self.search(name)
- if index >= 0:
- self._collection[index].withdraw(amount)
- else:
- raise Exception("Name '" + name + "' not found.")
- def get_length(self):
- return len(self._collection)
- def increase_size(self):
- temp = []
- for i in range(0, (len(self._collection ) * 2)):
- temp[i] = Account
- for account in range(0, len(self._collection)):
- temp[account] = self._collection[account]
- self._collection = temp
- def search(self, name):
- match = -1
- for i in range(0, len(self._collection)):
- if self._collection[i].get_name() == name:
- match = i
- break
- else:
- match = -1
- return match
- def show_account(self, index):
- return self._collection[index]
- # Three single quotes is a multi-line comment for some reason.
- '''
- def sort(self):
- highest = 0.0
- sorted = []
- for i in range(0, len(self._collection)):
- for j in range(0, len(self._collection)):
- balance = self._collection[j].get_balance()
- if balance > highest:
- highest = balance
- else:
- highest = highest
- return sorted
- '''
- def __str__ (self):
- report = ""
- report += "Account List\n\n"
- report += " Number of Accounts: " + str(self._count) + "\n"
- for i in range(0, self._count):
- report += str(self._collection[i]) + "\n"
- return report
- if __name__ == "__main__":
- import sys
- list = AccountCollection()
- print """\
- Welcome to WeWillNotStealYourMoney bank, brought to you by Gamefly - the
- service that allows you to rent used games for the low price of $7.95 per
- month.
- """
- menu = ""
- menu += "To add an account press 1\n"
- menu += "To deposit money into an existing account press 2\n"
- menu += "To withdrawal money from an existing account press 3\n"
- menu += "To view all accounts press 4\n"
- menu += "To view one account press 5\n"
- #menu += "To sort by balance press 6\n"
- menu += "To exit press 7\n"
- option = 0
- count = 0
- while True:
- option = int(input(menu))
- if option is 1:# Create an account
- name = raw_input("Enter your name: ")
- account_number = count
- ssn = int(input("Enter your social security number: "))
- starting_balance = long(input("Enter your starting balance: "))
- list.add_account(name, account_number, ssn, starting_balance)
- print str(list)
- elif option is 2:# Deposit
- name = raw_input("Enter the name under which your account was created: ")
- amount = long(input("Enter the amount to deposit into your account: "))
- try:
- list.deposit(name, amount)
- except:
- print "Unexpected error:", sys.exc_info()[0]
- raise
- print list.show_account(list.search(name))
- elif option is 3:# Withdraw
- name = raw_input("Enter the name under which your account was created: ")
- amount = long(input("Enter the amount that you wish to withdraw: "))
- try:
- list.withdraw(name, amount)
- except:
- print "Unexpected error:", sys.exc_info()[0]
- raise
- print list.show_account(list.search(name))
- elif option is 4:
- print str(list)
- elif option is 5:
- name = raw_input("Enter then name under which the account you would like to view was created: ")
- index = list.search(name)
- output = list.show_account(index)
- print str(output)
- del output
- del index
- elif option is 6:
- print "Not yet implemented"
- elif option is 7:
- print "exit"
- break
- else:
- continue
- count += 1
Add Comment
Please, Sign In to add comment