Skip to content
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

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c25d751
docs: 기능목록 작성
JaegeonYu Dec 3, 2022
0c6a33f
feat: create askPricePrint method
JaegeonYu Dec 3, 2022
26cca6e
feat: create askProductPrint method
JaegeonYu Dec 3, 2022
0f93c2b
feat: create askInputPricePrint method
JaegeonYu Dec 3, 2022
0985e8d
feat: create askBuyProductPrint method
JaegeonYu Dec 3, 2022
fc448b2
docs: 기능목록 수정
JaegeonYu Dec 3, 2022
76aaa14
feat: create isDivideMinCoin method
JaegeonYu Dec 3, 2022
d4a0cc1
feat: create inputChange method
JaegeonYu Dec 3, 2022
108a648
feat: create Product Class
JaegeonYu Dec 3, 2022
d12f080
feat: create inputProducts method
JaegeonYu Dec 3, 2022
7becaec
docs: 기능목록 수정
JaegeonYu Dec 3, 2022
5c1e064
feat: create Change class and create Random Change Coins
JaegeonYu Dec 3, 2022
a156fcd
refactor: coin 자료구조 map 에서 list로 변경
JaegeonYu Dec 3, 2022
5f3479b
feat: create Change toString print
JaegeonYu Dec 3, 2022
f275785
docs: 기능목록 수정
JaegeonYu Dec 3, 2022
ca2f3de
feat: create printFirstChange method
JaegeonYu Dec 3, 2022
a073b95
docs: 기능목록 작성
JaegeonYu Dec 3, 2022
c53db3a
feat: create Products class
JaegeonYu Dec 3, 2022
9a31ac5
feat: create VendingMachine class
JaegeonYu Dec 3, 2022
ee2ad91
feat: create change, product, amount input method
JaegeonYu Dec 3, 2022
398e265
docs: 기능목록 수정
JaegeonYu Dec 3, 2022
4028e7e
feat: create VendingMachine Service Class
JaegeonYu Dec 3, 2022
87a25f6
refactor: extract lastChangePrint to appendPrint
JaegeonYu Dec 3, 2022
b253476
refactor: extract while method
JaegeonYu Dec 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ java {
test {
useJUnitPlatform()
}

48 changes: 48 additions & 0 deletions docs/README.md
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원보다 크거나 같은지 확인
- [ ] 상품명, 가격, 수량 입력시 ";"로 나뉘는지 확인
- [ ] 구매할 상품이 존재하는 상품인지 확인
4 changes: 3 additions & 1 deletion src/main/java/vendingmachine/Application.java
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();
}
}
16 changes: 0 additions & 16 deletions src/main/java/vendingmachine/Coin.java

This file was deleted.

59 changes: 59 additions & 0 deletions src/main/java/vendingmachine/domain/Change.java
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;
Copy link
Member

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 처럼 구성할 수 있을 것 같아요


public Change(int amount) {
this.amount = amount;
coins = makeCoins(amount);
}

private int[] makeCoins(int amount) {
int[] coins = new int[4];
Copy link
Member

@Choi-JJunho Choi-JJunho Dec 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enum으로 관리하면 좋을것 같아요
만약에 1000원단위가 생기면 손대야할 부분이 너무 많아질것 같아요

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");
}
}
27 changes: 27 additions & 0 deletions src/main/java/vendingmachine/domain/Product.java
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){
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
33 changes: 33 additions & 0 deletions src/main/java/vendingmachine/domain/Products.java
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){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드 포맷팅 해주시면 좋을거같아요

buyPrice = buy;
}
}
if(buyPrice == 0)throw new IllegalArgumentException("[ERROR] 해당 제품이 존재하지 않습니다.");

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

products.get(index).isExistProduct() 부분에서 products.get(index) 부분을 한번 변수로 분리해주는것이 좋아보여요.

}
Comment on lines +28 to +30
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for (Product product : products) return product.isExistProduct()

이런식으로 정리하면 깔끔할꺼 같아요!

return true;
}
}
36 changes: 36 additions & 0 deletions src/main/java/vendingmachine/domain/VendingMachine.java
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getter를 통해 상태를 가져오고 처리하는 방식은 어떨까요?

}


@Override
public String toString() {
Copy link
Member

@Choi-JJunho Choi-JJunho Dec 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lastPrint()의 내용이 VendingMachine.toString()에 더 적합해보입니다.

아래 구문은 getAmountPrice()정도로 사용할 수 있을것 같네요

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();
}
}
42 changes: 42 additions & 0 deletions src/main/java/vendingmachine/enums/Coin.java
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<>();
Copy link
Member

Choose a reason for hiding this comment

The 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){
Copy link
Member

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try catch가 없어서 NumberFormat Exception에 대처하기 어려워보이네요
해당 메소드가 숫자를 검증하는 역할까지 가진다는 것에서 책임이 너무 많아지는것 같아요

차라리 파라미터로 primitive type (int)으로 받고 처리하는건 어떨까요?

return false;
}
}
25 changes: 25 additions & 0 deletions src/main/java/vendingmachine/service/VendingMachineService.java
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());
}
}
Loading