REVISED: Sunday, March 3, 2013
This tutorial discusses the basic design of a Python 3 "credit card class".
I. PYTHON 3 "CREDIT CARD CLASS" SOURCE CODE
Consider the following Python 3 "credit card class" source code:
class CreditCard:
def __init__(self, title = " ", first_name = " ", last_name = " ", city = " ", state = " ", country = " ", zip_code = " ", card_number = 0, exp_month = " ", exp_year = 0, cvv2 = " ", card_type = " ", currency_code = " ", account = " ", line_of_credit = 0, beginning_balance = 0, account_balance = 0, charges = 0, overdraft_fees = 0, payments = 0, ending_balance = 0):
""" Opens an account. """
self.title = title
self.first_name = first_name
self.last_name = last_name
self.city = city
self.state = state
self.country = country
self.zip_code = zip_code
self.card_number = card_number
self.expiration_month = exp_month
self.expiration_year = exp_year
self.security_code = cvv2
self.card_type = card_type
self.currency_code = currency code
self.account = account
self.line_of_credit = line_of_credit
self.beginning_balance = beginning_balance
self.account_balance = account_balance
self.charges = charges
self.overdraft_fees = overdraft_fees
self.payments = payments
self.ending_balance = ending_balance
def create_account(self, account):
""" Establishes account. """
self.account = account
def create_line_of_credit(self, amount):
""" Establishes line of credit. """
self.line_of_credit += amount
def create_beginning_balance(self, amount):
""" Establiishes beginning balance. """
self.beginning_balance += amount
self.account_balance += amount
self.ending_balance += amount
def create_charges(self, amount):
"""Reduces the amount from the account balance.
Each charge resulting in an over draft negative account balance
also deducts a fee of 25 dollars from the account balance."""
self.account_balance -= amount
self.ending_balance -= amount
if self.account_balance < 0:
self.account_balance -= 25
self.ending_balance -= 25
self.overdraft_fee += 25
def create_payments(self, amount):
"""Deposits the amount into the account balance."""
self.account_balance += amount
self.ending_balance += amount
self.payments += amount
def display_line_of_credit(self):
"""Returns the current balance in the account."""
print ("Line Of Credit: %s\n" % str(self.line_of_credit))
def display_beginning_balance(self):
""" Returns the beginning balance. """
print ("Beginning Balance: %s" % str(self.beginning_balance))
def display_charges(self):
""" Returns total charges. """
print ("- Charges: %s" % str(self.charges))
def display_fees(self):
"""Returns the total fees."""
print ("- Overdraft Fees: %s" % str(self.overdraft_fee))
def display_payments(self):
""" Returns total payments. """
print ("+ Payments: %s" % str(self.payments))
def display_account_balance(self):
""" Returns the account balance. """
print ("Account Balance: %s" % str(self.account_balance))
def display_ending_balance(self):
""" Returns the ending balance. """
print ("Ending Balance: %s" % str(self.ending_balance))
def __str__(self):
s = "Account: %s\n" % (self.account)
s += "Line Of Credit: %s\n" % (self.line_of_credit)
s += "Beginning Balance: %s\n" % (self.beginning_balance)
s += "- Charges: %s\n" % (self.charges)
s += "- Overdraft Fees: %s\n" % (self.overdraft_fees)
s += "+ Payments: %s\n" % (self.payments)
s += "Ending Balance: %s\n" % (self.ending_balance)
return s
def __init__(self, title = " ", first_name = " ", last_name = " ", city = " ", state = " ", country = " ", zip_code = " ", card_number = 0, exp_month = " ", exp_year = 0, cvv2 = " ", card_type = " ", currency_code = " ", account = " ", line_of_credit = 0, beginning_balance = 0, account_balance = 0, charges = 0, overdraft_fees = 0, payments = 0, ending_balance = 0):
""" Opens an account. """
self.title = title
self.first_name = first_name
self.last_name = last_name
self.city = city
self.state = state
self.country = country
self.zip_code = zip_code
self.card_number = card_number
self.expiration_month = exp_month
self.expiration_year = exp_year
self.security_code = cvv2
self.card_type = card_type
self.currency_code = currency code
self.account = account
self.line_of_credit = line_of_credit
self.beginning_balance = beginning_balance
self.account_balance = account_balance
self.charges = charges
self.overdraft_fees = overdraft_fees
self.payments = payments
self.ending_balance = ending_balance
def create_account(self, account):
""" Establishes account. """
self.account = account
def create_line_of_credit(self, amount):
""" Establishes line of credit. """
self.line_of_credit += amount
def create_beginning_balance(self, amount):
""" Establiishes beginning balance. """
self.beginning_balance += amount
self.account_balance += amount
self.ending_balance += amount
def create_charges(self, amount):
"""Reduces the amount from the account balance.
Each charge resulting in an over draft negative account balance
also deducts a fee of 25 dollars from the account balance."""
self.account_balance -= amount
self.ending_balance -= amount
if self.account_balance < 0:
self.account_balance -= 25
self.ending_balance -= 25
self.overdraft_fee += 25
def create_payments(self, amount):
"""Deposits the amount into the account balance."""
self.account_balance += amount
self.ending_balance += amount
self.payments += amount
def display_line_of_credit(self):
"""Returns the current balance in the account."""
print ("Line Of Credit: %s\n" % str(self.line_of_credit))
def display_beginning_balance(self):
""" Returns the beginning balance. """
print ("Beginning Balance: %s" % str(self.beginning_balance))
def display_charges(self):
""" Returns total charges. """
print ("- Charges: %s" % str(self.charges))
def display_fees(self):
"""Returns the total fees."""
print ("- Overdraft Fees: %s" % str(self.overdraft_fee))
def display_payments(self):
""" Returns total payments. """
print ("+ Payments: %s" % str(self.payments))
def display_account_balance(self):
""" Returns the account balance. """
print ("Account Balance: %s" % str(self.account_balance))
def display_ending_balance(self):
""" Returns the ending balance. """
print ("Ending Balance: %s" % str(self.ending_balance))
def __str__(self):
s = "Account: %s\n" % (self.account)
s += "Line Of Credit: %s\n" % (self.line_of_credit)
s += "Beginning Balance: %s\n" % (self.beginning_balance)
s += "- Charges: %s\n" % (self.charges)
s += "- Overdraft Fees: %s\n" % (self.overdraft_fees)
s += "+ Payments: %s\n" % (self.payments)
s += "Ending Balance: %s\n" % (self.ending_balance)
return s
II. PYTHON 3 "CREDIT CARD CLASS" OBJECT INSTANTIATION
def main():
my_creditcard = CreditCard()
my_creditcard = CreditCard()
III. CALLING PYTHON 3 "CREDIT CARD CLASS" OBJECT METHODS
my_creditcard.create_account("my_creditcard")
my_creditcard.create_line_of_credit(5000)
my_creditcard.create_line_of_credit(5000)
my_creditcard.create_beginning_balance(4000)
my_creditcard.create_charges(3000)
my_creditcard.create_payments(2000)
IV. DISPLAYING PYTHON 3 "CREDIT CARD CLASS" DATA
print (my_creditcard)
if __name__ == '__main__':
main()
main()
V. SAMPLE PYTHON 3 "CREDIT CARD CLASS" DISPLAY OUTPUT
>>>
Account: my_creditcard
Line Of Credit: 5000
Beginning Balance: 4000
- Charges: 3000
- Overdraft Fees: 0
+ Payments: 2000
Ending Balance: 3000
>>>
Account: my_creditcard
Line Of Credit: 5000
Beginning Balance: 4000
- Charges: 3000
- Overdraft Fees: 0
+ Payments: 2000
Ending Balance: 3000
>>>
VI. REFERENCES
The Python Tutorial by Guido van Rossum and Fred L. Drake, Jr., editor (2003)
Style Guide for Python Code by Guido van Rossum and Barry Warsaw (2001)
In this tutorial, you have learned the basic design of a Python 3 "credit card class".
Elcric Otto Circle
-->
-->
-->
How to Link to My Home Page
It will appear on your website as:"Link to: ELCRIC OTTO CIRCLE's Home Page"
No comments:
Post a Comment