Aer/tests/luhn_verify.aer
belliash 55acf8111f
All checks were successful
The build was successful.
Assume private visibility for all class members by default.
In most (all?) modern OOP languages class members visibility is assumed to be private and programmer has to consciously set it to public or protected. PHP has the different approach what can cause a security flaws in written scripts. AerScript will not follow this way, as it seems to be conceptually broken.
2019-05-17 08:40:41 +02:00

45 lines
1016 B
Plaintext

class Luhn {
private string $number;
public string getNumber() {
return $this->number;
}
public void setNumber(string $number) {
$this->number = $number;
}
public bool validate() {
string $sum;
string $revNumber;
int $len;
$revNumber = strrev($this->number);
$len = strlen($this->number);
for(int $i = 0; $i < $len; $i++) {
$sum += $i & 1 ? $revNumber[$i] * 2 : $revNumber[$i];
}
return array_sum(str_split($sum)) % 10 == 0;
}
}
class Program {
private const NUMBERS = {'3788803280', '6487308345', '5443489710530865', '5539266155200609', '4024007151066296', '4345234978'};
public void main() {
int $i, $nums = sizeof($this->NUMBERS);
object $luhn = new Luhn();
while($i < $nums) {
$luhn->setNumber($this->NUMBERS[$i]);
if($luhn->validate()) {
print('The number ' + $luhn->getNumber() + ' has passed the Luhn validation.' + "\n");
} else {
print('The number ' + $luhn->getNumber() + ' has NOT passed the Luhn validation.' + "\n");
}
$i++;
}
}
}