Sobes.tech
Middle+

Rakendage ja testige kaardi valideerimise klassi puuduvad meetodid. Kuupäeva kehtivuse kontrolli meetod on juba veidots bez kļūdām. Osaline lahendus nõuab vaid 2 puuduvate meetodite rakendamist, mis kontrollivad kaardi omadusi.

sobes.tech AI

Vastus AI-lt

To implement the missing methods in the card validation class, you can, for example, add a check for the correctness of the card number and a CVV check.

Python example:

class CardValidator:
    def __init__(self, number, expiry_date, cvv):
        self.number = number
        self.expiry_date = expiry_date
        self.cvv = cvv

    def validate_expiry_date(self):
        # Implemented
        pass

    def validate_number(self):
        # Card number check using Luhn algorithm
        def luhn_checksum(card_number):
            def digits_of(n):
                return [int(d) for d in str(n)]
            digits = digits_of(card_number)
            odd_digits = digits[-1::-2]
            even_digits = digits[-2::-2]
            checksum = sum(odd_digits)
            for d in even_digits:
                checksum += sum(digits_of(d*2))
            return checksum % 10

        return luhn_checksum(self.number) == 0

    def validate_cvv(self):
        # CVV should be 3 or 4 digits
        return self.cvv.isdigit() and len(self.cvv) in (3, 4)

# Test
validator = CardValidator('4532015112830366', '12/25', '123')
assert validator.validate_number() == True
assert validator.validate_cvv() == True

This way, methods that check the properties of the card are implemented, in addition to the already existing date check.