-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathChange.java
More file actions
59 lines (53 loc) · 1.75 KB
/
Copy pathChange.java
File metadata and controls
59 lines (53 loc) · 1.75 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
package vendingmachine.domain;
import vendingmachine.enums.Coin;
import java.util.ArrayList;
import java.util.List;
public class Change {
private final int amount;
private final int[] coins;
public Change(int amount) {
this.amount = amount;
coins = makeCoins(amount);
}
private int[] makeCoins(int amount) {
int[] coins = new int[4];
while (amount != 0) {
int coin = getUnderAmountCoin(amount);
int coinIndex = Coin.getIndex(coin);
coins[coinIndex]++;
amount -= coin;
}
return coins;
}
private int getUnderAmountCoin(int amount){
int coin = Coin.getRandomCoin();
while( amount < coin){
coin = Coin.getRandomCoin();
}
return coin;
}
@Override
public String toString() {
StringBuilder print = new StringBuilder();
print.append("500원 - "+ coins[0]+"개\n");
print.append("100원 - "+ coins[1]+"개\n");
print.append("50원 - "+ coins[2]+"개\n");
print.append("10원 - "+ coins[3]+"개\n");
return print.toString();
}
public String lastChangePrint(){
StringBuilder print = new StringBuilder();
for(int index = 0; index < coins.length; index++){
if(coins[index]!=0){
appendPrint(print, index);
}
}
return print.toString();
}
public void appendPrint(StringBuilder print, int index){
if(index == 0)print.append("500원 - "+coins[index]+"개\n");
if(index == 1)print.append("100원 - "+coins[index]+"개\n");
if(index == 2)print.append("50원 - "+coins[index]+"개\n");
if(index == 3)print.append("10원 - "+coins[index]+"개\n");
}
}