Learn practical skills, build real-world projects, and advance your career
Created 4 years ago
class CreditCard:
''' A consumer credit card.'''
def __init__(self, customer, bank, acnt, limit):
'''Create a new credit card instance.
The initial balance is zero.
customer the name of the customer (e.g., John Bowman )
bank the name of the bank (e.g., California Savings )
acnt the acount identifier (e.g., 5391 0375 9387 5309 )
limit credit limit (measured in dollars)
'''
self.customer = customer
self.bank = bank
self.account = acnt
self.limit = limit
self.balance = 0
def get_customer(self):
'''Return name of the customer.'''
return self.customer
def get_bank(self):
'''Return the bank's name.'''
return self.bank
def get_account(self):
'''Return the card identifying number (typically stored as a string).'''
return self.account
def get_limit(self):
'''Return current credit limit.'''
return self.limit
def get_balance(self):
'''Return current balance.'''
return self.balance
def charge(self, price):
'''Charge given price to the card, assuming sufficient credit limit.
Return True if charge was processed; False if charge was denied.
'''
if price + self.balance > self.limit: # if charge would exceed limit,
return False # cannot accept charge
else:
self.balance += price
return True
def make_payment(self, amount):
'''Process customer payment that reduces balance.'''
self.balance -= amount
def make_donation(self, amount, donation_code):
'''Process customer donation that reduces balance and give it to a particular indastry'''
self.__transfer_money(amount, indastry_code = donation_code)
def __transfer_money(self, amount, card_num=None, indastry_code=None):# private method
if card_num is not None:
if self.is_valid(card_num):
self.make_payment(amount)
self.get_account(card_num).charge(amount)
else:
return "Enter valid Card Number."
if indastry_code is not None:
if self.is_valid(indastry_code):
self.make_payment(amount)
self.get_account(indastry_code).charge(amount)
else:
return "Enter valid Indastry Code."
-
init: Constructor: Used to Initialize the data when created and prepar it to use for later time, init execute before calling any method in class.
-
self: mean each variable or method in the class are owned by the class, self.customer = CreditCard.customer.
-
__name: private
# Testing
if __name__=="__main__":
wallet1 = CreditCard("John Bowman", "California Savings",5391037593875309, 2500)
wallet2 = CreditCard("John Bowman", "California Federal",3485039933951954, 3500)
wallet3 = CreditCard("John Bowman", "California Finance",5391037593875309, 5000)
wallet = [wallet1,wallet2,wallet3]
for val in range(1, 17):
wallet[0].charge(val)
wallet[1].charge(2*val)
wallet[2].charge(3*val)
for c in range(3):
print("Customer =", wallet[c].get_customer())
print("Bank =", wallet[c].get_bank())
print("Account =", wallet[c].get_account())
print("Limit =", wallet[c].get_limit())
print("Balance =", wallet[c].get_balance())
while wallet[c].get_balance( ) > 100:
wallet[c].make_payment(100)
print("New balance =", wallet[c].get_balance())
print()
Customer = John Bowman
Bank = California Savings
Account = 5391037593875309
Limit = 2500
Balance = 136
New balance = 36
Customer = John Bowman
Bank = California Federal
Account = 3485039933951954
Limit = 3500
Balance = 272
New balance = 172
New balance = 72
Customer = John Bowman
Bank = California Finance
Account = 5391037593875309
Limit = 5000
Balance = 408
New balance = 308
New balance = 208
New balance = 108
New balance = 8
help(CreditCard)
Help on class CreditCard in module __main__:
class CreditCard(builtins.object)
| CreditCard(customer, bank, acnt, limit)
|
| A consumer credit card.
|
| Methods defined here:
|
| __init__(self, customer, bank, acnt, limit)
| Create a new credit card instance.
| The initial balance is zero.
| customer the name of the customer (e.g., John Bowman )
| bank the name of the bank (e.g., California Savings )
| acnt the acount identifier (e.g., 5391 0375 9387 5309 )
| limit credit limit (measured in dollars)
|
| charge(self, price)
| Charge given price to the card, assuming sufficient credit limit.
| Return True if charge was processed; False if charge was denied.
|
| get_account(self)
| Return the card identifying number (typically stored as a string).
|
| get_balance(self)
| Return current balance.
|
| get_bank(self)
| Return the bank's name.
|
| get_customer(self)
| Return name of the customer.
|
| get_limit(self)
| Return current credit limit.
|
| make_payment(self, amount)
| Process customer payment that reduces balance.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)