-
Notifications
You must be signed in to change notification settings - Fork 340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[자판기] 유재건 미션 제출합니다. #163
base: main
Are you sure you want to change the base?
[자판기] 유재건 미션 제출합니다. #163
Changes from all commits
c25d751
0c6a33f
26cca6e
0f93c2b
0985e8d
fc448b2
76aaa14
d4a0cc1
108a648
d12f080
7becaec
5c1e064
a156fcd
5f3479b
f275785
ca2f3de
a073b95
c53db3a
9a31ac5
ee2ad91
398e265
4028e7e
87a25f6
b253476
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ java { | |
test { | ||
useJUnitPlatform() | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
## 🚀 기능 요구사항 | ||
|
||
### View | ||
- [x] 자판기 보유 금액 묻는 print | ||
- [ ] 자판기 보유한 동전 print | ||
- [x] 상품명, 가격, 수량 묻는 print | ||
- [x] 투임 금액 묻는 print | ||
- [x] 구매할 상품 묻는 print | ||
- [ ] 마지막 잔돈 print | ||
- [x] 자판기 보유 금액 input | ||
- [x] 상품명, 가격, 수량 input | ||
- [ ] 투입 금액 input | ||
|
||
### VendingMachine | ||
- [ ] 자판기 보유 금액, 상품 관리 | ||
- [ ] 자판기 보유 금액 toString print 관리 | ||
- [ ] 자판기 보유 잔돈 저장 | ||
- [ ] 투입 금액 저장 | ||
- [ ] 투입 금액 변동 시 금액 수정 | ||
|
||
### Coin | ||
- [ ] 자판기 보유 잔돈 최수 개수로 생성 | ||
- [ ] 잔돈이 최저 코인으로 나눠지는지 확인 | ||
|
||
### Change | ||
- [x] 보유 금액을 받아 잔돈 무작위 생성 및 저장 | ||
- [x] 보유 잔돈 toString | ||
|
||
|
||
### Product | ||
- [x] 상품명, 가격, 수량 저장 | ||
- [ ] 구매시 수량 변경 | ||
- [ ] 수량이 0이면 메시지 | ||
|
||
|
||
### Products | ||
- [ ] Product 들과 최저 가격 저장 | ||
- [ ] 상품 구매 시 상품이름에 맞는 가격 리턴, 없으면 예외처리 | ||
- [ ] 상품 수량이 없거나 최저 가격보다 투입금액이 작으면 메시지 | ||
|
||
|
||
### Exception | ||
예외 발생시 재입력 받기 | ||
- [ ] 금액 입력 시 숫자 인지 확인 | ||
- [ ] 잔돈, 상품 가격 저장 시 10원으로 떨어지는 지 확인 | ||
- [ ] 상품 가격 저장 시 100원보다 크거나 같은지 확인 | ||
- [ ] 상품명, 가격, 수량 입력시 ";"로 나뉘는지 확인 | ||
- [ ] 구매할 상품이 존재하는 상품인지 확인 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package vendingmachine; | ||
|
||
import vendingmachine.service.VendingMachineService; | ||
|
||
public class Application { | ||
public static void main(String[] args) { | ||
// TODO: 프로그램 구현 | ||
new VendingMachineService().play(); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enum으로 관리하면 좋을것 같아요 |
||
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"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package vendingmachine.domain; | ||
|
||
public class Product { | ||
private final String name; | ||
private final int price; | ||
private final int RUN_OUT = -1; | ||
private int amount; | ||
|
||
|
||
public Product(String name, int price, int amount) { | ||
this.name = name; | ||
this.price = price; | ||
this.amount = amount; | ||
} | ||
|
||
public int buyProduct(String name){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. buyProduct 함수가 isExistProduct의 역할을 같이 수행하는것 같네요 아래에서 isExistProduct로 상품잔여개수에 대한 판별을 하는데 굳이 RUN_OUT을 추가로 반환해줄 필요가 해당 부분에서는 개수가 부족할때 예외를 던지는 방식으로 시도해보는것을 어떨까요 |
||
if(this.name.equals(name)){ | ||
amount--; | ||
return price; | ||
} | ||
return RUN_OUT; | ||
} | ||
public boolean isExistProduct(){ | ||
if(amount == 0)return false; | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package vendingmachine.domain; | ||
|
||
import java.util.List; | ||
|
||
public class Products { | ||
private final List<Product> products; | ||
private final int minAmount; | ||
private final static int RUN_OUT = -1; | ||
|
||
public Products(List<Product> products, int minAmount) { | ||
this.products = products; | ||
this.minAmount = minAmount; | ||
} | ||
|
||
public int buy(String buyProduct) { | ||
int buyPrice = 0; | ||
for(int index = 0 ; index < products.size();index++){ | ||
int buy = products.get(index).buyProduct(buyProduct); | ||
if(buy!=RUN_OUT){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 코드 포맷팅 해주시면 좋을거같아요 |
||
buyPrice = buy; | ||
} | ||
} | ||
if(buyPrice == 0)throw new IllegalArgumentException("[ERROR] 해당 제품이 존재하지 않습니다."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 구매하는 역할과 예외처리하는 역할을 buy메서드가 동시에 하고 있는 것 같아요. 역할을 분리해보는게 어떨까용 |
||
return buyPrice; | ||
} | ||
public boolean checkContinue(int amount){ | ||
if(minAmount > amount) return false; | ||
for(int index = 0; index < products.size(); index++){ | ||
if(!products.get(index).isExistProduct())return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
Comment on lines
+28
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for (Product product : products) return product.isExistProduct() 이런식으로 정리하면 깔끔할꺼 같아요! |
||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package vendingmachine.domain; | ||
|
||
import java.util.List; | ||
|
||
public class VendingMachine { | ||
private final Products products; | ||
private int amountPrice; | ||
private final Change change; | ||
|
||
public VendingMachine(Products products, int amountPrice, Change change) { | ||
this.products = products; | ||
this.change = change; | ||
this.amountPrice = amountPrice; | ||
|
||
} | ||
|
||
public void buy(String buyProduct){ | ||
amountPrice -= products.buy(buyProduct); | ||
} | ||
public boolean isFinish(){ | ||
return products.checkContinue(amountPrice); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getter를 통해 상태를 가져오고 처리하는 방식은 어떨까요? |
||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
아래 구문은 |
||
return String.valueOf(amountPrice); | ||
} | ||
public String lastPrint(){ | ||
StringBuilder print = new StringBuilder(); | ||
print.append("투입금액:" +amountPrice+"원\n"); | ||
print.append("잔돈\n"); | ||
print.append(change.lastChangePrint()); | ||
return print.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package vendingmachine.enums; | ||
|
||
import camp.nextstep.edu.missionutils.Randoms; | ||
import vendingmachine.view.InputView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public enum Coin { | ||
COIN_500(500), | ||
COIN_100(100), | ||
COIN_50(50), | ||
COIN_10(10); | ||
|
||
private final int amount; | ||
|
||
Coin(final int amount) { | ||
this.amount = amount; | ||
} | ||
public static int getRandomCoin(){ | ||
List<Integer> coins = new ArrayList<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 호출 할때마다 List를 새로 생성하면 자원 낭비가 될것같네요 private final `List<Integer>` coins = List.of(
COIN_10.amount,
COIN_50.amount,
COIN_100.amount,
COIN_500.amount
) |
||
coins.add(COIN_10.amount); | ||
coins.add(COIN_50.amount); | ||
coins.add(COIN_100.amount); | ||
coins.add(COIN_500.amount); | ||
return Randoms.pickNumberInList(coins); | ||
} | ||
public static int getIndex(int coin){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. java의 Enum은 선언된 순서대로 index값을 가집니다. |
||
if(COIN_10.amount == coin)return 3; | ||
if(COIN_50.amount == coin)return 2; | ||
if(COIN_100.amount == coin)return 1; | ||
return 0; | ||
} | ||
|
||
// 추가 기능 구현 | ||
public static boolean isDivideMinCoin(String input) { | ||
if (Integer.parseInt(input) % COIN_10.amount == 0) return true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try catch가 없어서 NumberFormat Exception에 대처하기 어려워보이네요 차라리 파라미터로 primitive type (int)으로 받고 처리하는건 어떨까요? |
||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package vendingmachine.service; | ||
|
||
import vendingmachine.domain.Change; | ||
import vendingmachine.domain.Products; | ||
import vendingmachine.domain.VendingMachine; | ||
import vendingmachine.view.InputView; | ||
import vendingmachine.view.OutputView; | ||
|
||
public class VendingMachineService { | ||
private final InputView inputView = new InputView(); | ||
private final OutputView outputView = new OutputView(); | ||
public void play(){ | ||
|
||
int changeAmount = inputView.inputChange(); | ||
Change change = new Change(changeAmount); | ||
outputView.printFirstChange(change); | ||
Products products = inputView.inputProducts(); | ||
int inputAmount = inputView.inputAmount(); | ||
VendingMachine vendingMachine = new VendingMachine(products, inputAmount, change); | ||
while(vendingMachine.isFinish()){ | ||
inputView.inputBuyProduct(vendingMachine); | ||
} | ||
outputView.printLastChange(vendingMachine.lastPrint()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Map을 이용하면 더 좋을것 같습니다.
예를들면
Map<Coin, Integer> coins
처럼 구성할 수 있을 것 같아요