HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Credit card validation - Python 3.4

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pythoncreditvalidationcard

Problem

Just completed a piece of code - a credit card validation program in python - as a little side project. Having no experience with classes in the past, I decided to employ classes in this project.

I am wondering if you could review my code, both appraising the actual code, but also evaluate my use of OOP/classes.

The requirements for the program are:


The user enters their name, postcode, the card code, and the card
date.


The eighth digit of the card code is removed and acts as a check digit


The code is then reversed


The 1st, 3rd, 5th, and 7th digits are multiplied by 2


If the result of the multiplication is > 9, subtract 9 from it.


If the sum of the 7 digits, and the check digit are divisable by 10,
the code is valid


The card date must also be in the future.


Finally, output their name, postcode, card number, and whether it is
valid or not.

The code:

```
"""Program used to check if a credit card is authentic."""
# !/usr/bin/env python
# -- coding: utf-8 --

# Check it out
import datetime

class Customer:
"""Class representing the customer and their credit card details"""

# Constructor
def __init__(self):
self.name = input("Name: ")
self.postcode = input("Postcode: ")
self.card_date = input("Card date: ")
self.card_code = input("Card code: ").strip()

def check_date(self):
"""Checks current date against the credit card's date. If it is valid, returns True; else False."""
card = datetime.datetime.strptime(self.card_date, "%d/%m/%Y").date()
if datetime.date.today() 9:
code_list[temp_location] = int(item) - 9
# For each digit, if it is greater than 9; 9 is subtracted from it.

sum_list = 0
for item in code_list:
sum_list += int(item)
# Calculates the sum of the digits

code_total = sum_list + int(check_digit)

if code_total % 10 == 0:
return True
e

Solution

I think there is this code organization issue - you have a class named Customer, but it, aside from the .name attribute, consists of credit-card related logic only.

I would also pass the obtained attributes to the class constructor instead of asking for them inside it:

def __init__(self, name, post_code, card_date, card_code):
    self.name = name
    self.post_code = post_code
    self.card_date = card_date
    self.card_code = card_code


It is a little bit cleaner to do this way since now our class is more generic, it is agnostic of where the attributes are coming from.

Some other code-style related notes:

  • consistent naming: rename postcode to post_code



  • revise the quality and necessity of comments: there is probably not much sense in having a comment # Constructor



-
you can simplify the way you return a boolean result from your methods. For instance, you can replace:

if datetime.date.today() < card:
    return True
else:
    return False


with:

return datetime.date.today() < card


And, it's worth mentioning that, generally speaking, if you doing this for production, you should not be reinventing the wheel and switch to a more mature, well-used and tested package like pycard.

Code Snippets

def __init__(self, name, post_code, card_date, card_code):
    self.name = name
    self.post_code = post_code
    self.card_date = card_date
    self.card_code = card_code
if datetime.date.today() < card:
    return True
else:
    return False
return datetime.date.today() < card

Context

StackExchange Code Review Q#161089, answer score: 8

Revisions (0)

No revisions yet.