-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
87 lines (80 loc) · 2.61 KB
/
generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Set attributes on generate method
* @param { String} code
* @param {String} nuban
*/
class NJbank{
constructor(options){
this.digits = [3,7,3,3,7,3,3,7,3,3,7,3];
this.options = {
accountNumber: 0
};
this.accountNumber = this.options.accountNumber;
Object.assign(this.options, options);
}
generate(code){
this.bankUniqueCode = code;
this.nuban = this.randomNumberGenerator();
this.checksum = 0;
this.accountNumber = 0;
//CHECK FOR ERRORS
if(typeof this.bankUniqueCode != "string" || this.bankUniqueCode.length != 3){
console.log("ERROR:Bank unique code must be of length 3 and must be a string");
return null;
}
if(typeof this.nuban != "string" || this.nuban.length != 9 ){
console.log("ERORR:The NUBAN must be of length 9 and must be a string ");
return null;
}
this.checksum = this.checkSum();
this.accountNumber = this.nuban+""+this.checksum;
return this.accountNumber
}
set(code, nuban){
this.bankUniqueCode = code;
this.nuban = nuban;
this.checksum = this.checkSum();
this.accountNumber = this.nuban+""+this.checksum;
if(isNaN(this.checksum)){
console.log("ERROR: Invalid bank code or NUBAN")
return null;
}else{
return this.accountNumber
}
}
checkSum(){
//The check digits algorithm
//STEP 1.
this.accountNumber = this.bankUniqueCode+""+this.nuban;
this.accountNumber = this.accountNumber.split("");
let total = 0;
let modulo =0;
for(let i=0; i < this.accountNumber.length; i++){
total += this.accountNumber[i]* this.digits[i];
}
modulo = (total % 10 );
//STEP 2.
this.checksum = 10 - modulo;
this.checksum = (this.checksum == 10)? 0 : this.checksum;
//SETP 3.
return this.checksum;
}
randomNumberGenerator(){
var rndNumber = (Math.floor((Math.random()*900000000) + 100000000)).toString();
return rndNumber;
}
}
module.exports = NJbank;
const NJbankNumber = new NJbank();
//FOR FIRST BANK
console.log("FIRST BANK")
console.log(`::::::${NJbankNumber.generate("011")}`)
//FOR ACCESS BANK
console.log("ACCESS BANK")
console.log(`::::::${NJbankNumber.generate("044")}`)
//FOR HIGH STREET
console.log("HIGH STREET")
console.log(`::::::${NJbankNumber.generate("125")}`)
//FOR HIGH STREET
console.log("HIGH STREET")
console.log(`::::::${NJbankNumber.set("123", "682177105")}`)