fork download
  1. /**
  2.  * Cash Register
  3.  *
  4.  * Description
  5.  * The goal of this challenge is to design a cash register program. You will be
  6.  * given two decimal numbers. The first is the purchase price (PP) of the item.
  7.  * The second is the cash (CH) given by the customer. Your register currently has
  8.  * the following bills/coins within it:
  9.  * <table>
  10.  * <tr><td>PENNY</td><td>.01</td></tr>
  11.  * <tr><td>NICKEL</td><td>.05</td></tr>
  12.  * <tr><td>DIME</td><td>.10</td></tr>
  13.  * <tr><td>QUARTER</td><td>.25</td></tr>
  14.  * <tr><td>HALF DOLLAR</td><td>.50</td></tr>
  15.  * <tr><td>ONE</td><td>1.00</td></tr>
  16.  * <tr><td>TWO</td><td>2.00</td></tr>
  17.  * <tr><td>FIVE</td><td>5.00</td></tr>
  18.  * <tr><td>TEN</td><td>10.00</td></tr>
  19.  * <tr><td>TWENTY</td><td>20.00</td></tr>
  20.  * <tr><td>FIFTY</td><td>50.00</td></tr>
  21.  * <tr><td>ONE HUNDRED</td><td>100.00</td></tr>
  22.  * </table>
  23.  * The aim of the program is to calculate the change that has to be returned to
  24.  * the customer.
  25.  *
  26.  * Input
  27.  * Your program should read lines of text from standard input. Each line contains
  28.  * two numbers which are separated by a semicolon. The first is the Purchase price (PP)
  29.  * and the second is the cash(CH) given by the customer.
  30.  *
  31.  * Output
  32.  * For each line of input print a single line to standard output which is the change
  33.  * to be returned to the customer. In case the CH < PP, print out ERROR. If CH == PP,
  34.  * print out ZERO. For all other cases print the amount that needs to be returned,
  35.  * in terms of the currency values provided. The output should be alphabetically
  36.  * sorted.
  37.  *
  38.  * Example
  39.  * 15.94;16.00 => NICKEL,PENNY
  40.  * 17;16 => ERROR
  41.  * 35;35 => ZERO
  42.  * 45;50 => FIVE
  43.  */
  44. import java.io.BufferedReader;
  45. import java.io.IOException;
  46. import java.io.InputStreamReader;
  47. import java.util.Set;
  48. import java.util.SortedMap;
  49. import java.util.stream.Collectors;
  50. import java.util.stream.Stream;
  51. import java.nio.charset.StandardCharsets;
  52.  
  53. public class Main {
  54.  
  55. private static final SortedMap<Double, String> coins;
  56.  
  57. static {
  58. coins = new java.util.TreeMap<Double, String>( (d1,d2) -> (d1>d2)? -1 : (d1<d2)?1:0 );
  59. coins.put(0.01d,"PENNY");
  60. coins.put(0.05d,"NICKEL");
  61. coins.put(0.10d,"DIME");
  62. coins.put(0.25d,"QUARTER");
  63. coins.put(0.50d,"HALF DOLLAR");
  64. coins.put(1.00d,"ONE");
  65. coins.put(2.00d,"TWO");
  66. coins.put(5.00d,"FIVE");
  67. coins.put(10.00d,"TEN");
  68. coins.put(20.00d,"TWENTY");
  69. coins.put(50.00d,"FIFTY");
  70. coins.put(100.00d,"ONE HUNDRED");
  71. }
  72. /**
  73.   * Iterate through each line of input.
  74.   */
  75. public static void main(String[] args) throws IOException {
  76. InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
  77. BufferedReader in = new BufferedReader(reader);
  78. String line;
  79. while ((line = in.readLine()) != null) {
  80. double[] inputs = Stream.of(line.split(";")).mapToDouble(Double::valueOf).toArray();
  81.  
  82. if (inputs[1] < inputs[0]) {
  83. System.out.println("ERROR");
  84. continue;
  85. } else if (inputs[1] == inputs[0]) {
  86. System.out.println("ZERO");
  87. continue;
  88. }
  89.  
  90. Set<String> changes = new java.util.HashSet<>();
  91. double remains = inputs[1] - inputs[0];
  92. for (Double coin : coins.keySet()) {
  93. double newRemains = remains % coin;
  94. if (newRemains != remains) {
  95. remains = newRemains;
  96. changes.add(coins.get(coin));
  97. }
  98. }
  99. System.out.println(changes.stream().sorted().collect(Collectors.joining(",")));
  100. }
  101. }
  102. }
Success #stdin #stdout 0.1s 34884KB
stdin
15.94;16.00
17;16
35;35
45;50
stdout
NICKEL,PENNY
ERROR
ZERO
FIVE