-
Notifications
You must be signed in to change notification settings - Fork 341
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
자판기 미션 리뷰 부탁드립니다!! #193
base: main
Are you sure you want to change the base?
자판기 미션 리뷰 부탁드립니다!! #193
Changes from all commits
b3834d0
d6032eb
58ddf68
4b44d91
d30e905
f23db6c
3e26a3d
e2e0cb8
7e5c14b
9fa0c91
70d42b8
e9ab128
d477652
9c838df
0465174
a09f743
60ffbb1
027e16b
2e708f5
bb6a233
89b32e2
565f565
81e10ce
02e8e9d
02adde9
bc44def
cb8df9b
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 |
---|---|---|
@@ -1,7 +1,21 @@ | ||
package vendingmachine; | ||
|
||
import vendingmachine.controller.CoinController; | ||
import vendingmachine.controller.MoneyController; | ||
import vendingmachine.controller.OrderController; | ||
import vendingmachine.controller.ProductController; | ||
import vendingmachine.model.Products; | ||
|
||
public class Application { | ||
public static void main(String[] args) { | ||
// TODO: 프로그램 구현 | ||
CoinController coinController = new CoinController(); | ||
ProductController productController = new ProductController(); | ||
MoneyController moneyController = new MoneyController(); | ||
OrderController orderController = new OrderController(); | ||
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. Service나 Domain 계층에 책임을 위임하지 않고, Controller에 구현하신 우진님만의 의도가 궁금합니다!
Comment on lines
+11
to
+14
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. 컨트롤러가 많네요.. |
||
|
||
int[] coins = coinController.initMoney(); | ||
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. Collection이 아닌 배열을 사용하신 이유가 궁금합니다! |
||
Products products = productController.initStock(); | ||
int money = moneyController.receiveMoney(); | ||
orderController.makeOrder(products, money); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package vendingmachine.controller; | ||
|
||
import static camp.nextstep.edu.missionutils.Randoms.pickNumberInList; | ||
import static vendingmachine.view.InputView.inputInteger; | ||
import static vendingmachine.view.OutputView.printCoins; | ||
import static vendingmachine.view.OutputView.printHavingMoneyMessage; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import vendingmachine.model.Coin; | ||
|
||
public class CoinController { | ||
|
||
private static final int size = Coin.values().length; | ||
private static int[] coins = new int[size]; | ||
|
||
public int[] initMoney() { | ||
printHavingMoneyMessage(); | ||
return initCoins(inputInteger()); | ||
} | ||
|
||
private int[] initCoins(int total) { | ||
Coin[] coinType = Coin.values(); | ||
int index = 0; | ||
while (total > 0) { | ||
//동전 합이 total이 될 때까지 loop | ||
//0부터 size까지를 왔다갔다할 인덱스 | ||
int coinPrice = coinType[index].getAmount(); | ||
//현재 index에 해당하는 동전의 액면가 | ||
int possibleCoin = total / coinPrice; | ||
//현재 index의 동전의 최대 개수 | ||
List<Integer> numberRange = makeNumberRange(possibleCoin); | ||
//pickNumberInList의 매개변수 타입에 맞게 0부터 possibleCoin까지의 모든 수를 넣은 List | ||
if (possibleCoin > 0) { | ||
//가능한 코인 개수가 있으면 | ||
int pickedNumber = pickNumberInList(numberRange); | ||
//랜덤 돌려서 | ||
coins[index] += pickedNumber; | ||
//코인 개수에 추가 | ||
total -= coinPrice * pickedNumber; | ||
//while문 조건에 반영하기 위해 동전개수 * 액면가를 total에서 제해줌 | ||
Comment on lines
+26
to
+41
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. 주석은 설계 과정에서 스스로 쉽게 알아보기 위해 작성한 건가요?! |
||
} | ||
index++; | ||
if (index == size) { | ||
//모든 종류의 코인 다 돌았으면 | ||
index = 0; | ||
//Index 초기화 | ||
} | ||
} | ||
//보유 금액만큼 동전 생성 후 출력 | ||
printCoins(coins); | ||
return coins; | ||
} | ||
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. 주석을 남겨두신 이유가 궁금해요!🧐 |
||
|
||
private static List<Integer> makeNumberRange(int possibleCoin) { | ||
List<Integer> numberRange = new ArrayList<>(); | ||
for (int i = 0; i <= possibleCoin; i++) { | ||
numberRange.add(i); | ||
} | ||
return numberRange; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package vendingmachine.controller; | ||
|
||
import static vendingmachine.view.InputView.inputInteger; | ||
import static vendingmachine.view.OutputView.printPurchasingMoneyMessage; | ||
|
||
public class MoneyController { | ||
|
||
public int receiveMoney() { | ||
printPurchasingMoneyMessage(); | ||
return inputInteger(); | ||
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. Console과 같은 라이브러리뿐만 아니라, InputView와 같은 클래스까지 모두 static import 하신 의도가 궁금합니다! |
||
} | ||
} | ||
Comment on lines
+6
to
+12
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. 컨트롤러 분리가 조금 과한 감이 있는 것 같아요. 코드가 많이 길어지면 그 때 가서 분리해도 좋을 것 같습니다! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package vendingmachine.controller; | ||
|
||
import static vendingmachine.controller.ProductController.minPrice; | ||
import static vendingmachine.controller.ProductController.validateEmptyStock; | ||
import static vendingmachine.model.Products.getProductByName; | ||
import static vendingmachine.view.InputView.input; | ||
import static vendingmachine.view.OutputView.printPurchasingItemMessage; | ||
import static vendingmachine.view.OutputView.printPurchasingMoney; | ||
|
||
import vendingmachine.model.Product; | ||
import vendingmachine.model.Products; | ||
|
||
public class OrderController { | ||
|
||
public void makeOrder(Products products, int purchasingMoney) { | ||
while (validateEmptyStock(products) && purchasingMoney > minPrice(products)) { | ||
printPurchasingMoney(purchasingMoney); | ||
//투입금액(남은금액) 출력 | ||
printPurchasingItemMessage(); | ||
String purchasingItemName = input(); | ||
Product purchasingItem = getProductByName(purchasingItemName); | ||
purchasingMoney -= purchasingItem.getPrice(); | ||
} | ||
giveChange(); | ||
} | ||
|
||
public void giveChange() { | ||
|
||
} | ||
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. 이 부분은 아직 작성되지 않은건가요?! |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package vendingmachine.controller; | ||
|
||
import static vendingmachine.view.InputView.input; | ||
import static vendingmachine.view.OutputView.printOrderMessage; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.StringTokenizer; | ||
import vendingmachine.model.Product; | ||
import vendingmachine.model.Products; | ||
|
||
public class ProductController { | ||
|
||
public Products initStock() { | ||
printOrderMessage(); | ||
return makeProductList(); | ||
} | ||
|
||
private Products makeProductList() { | ||
String stockInput = input(); | ||
StringTokenizer semicolonSt = new StringTokenizer(stockInput, ";"); | ||
List<Product> products = new ArrayList<>(); | ||
return processProductInput(semicolonSt, products); | ||
} | ||
|
||
private Products processProductInput(StringTokenizer semicolonSt, List<Product> productList) { | ||
while (semicolonSt.hasMoreTokens()) { | ||
String processingInput = semicolonSt.nextToken(); | ||
processingInput = processingInput.replace("[", "").replace("]", ""); | ||
String name = processingInput.split(",")[0]; | ||
int price = Integer.parseInt(processingInput.split(",")[1]); | ||
int stock = Integer.parseInt(processingInput.split(",")[2]); | ||
productList.add(new Product(name, price, stock)); | ||
} | ||
return new Products(productList); | ||
} | ||
|
||
public static boolean validateEmptyStock(Products products) { | ||
//재고가 하나라도 있으면 진행 가능 | ||
return products.getProducts().stream().anyMatch(product -> product.getStock() > 0); | ||
} | ||
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. 재고 확인 검증을 Domain이 아닌 Controller에서 하신 이유가 궁금합니다! |
||
|
||
public static int minPrice(Products products) { | ||
return products.getProducts().stream().mapToInt(Product::getPrice).min() | ||
.orElse(Integer.MAX_VALUE); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package vendingmachine.model; | ||
|
||
public class Product { | ||
|
||
private String name; | ||
private int price; | ||
private int stock; | ||
|
||
public Product(String name, int price, int stock) { | ||
this.name = name; | ||
this.price = price; | ||
this.stock = stock; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getPrice() { | ||
return price; | ||
} | ||
|
||
public int getStock() { | ||
return stock; | ||
} | ||
|
||
public void reduceStock() { | ||
this.stock -= 1; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package vendingmachine.model; | ||
|
||
import java.util.List; | ||
|
||
public class Products { | ||
|
||
private static List<Product> products; | ||
|
||
public Products(List<Product> productList) { | ||
this.products = productList; | ||
} | ||
|
||
public List<Product> getProducts() { | ||
return products; | ||
} | ||
|
||
public static Product getProductByName(String name) { | ||
return products.stream().filter(product -> product.getName().equals(name)).findFirst() | ||
.orElse(null); | ||
} | ||
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. 상품 이름으로 Product를 반환하는 메서드라면, findAny()를 사용하는게 더 좋지 않을까요?? 우진님의 의견은 어떠신지 궁금해요! |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package vendingmachine.view; | ||
|
||
import static camp.nextstep.edu.missionutils.Console.readLine; | ||
|
||
public class InputView { | ||
|
||
public static int inputInteger() { | ||
return Integer.parseInt(input()); | ||
} | ||
|
||
public static String input() { | ||
return readLine(); | ||
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. 항상 Console.readLint()을 사용해왔는데, 해당 라인처럼 더 간결하게 표현할 수 있어서 좋은 것 같아요!👍 |
||
} | ||
} |
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.
다음번에는 시간을 재고 해보는 것도 좋을 것 같아요!