Wednesday, 15 April 2015

ATM Cashwithdraw Problem and Solved



Problem statements here :-

  • ATM dispenses amount base on user enter amount.
  • Amount must be multiple of 50 
  • Should be parallel withdraw system, multiple user can withdraw amount at same time.
  • Availability of various Denominator in ATM i.e 50,100,500,1000.

ATMMachine Java file :

package com.atm;

import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.TreeMap;
import java.util.TreeSet;

public class ATMMachine {

    private final Map<Integer, Integer> denominatorMap;

    public ATMMachine(Map<Integer, Integer> denoMap) {
        this.denominatorMap = denoMap;
    }

    private boolean isValidAmt(int amt) {
        return amt % 50 == 0 ? true : false;
    }

    private int getDenominatorNotes(int denominator) {
        return denominatorMap.get(denominator);
    }

    public synchronized Map<Integer, Integer> withDrawAmt(int amt) {
        if (!isValidAmt(amt)) {
            System.out.println(" Please enter amount multiple by 50 only.");
        }
        Map<Integer, Integer> returnMap = new TreeMap<Integer, Integer>();
        Iterator<Integer> iteratorOfDenominator = new TreeSet<Integer>(
                denominatorMap.keySet()).descendingIterator();
        while (amt > 0) {
            Integer denominator =0;
            try{
            denominator = iteratorOfDenominator.next();
            }catch(NoSuchElementException e){
                System.out.println(" Please enter another amount multiple by 100");
                break;
            }
            Integer noOfNotes = amt < denominator ? 0 : amt / denominator;
            if (noOfNotes > getDenominatorNotes(denominator)) {
                noOfNotes = getDenominatorNotes(denominator);
            }
            /*System.out.printf(" amt %d denominator %d NoOfNotes %d\n", amt,
                    denominator, noOfNotes);
            */
            amt = amt - (denominator * noOfNotes);
            returnMap.put(denominator, noOfNotes);
            reduceBalance(denominator, noOfNotes);
        }
        return returnMap;
    }

    public String printWithDrawAmtWithDenominator(
            Map<Integer, Integer> returnMap) {

        StringBuffer sb = new StringBuffer();
        for (Integer denominator : returnMap.keySet()) {
            sb.append("").append(denominator).append(" * ")
                    .append(returnMap.get(denominator)).append(" = ")
                    .append(denominator * returnMap.get(denominator))
                    .append(",\n");
        }
        return sb.toString();
    }

    private synchronized void reduceBalance(int denominator, int amt) {
        Integer _denominator_amt = denominatorMap.get(denominator);
        denominatorMap.put(denominator, (_denominator_amt - amt));
    }

    public synchronized int getBalanceATMAmt() {
        int balance = 0;
        for (Integer denominator : denominatorMap.keySet()) {
            balance += (denominator * denominatorMap.get(denominator));
        }
        return balance;
    }

}
 
 ATMUser Java file :

package com.atm;

public class ATMUser implements Runnable{

    private final ATMMachine machine;
    private final int amt;
    public ATMUser(ATMMachine machine,int amount){
        this.machine = machine;
        this.amt = amount;
    }
   
    @Override
    public void run() {
        System.out.println(" Request Amount is :"+this.amt);
        System.out.println(" Total Amount Balance Before withdraw "+machine.getBalanceATMAmt());
        System.out.println(" Receive Amount From ATM :"+machine.printWithDrawAmtWithDenominator(machine.withDrawAmt(this.amt)));
        System.out.println(" Total Amount Balance Before withdraw "+machine.getBalanceATMAmt());
    }

}



Main Java Program :-

package com.atm;

import java.util.HashMap;
import java.util.Map;

public class Main {

   
    public static void main(String args[]){
       
        ATMMachine machine = new ATMMachine(depositeAmtToATM());
       
        ATMUser user1 = new ATMUser(machine, 54050);
       
        Thread t1 = new Thread(user1);
        t1.start();
       
    }
   
    private static Map<Integer,Integer> depositeAmtToATM(){
        Map<Integer,Integer> ATMDenominator = new HashMap<Integer, Integer>();
        ATMDenominator.put(1000,120);
        ATMDenominator.put(500,220);
        ATMDenominator.put(100,500);
        ATMDenominator.put(50,10);
        return ATMDenominator;
    }
}



Please write here your suggestion... thanks

No comments:

Post a Comment