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

[AutoVending] ADD | 자판기 기본 구현 #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
96 changes: 96 additions & 0 deletions KIMJIHYE/AutoVending/src/main/java/AdminMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class AdminMenu implements Menu {
Scanner scanner = new Scanner(System.in);
private Machine machine;
private List<Item> items;
private Map<Item, Integer> stocks;
private Report report;

public AdminMenu(Machine machine) {
this.machine = machine;
items = machine.getItems();
stocks = machine.getStocks();
report = machine.getReport();
}

public void showItems() {
System.out.println("번호 상품명 제조사 용량 가격 재고");
for (Item item : items) {
System.out.print(item);
System.out.println(stocks.get(item) + "개");
}
}

public void showMenus() {
System.out.println("========= 관리자 메뉴 =======");
System.out.println("1. 재고 채우기");
System.out.println("2. 사용자의 구매 이력 보기");
System.out.print("메뉴 번호를 입력하세요 :) ");
}

public void doProcess(int menuNum) {
switch (menuNum) {
case 1:
showItems();
fillItems();
break;
case 2:
report.print();
break;
default:
System.out.println("잘못된 입력..");
break;

}
}

public void raisePrice(int itemId, int amount) {
Item item = machine.getItem(itemId);
item.price += amount;
}

public void reducePrice(int itemId, int amount) {
Item item = machine.getItem(itemId);
item.price -= amount;
}

public void fillItems() {
int capacity = machine.getCapacity();
Map<Item, Integer> needToBuy = new HashMap<>();

for (Item item : stocks.keySet()) {
if (stocks.get(item) < capacity) {
int amount = capacity - stocks.get(item);
//report.itemIn(item,amount);
needToBuy.put(item, amount);
}
}

String list = "";
for (Item item : needToBuy.keySet()) {
list += item.name + " " + needToBuy.get(item) + "개, ";
}
list = list.substring(0, list.length() - 2);

System.out.println("재고를 꽉 채우려면");
System.out.println(list + "가 필요합니다.");
System.out.print("주문하시겠습니까? (Y/N) ");
switch (scanner.next()) {
case "Y":
case "y":
for (Item item : needToBuy.keySet()) {
machine.fill(item, needToBuy.get(item));
}
break;
case "N":
case "n":
System.out.println("관리자 메뉴를 종료합니다. :)");
System.out.println("=============================");
break;
}
}
}
22 changes: 22 additions & 0 deletions KIMJIHYE/AutoVending/src/main/java/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Item {
int itemId;
String brand;
int weight;
int price;
String name;
long date;

public Item(int itemId, String brand, int weight, int price, String name) {
this.itemId = itemId;
this.brand = brand;
this.weight = weight;
this.price = price;
this.name = name;
this.date = System.currentTimeMillis();
}

@Override
public String toString() {
return itemId + ". " + name + " " + brand + " " + weight + "ml " + price + "원 ";
}
}
92 changes: 92 additions & 0 deletions KIMJIHYE/AutoVending/src/main/java/Machine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Machine {
private Report report;
private List<Item> items;
private Map<Item, Integer> stocks;
private int capacity = 10;
private int money;

public Machine() {
items = new ArrayList<>();
items.add(new Item(1, "해태", 240, 1000, "코코팜"));
items.add(new Item(2, "동아오츠카", 240, 900, "데자와"));
items.add(new Item(3, "롯데", 150, 500, "레쓰비"));
items.add(new Item(4, "롯데", 210, 900, "칠성사이다"));
items.add(new Item(5, "델몬트", 240, 1100, "오렌지에이드"));

report = new Report();

stocks = new HashMap<Item, Integer>();
for (Item item : items) {
stocks.put(item, capacity);
}
}

public void setMoney(int money) {
this.money = money;
}

public int getMoney() {
return money;
}

public int getCapacity() {
return capacity;
}

public void setCapacity(int capacity) {
this.capacity = capacity;
}

public Item getItem(int itemId) {
Item it = null;
for (Item item : items) {
if (item.itemId == itemId) {
it = item;
}
}

if (it == null) {
System.out.println("찾으시는 상품이 없습니다.");
return null;
} else {
return it;
}
}

public void fill(Item item, int amount) {
int remains = stocks.get(item);
stocks.put(item, remains + amount);
}

public void buy(int itemId) {
for (Item item : items) {
if (item.itemId == itemId) {
Item it = item;
int remains = stocks.get(it);
if (remains == 0) {
System.out.println("Sold Out!!");
} else {
stocks.put(it, remains - 1);
money -= it.price;
}
}
}
}

public List<Item> getItems() {
return items;
}

public Map<Item, Integer> getStocks() {
return stocks;
}

public Report getReport() {
return report;
}
}
34 changes: 34 additions & 0 deletions KIMJIHYE/AutoVending/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Machine machine = new Machine();
Menu menu = null;

System.out.println(" 자팡기자판기 ");
while (true) {
System.out.println("1. 상품 구매(사용자 메뉴)\n2. 상품 관리(관리자 메뉴)");
System.out.print("메뉴 번호를 입력하세요 :) ");
switch (scanner.nextInt()) {
case 1:
menu = new UserMenu(machine);
menu.showItems();
menu.doProcess();
break;
case 2:
menu = new AdminMenu(machine);
menu.showItems();
menu.showMenus();
menu.doProcess(scanner.nextInt());
break;
}


}


}

}
12 changes: 12 additions & 0 deletions KIMJIHYE/AutoVending/src/main/java/Menu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public interface Menu {
void showItems();
default void doProcess() {
}
default void doProcess(int menuNum) {
}
default void showMenus() {
}
default boolean showCanBuy(int money) {
return true;
}
}
27 changes: 27 additions & 0 deletions KIMJIHYE/AutoVending/src/main/java/Report.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class Report {
List<String> reports;

public Report() {
this.reports = new ArrayList<String>();
}

public void write(String item) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String log = (reports.size() + 1) + ". " + item + " 1개 " + df.format(System.currentTimeMillis());
reports.add(log);
}

public void print() {
System.out.println("====== 사용자의 구매 이력 =======");
for (String report : reports) {
System.out.println(report);
}
System.out.println("=================================");
}

}
88 changes: 88 additions & 0 deletions KIMJIHYE/AutoVending/src/main/java/UserMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import java.util.List;
import java.util.Scanner;

public class UserMenu implements Menu {
Scanner scanner = new Scanner(System.in);
private Machine machine;
private Report report;
List<Item> items;
int minPrice;

public UserMenu(Machine machine) {
this.machine = machine;
items = machine.getItems();
report = machine.getReport();

minPrice = items.get(0).price;
for (Item item : items) {
if (item.price < minPrice) {
minPrice = item.price;
}
}

System.out.println("=========== 상품 구매 ==========");
}

public void doProcess() {
boolean isEnd = false;
while (true) {
System.out.print("돈을 넣으세요 :) ");
int money = scanner.nextInt();
machine.setMoney(machine.getMoney() + money);
if (showCanBuy(machine.getMoney())) {
System.out.print("원하는 상품을 선택하세요 :) ");
int itemId = scanner.nextInt();
String item = machine.getItem(itemId).name;
machine.buy(itemId);
System.out.println(item + "을 구입했습니다.");
report.write(item);
System.out.println("더 구매하시겠습니까? (Y/N) ");
switch (scanner.next()) {
case "Y":
case "y":
break;
case "N":
case "n":
System.out.println("거스름돈 " + machine.getMoney() + "원을 가져가세요 :) ");
machine.setMoney(0);
isEnd = true;
break;
default:
System.out.println("잘못된 입력.. 처음으로 이동합니다.");
}
} else {
continue;
}
System.out.println("=======================================");
if (isEnd) {
break;
}
}

}

public void showItems() {
System.out.println("번호 상품명 제조사 용량 가격");
for (Item item : items) {
System.out.println(item);
}
}

public boolean showCanBuy(int money) {
if (money < minPrice) {
System.out.println("돈이 너무 적습니다.. 더 넣어주세요.. ");
return false;
} else {
System.out.println(money + "원으로 살 수 있는 음료");
System.out.println("=> ");

for (Item item : items) {
if (item.price <= money) {
System.out.println(item.itemId + ". " + item.name + " " + item.price + "원");
}
}
return true;
}
}

}