-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCashDispenser.java
More file actions
114 lines (100 loc) · 3.34 KB
/
CashDispenser.java
File metadata and controls
114 lines (100 loc) · 3.34 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.util.*;
public class CashDispenser {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
Cash cash = new Cash();
while(true){
cash.display();
int money= console.nextInt();
if(money>0){
new Cash(money);
cash.viewData();
cash.Reset();
}
else break;
}
}
}
class Cash{
private static int oneThousand = 5;
private static int fiveHundred = 5;
private static int twoHundred=5;
private static int oneHundred = 5;
private static int tenCoins = 5;
private static String outputDisplay= "";
private static int money = 0;
private static int counter1k = 0;
private static int counter5 = 0;
private static int counter2 = 0;
private static int counter1 = 0;
private static int counter10= 0;
private static boolean ind = false;
Cash(){
}
Cash(int money){
this.money = money;
computeMoney();
}
public void display(){
System.out.print("Cash Dispenser\nCash\tQuantity\n1000\t["+oneThousand+"]\n500\t["+fiveHundred+"]\n200\t["+twoHundred+"]\n100\t["+oneHundred+"]\n10\t["+tenCoins+"]\n\nInput: ");
}
public void computeMoney(){
while(money!=0){
if(money >= 1000 && oneThousand != 0 && money/1000 <= oneThousand){
money-= 1000;
oneThousand-= 1;
counter1k++;
}
else if(money>= 500 && fiveHundred != 0 && money/500 <= fiveHundred){
money-= 500;
fiveHundred-=1;
counter5++;
}
else if(money>= 200 && twoHundred != 0 && money/200<=twoHundred){
money-= 200;
twoHundred-=1;
counter2++;
}
else if(money>= 100 && oneHundred != 0 && money/100<=oneHundred){
money-= 100;
oneHundred-=1;
counter1++;
}
else if(money>= 10 && tenCoins != 0&& money/10<= tenCoins){
money-= 10;
tenCoins-=1;
counter10++;
}
else{
System.out.println("Not enough money to dispense");
ind = true;
break;
}
}
while(true){
if(ind == true){
break;
}
else if(counter1k>0){
outputDisplay+= "1000 *"+ counter1k+"= "+ (1000*counter1k)+ "\n";
counter1k = 0;
}
else if(counter5 > 0){
outputDisplay+= "500 *"+ counter5+"= "+ (500*counter5)+ "\n"; counter5=0;}
else if(counter2 > 0){
outputDisplay+= "200 *"+ counter2+"= "+ (200*counter2)+ "\n";counter2=0;}
else if(counter1 > 0){
outputDisplay+= "100 *"+ counter1+"= "+ (100*counter1)+ "\n";counter1= 0;}
else if(counter10 > 0){
outputDisplay+= "10 *"+ counter10+"= "+ (10*counter10)+ "\n";counter10= 0;}
else break;
}
}
public void viewData(){
System.out.println("Output: \n"+ outputDisplay);
}
public void Reset(){
outputDisplay= "";
ind= false;
}
}