-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoinChange.java
62 lines (50 loc) · 1.48 KB
/
CoinChange.java
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
import java.util.Scanner;
/**
* Program Change - Java
* @date 12 August 2014
* @author Nefari0uss
*
* This program will request the amount of money to return and will provide the minimum amount of coinage possible.
* Assumes user enters a positive int.
*
**/
public class CoinChange {
public CoinChange() {}
public static void main(String[] args) {
int n = getInput();
calculateChange(n);
}
private static int getInput() {
int n;
Scanner input = new Scanner(System.in);
do {
System.out.println("How much change to return? \n Please enter total in cents: ");
String num = input.nextLine();
// Handle input greater than Java's MAX_INT.
try {
n = Integer.parseInt(num);
if (n < 0) {
System.out.println("Negative change isn't a thing...Enter a positive number: ");
}
}
catch (NumberFormatException e) {
System.out.println("n is too lage. Setting n to be the largest possible int value.");
n = Integer.MAX_VALUE;
//System.out.println(n);
}
} while (n < 0);
input.close();
return n;
}
private static void calculateChange (double n) {
String denominations[] = { "dollars", "quarters", "dimes", "nickels", "pennies" };
int values[] = { 100, 25, 10, 5, 1 };
System.out.println("\nThe total change is...");
for (int i = 0; i < values.length; i++) {
int denomValue = values[i];
values[i] = (int) (n / denomValue);
n -= denomValue * values[i];
System.out.println(values[i] + " " + denominations[i]);
}
}
}