Skip to content
Advertisement

Money denomination program for ATM in js that would be flexible to handle and dispense money in minimum notes

the code should be able to handle any amount up to 20000, For example, suppose the Entered amount is 2600 when the balance in the card is 3000. Will output following : New Balance – 400 Notes: 2000 * 1 500 * 1 100 * 1

(only three banknotes 2000, 500, 100) and the cash limit is 20000

I am new in the javascript world, and I am not able to write the code, could anyone help me out??? please!

var h = 5;
var f = 2;
var t = 1;


var ifAmtLessThn2000 = ifAmtLessThn2000(n) {
    var temp;
    if (n < 500) {
        h += (n / 100);
        return {
            h
        }
    } else if (n >= 500 && n < 2000) {
        f += n / 500;
        h += (n - 500) / 100;
        return {
            h,
            f
        }
    } else {
        temp = n - 1500;
        if (temp < 500) {
            h += (temp / 100);
            return {
                h
            }
            console.log('hundred : ' + h);
        } else {
            f += 1;
            h += (temp - 500) / 100;
            console.log('five hundred : ' + f);
            console.log('hundred : ' + h);
            return {
                f,
                h
            }
        }
    }
}


var ifAmtGreaterthan2000 = (n) => {
    var h = 0;
    var f = 0;
    var t = 0;
    var tt = 0;
    var temp;

    if (n < 2000) {
        tt += (n / 2000);
    }
    else if (n >= 2000 && n < 10000) {
        f += n / 500;
        h += (n - 500) / 100;
    }
    else {
        temp = n - 1500;
        if (temp < 500) {
            h += (temp / 100);
        }
        else {
            f += 1;
            h += (temp - 500) / 100;
        }
    }
}




var checkAmt = (n) => {
    if (n < 100 || (n % 100) > 0) {
        console.log("Invalid Amount : less than 100 ");
    } else {
        if (n > 20000) {
            console.log("ATM Cash Limit exceeds.");
        } else {
            if (n <= 2500) {
                ifAmtLessThn2500(n);
                console.log(h + " x 100");
                console.log(f + " x 500");
            } else {
                temp = n - 2500;
                t += temp / 1000;
                if (temp > 500)
                    temp = temp - (1000 * (t - 1));
                ifAmtLessThn2500(temp);
                console.log(h + " x 100");
                console.log(f + " x 500");
                console.log(t + " x 1000");
            }
        }
    }

}

checkAmt(2500);

Sorry for a dumb program, but I need help please can anyone give me a solution in typeScript code, returning the req denomination in array!!

Advertisement

Answer

amtArray = [2000, 500, 100];  // the denomination you want to find.

 for (let i = 0; i < this.amtArray.length; i++) {
            this.resultArray.push(Math.floor(total / this.amtArray[i]));
            // Get the new total
            total = total % this.amtArray[i];
        }
        var twothousands_notes = this.resultArray[0];
        var fivehundred_notes = this.resultArray[1];
        var hundred_notes = this.resultArray[2];

        console.log('calculated amt : ' + '100 : ' +
            hundred_notes + '   500 :  ' +
            fivehundred_notes + '  2000 :  ' +
            twothousands_notes);

Based on the amount you can adjust the logic.

Hope this helps.. 🙂

Advertisement