문제 설명
- 프로그램 시작: 1.주문요청 2.주문처리 3.매출액 증액 4.종료
- input1: Food(foodName, price), Order(Food, amount){자료구조 추가} 생성
- input2: 자료구조에 있는 주문처리(주문된 내용 출력)
- input3: 주문처리된 메뉴에 대한 합계 출력
코드
Food.java
package kosa.order;
public class Food {
private int idx;
private String foodName;
private int price;
private int saleCount;
public Food(){}
public Food(String foodName, int price, int idx) {
this.foodName = foodName;
this.price = price;
this.idx = idx;
}
public String getFoodName() {
return foodName;
}
public void setFoodName(String foodName) {
this.foodName = foodName;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public void setIdx(int idx){
this.idx = idx;
}
public int getIdx(){
return idx;
}
public void setSaleCount(int saleCount){
this.saleCount += saleCount;
}
public int getSaleCount(){
return saleCount;
}
@Override
public String toString() {
return idx + ". " +foodName + "-" + price + "원";
}
}
Order.java
package kosa.order;
public class Order {
private Food food;
private int amount;
private int orderPrice;
public Order(){}
public Order(Food food, int amount) {
this.food = food;
this.amount = amount;
orderPrice = food.getPrice()*amount;
}
public Food getFood() {
return food;
}
public void setFood(Food food) {
this.food = food;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public int getOrderPrice(){
return orderPrice;
}
@Override
public String toString() {
return "주문서 ----- 메뉴: " + food.getFoodName() + ", 가격: " + food.getPrice();
}
}
OrderService.java
package kosa.order;
import java.util.*;
public class OrderService {
private List<Food> foodList = new ArrayList<>();
private LinkedList<Order> orderList = new LinkedList<>();
private int sales;
public OrderService(){}
public void addFood(String foodName, int price, int idx){
foodList.add(new Food(foodName, price, idx));
}
public List<Food> foods(){
return foodList;
}
public Food findFood(int foodIdx){
return foodList.get(foodIdx-1);
}
public void addOrder(int foodIdx, int amount){
Food findFood = findFood(foodIdx);
Order order = new Order(findFood, amount);
orderList.offer(order);
System.out.println("주문 - 메뉴: " + order.getFood().getFoodName() + ", " +
"수량: " + order.getAmount());
sales += order.getOrderPrice();
findFood.setSaleCount(amount);
}
public LinkedList<Order> orders(){
return orderList;
}
public void clearOrder(){
Iterator<Order> iter = orderList.iterator();
if((iter.hasNext())) {
System.out.println("주문한 " + orderList.getFirst().getFood().getFoodName() + " " +
orderList.getFirst().getAmount() +"개 나왔습니다!");
orderList.poll();
}
else System.out.println("현재 주문이 없습니다.");
}
public void getSales(){
System.out.println("========== 매출전표 ==========");
for (Food food : foodList) {
System.out.println("- 메뉴명: " + food.getFoodName() + ", 수량: " +
food.getSaleCount() + "개, 합계: " + food.getPrice()*food.getSaleCount() +"원");
}
System.out.println("- 총 매출: " + sales +"원");
}
}
Main.java
package kosa.order;
import java.util.List;
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
OrderService service = new OrderService();
service.addFood("눈꽃탕수육", 17000, 1);
service.addFood("탄탄면", 9500, 2);
service.addFood("차돌짬뽕", 11000, 3);
service.addFood("짜장면", 6500, 4);
while(true){
System.out.println("----------------------------------------------------------------");
System.out.print("1.주문요청 2.주문처리 3.매출총액 4.종료 \n번호를 입력하십시오: ");
int menu = sc.nextInt();
if((menu > 4 || menu < 0)){
System.out.println("-----> !!올바른 입력이 아닙니다.");
continue;
}
System.out.println();
switch (menu){
case 1:
System.out.println("[----- 메뉴판 -----]");
List<Food> foods = service.foods();
for (Food food : foods) System.out.println(food);
System.out.println();
System.out.print("-----> 주문할 메뉴 번호를 입력하십시오: ");
int foodIdx = sc.nextInt();
if(foodIdx > foods.size() || foodIdx < 0){
System.out.println("-----> !! 올바른 입력이 아닙니다.");
break;
}
System.out.print("-----> 수량을 입력하십시오: ");
int amount = sc.nextInt();
service.addOrder(foodIdx, amount);
break;
case 2:
service.clearOrder();
break;
case 3:
service.getSales();
break;
case 4:
return;
}
}
}
}
결과