Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!!

like image 602
Abhishek Gautam Avatar asked Oct 17 '25 07:10

Abhishek Gautam


2 Answers

const withdraw = (amount) => {
  let hundredNotes = 0;
  let fiftyNotes = 0;
  let twentyNotes = 0;

  while (amount >= 20) {
    if (
      amount >= 100 &&
      ((amount % 100) % 50 === 0 || (amount % 100) % 20 === 0)
    ) {
      amount -= 100;
      hundredNotes++;
    } else if (
      amount >= 50 &&
      ((amount % 50) % 100 === 0 || (amount % 50) % 20 === 0)
    ) {
      amount -= 50;
      fiftyNotes++;
    } else {
      amount -= 20;
      twentyNotes++;
    }
  }
  return [hundredNotes, fiftyNotes, twentyNotes];
};


console.log(withdraw(230));

console.log(withdraw(250));
like image 71
Muhammad Huzaifa Avatar answered Oct 19 '25 20:10

Muhammad Huzaifa


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.. :)

like image 28
Abhishek Gautam Avatar answered Oct 19 '25 22:10

Abhishek Gautam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!