Return to Snippet

Revision: 64288
at July 19, 2013 08:18 by rnavanee


Initial Code
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChangeCalculator {

    /**
     * @param args
     */
    public static void main(String[] args) {
        /*Scanner scn = new Scanner(System.in);
        Integer amount = scn.nextInt();*/
        Integer amount = 105;
        Map<String, Integer> changeMap = getChange(amount);
        System.out.println(changeMap);

    }
    
    private static Map<String, Integer> getChange(Integer amount) {
        Map<String, Integer> changeMap = new HashMap<String, Integer>();
        List<Integer> changeList = new ArrayList<Integer>();
        changeList.add(50);
        changeList.add(20);
        changeList.add(10);
        changeList.add(5);
        changeList.add(2);
        changeList.add(1);
        Integer remAmt = amount;
        for (Integer change : changeList) {
            Integer changeCount = (remAmt / change);
            changeMap.put(change+"c", changeCount);
            remAmt = remAmt - (changeCount*change);
        }
        return changeMap;
    }
    
    //private static

}

Initial URL
http://java.dzone.com/articles/thursday-code-puzzler-change

Initial Description
Determine the minimum number of coins for change
Given any number between 1 and 99, determine how to give change with the minimum number of coins. You can assume that the coins are 1c, 2c, 5c, 10c, 20c and 50c.

Initial Title
Determine the minimum number of coins for change

Initial Tags
java

Initial Language
Java