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() < card:
return True
else:
return False
def check_code(self):
"""Contains the algorithm to check if the card code is authentic"""
code_list = list(str(self.card_code))
check_digit = int(code_list[7])
code_list.pop()
# The last digit is assigned to be a check digit and is removed from the list.
code_list.reverse()
for item in code_list:
temp_location = code_list.index(item)
if is_even(temp_location):
code_list[temp_location] = int(item) * 2
# Loops over each digit, if it is even, multiplies the digit by 2.
for item in code_list:
temp_location = code_list.index(item)
if int(item) > 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
else:
return False
# If the code is divisible by 10, returns True, else, it returns False.
def check_auth(self):
"""Checks the card's authenticity. """
if self.check_code() and self.check_date():
print("----------------------")
print("Valid")
print(self.name)
print(self.postcode)
print(self.card_date)
print(self.card_code)
else:
print("----------------------")
print("Invalid")
print(self.name)
print(self.postcode)
def is_even(number):
"""Function used to test if a number is even."""
if number % 2 == 0:
return True
else:
return False
if __name__ == "__main__":
customer().check_auth()
"%d/%m/%Y"
most card I know only care about the month and year \$\endgroup\$