Skip to content

Instantly share code, notes, and snippets.

@harshpatel
Created October 17, 2014 04:49
Show Gist options
  • Save harshpatel/b21d8802f32c8c8fdf53 to your computer and use it in GitHub Desktop.
Save harshpatel/b21d8802f32c8c8fdf53 to your computer and use it in GitHub Desktop.
class Card:
"""a credit card object, referenced in both person and bank with which it is associated
has a calculate_interest method, charge method, cash advance method and pay_balance method"""
def __init__(self, person, limit, rate):
self.owner = person
self.limit = limit
self.rate = rate
self.balance = 0
#I guess we could also store each transaction separately with dates, but... no.
print "" %()
def calculate_interest(self, amount):
"""takes an amount and calculates interest based on cards rate, returning that interest"""
return amount*self.rate
def charge(self, amount):
"""takes an amount validates and adds the balance to the card and returns a boolean signifying
success or failure of the transaction"""
if (self.balance + amount <= self.limit) and (amount > 0):
self.balance = self.balance + amount
return True
else: return False
def cash_advance(self, amount):
"""takes an amount, adds interest, validates and adds the balance to the card and the amount
to the owner's "cash", and returns a boolean signifying success or failure of the transaction"""
interest = self.calculate_interest(amount)
total = amount + interest
if (self.balance + total <= self.limit) and (total > 0):
self.balance = self.balance + total
self.owner.cash = self.owner.cash + amount
return True
def pay_balance(self, amount):
"""takes an amount and pays down the balance of the credit card by that amount"""
if (amount > 0) and (self.owner.cash - amount > 0):
self.balance = self.balance - amount
self.owner.cash = self.owner.cash - amount
class Person:
"""Jane Doe, Amercian Everywoman"""
def __init__(self, name, cash):
self.name = name
self.cash = cash
self.cards = {}
print "Greetings, %s. You have $%d!" %(self.name, self.cash)
class Bank:
"""a bank object can open accounts (open_account) and grant credit cards (get_credit_card),
count_deposits, and card_balances, and call the withdraw, deposit and transfer methods"""
def __init__(self, name):
self.name = name
self.account = {}
self.card = {}
print self.name, "was just created"
def count_deposits(self):
"""returns total amount of deposit money held in the bank"""
deposits = 0.0
for key in self.account:
deposits = deposits + self.account[key]
print "%s is holding $%d in deposits" %(self.name, deposits)
return deposits
def open_account(self, person):
"""takes a person object and creates an account stored in a hash,
accessed by a key that is the person object's name attribute"""
self.account[person.name] = 0.0
print "%s, thanks for opening an account at %s" %(person.name, self.name)
def withdraw(self, person, amount, log=True):
"""takes an amount and a person and validates then removes that amount
from bank account and adding it to the person's cash attribute. Returns a boolean"""
name = person.name
account = self.account
if (name in account) and (account[name] - amount > 0):
account[name] = account[name] - amount
person.cash = person.cash + amount
if log: print "%s withdrew $%d from %s. %s has $%d cash. %s's account has $%d" %(name, amount, self.name, name, person.cash, name, account[name])
return True
else:
print "Epic withdrawal fail. Unable to withdraw",amount
return False
def deposit(self, person, amount, log=True):
"""takes an amount and a person and validates then removes that amount
from the cash attribute and adding it to the bank account. Returns a boolean"""
name = person.name
account = self.account
if (name in account) and (amount > 0) and (person.cash - amount > 0):
account[name] = account[name] + amount
person.cash = person.cash - amount
if log: print "%s deposited $%d in %s. %s has $%d cash. %s's account has $%d" %(name, amount, self.name, name, person.cash, name, account[name])
return True
else:
print "Epic deposit fail. Unable to deposit",amount
return False
def transfer(self, person, bank, amount):
"""takes an amount, a person and another bank. Moves amount from person's account
in this object to person's account in other bank object. Returns a boolean"""
if self.withdraw(person, amount, False):
if bank.deposit(person, amount, False):
print "%s transfered $%d from the %s account to the %s account. The %s account has $%d and the %s account has $%d" %(person.name, amount, self.name, bank.name, self.name, self.account[person.name], bank.name, bank.account[person.name])
return True
else:
self.deposit(person, amount, False)
return False
else:
return False
def get_credit_card(self, person, limit, rate):
"""Takes a person, credit limit and rate. Creates a card object referenced by both
the bank and the person objects"""
credit_card = Card(person, limit, rate)
self.card[person.name] = credit_card
card_name = "%s %d %d percent" %(self.name, limit, rate*100)
person.cards[card_name] = credit_card
print "The", self.name, "is issuing a", card_name, "card to", person.name
def card_balances(self):
"""returns total credit card debt owed to bank"""
balances = 0.0
for key in self.card:
card = self.card[key]
balances = balances + card.balance
print "%s is owed $%d in credit card debt" %(self.name, balances)
return balances
#TESTS
if __name__ == '__main__':
TD = Bank("Toronto Dominion")
RBC = Bank("Royal Bank of Canada")
me = Person("Gandalf", 500);
some_guy = Person("Bilbo", 1000);
TD.open_account(me)
RBC.open_account(me)
TD.open_account(some_guy)
RBC.open_account(some_guy)
TD.deposit(me, 200)
TD.deposit(some_guy, 300)
TD.withdraw(me, 50)
TD.transfer(me, RBC, 100)
TD.deposit(me, 5000)
TD.withdraw(me, 5000)
TD.count_deposits()
RBC.count_deposits()
print "\n TESTING CREDIT CARD"
RBC.get_credit_card(me, 5000, .18)
for key in me.cards:
card = me.cards[key]
card.charge(1250.50)
print "charged card for 1250.50. New card balance is", card.balance
card.pay_balance(250.50)
print "Payed balance of 250.50. New card balance is", card.balance, "- money in pocket is", me.cash
card.cash_advance(2500)
print "Withdrew cash advance of 2500 + 18 percent interest. New card balance is", card.balance, "- money in pocket is", me.cash
RBC.card_balances()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment