Algorithm for ATM cash dispenser in denominations of $20 and $50

Saurabh Staff asked 11 months ago

How to design/develop a cash dispensing application (algorithm) for use in an ATM or similar device ?

Mandatory Feature Set

For an ATM-style of machine (with $20 and $50 note denominations), the following dispensed amounts are the test cases:

· $20

· $40

· $50

· $70

· $80

· $100

· $150

· $60

· $110

· $200, when there is only 3x$50 notes and 8x$20 notes available.

1 Answers
Best Answer
Saurabh Staff answered 11 months ago

Codepen at:- https://codepen.io/golibrary/pen/pWRKmV
 
Algorithm given below in javascript:- (Can be improved further)
 
 
 
 

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
init();

function init() {
    var denominations = {
        "50": 3,
        "20": 8
    }

    var totalCash = denominations["50"] * 50 + denominations["20"] * 20;
    var amountToDispense = [20, 40, 50, 70, 80, 100, 150, 60, 110, 200, 310, 10, 30, 130];
    for (i = 0; i = d1 && ((remainderAmount % d2 !== 0) || (remainderAmount % d1 === 0))) {

            if (remainderAmount % d1 === 0) {
                d1Count = remainderAmount / d1;
                remainderAmount = 0;
            } else {
                remainderAmount = remainderAmount - d1;
                d1Count++;
            }
        }
        if (remainderAmount && (remainderAmount % d2 === 0)) {
            d2Count = remainderAmount / d2;
        }

        if (d1 * d1Count + d2 * d2Count === amount) {
            if (checkDenominations(denominations, d1Count, d2Count)) {
                console.log("Dispensing Cash $", amount, "=", d1, "*", d1Count, "+", d2, "*", d2Count);
            } else {
                console.log("Recalculating for amount = $", amount);
                remainderAmount = amount;
                while (1) {
                    d1Count = denominations["50"];
                    //remainderAmount = remainderAmount - d1*d1Count;
                    if ((remainderAmount - d1 * d1Count) % d1 === 0) {
                        d1Count--;
                        d2Count = recalculate(remainderAmount, d1, d2, d1Count);
                        console.log("Dispensing Cash $", amount, "=", d1, "*", d1Count, "+", d2, "*", d2Count);
                        break;
                    } else {
                        d2Count = recalculate(remainderAmount, d1, d2, d1Count);
                        console.log("Dispensing Cash $", amount, "=", d1, "*", d1Count, "+", d2, "*", d2Count);
                        break;
                    }
                }
            }
            break;
        } else {
            console.log("Sorry, available denominations are $20 & $50!! Given amount of $", amount, " cannot be dispensed");
            break;
        }
    }
}


function recalculate(remainderAmount, d1, d2, d1Count) {
    /*if ((remainderAmount - d1*d1Count) % d2 === 0) {
        d2Count = (remainderAmount - d1*d1Count)/d2;
        return d2Count;
    }*/
    return (remainderAmount - d1 * d1Count) / d2;
}

function checkDenominations(denominations, x, y) {
    return denominations["50"] >= x && denominations["20"] >= y;
}