매출 계산기 III
작성자 정보
- 작성자 최고관리자
- 작성일
컨텐츠 정보
- 조회 287
본문
import java.util.ArrayList;
import java.util.Random;
public class SalesAnalyzer {
// 주문 리스트
private ArrayList<Menu> orders;
public SalesAnalyzer() {
orders = new ArrayList<Menu>();
}
// 메인 테스트
public static void main(String[] args) {
SalesAnalyzer sales = new SalesAnalyzer();
// 랜덤 주문 생성.
sales.randomOrders();
// 매출 계산.
int totalSales = sales.calculateSales();
System.out.println("총 매출액: " + totalSales + "원");
}
// 랜덤 주문 생산기
public void randomOrders() {
Random random = new Random();
int n = 51 + random.nextInt(50);
for (int i = 0; i < n; i++) {
orders.add(randomOrder());
}
}
private Menu randomOrder() {
Menu menu = null;
Random random = new Random();
switch (random.nextInt(7)) {
case 0:
menu = new Ramyun();
break;
case 1:
menu = new Kimbob();
break;
case 2:
menu = new Topokki();
break;
case 3:
menu = new Twigim();
break;
case 4:
menu = new Sundae();
break;
case 5:
menu = new Mandu();
break;
case 6:
menu = new Odeng();
break;
}
return menu;
}
// 매출 계산
public int calculateSales() {
int totalSales = 0;
int raCount = 0;
int kimCount = 0;
int toCount = 0;
int twiCount = 0;
int sunCount = 0;
int manCount = 0;
int oCount = 0;
for (Menu menu : orders) {
if (menu.getClass() == Ramyun.class)
raCount++;
else if (menu.getClass() == Kimbob.class)
kimCount++;
else if (menu.getClass() == Topokki.class)
toCount++;
else if (menu.getClass() == Twigim.class)
twiCount++;
else if (menu.getClass() == Sundae.class)
sunCount++;
else if (menu.getClass() == Mandu.class)
manCount++;
else if (menu.getClass() == Odeng.class)
oCount++;
totalSales += menu.getPrice();
}
int raSales = Ramyun.PRICE * raCount;
int kimSales = Kimbob.PRICE * kimCount;
int toSales = Topokki.PRICE * toCount;
int sunSales = Sundae.PRICE * sunCount;
int twiSales = Twigim.PRICE * twiCount;
int manSales = Mandu.PRICE * manCount;
int oSales = Odeng.PRICE * oCount;
System.out.printf("[라면] %d개: %d원n", raCount, raSales);
System.out.printf("[김밥] %d개: %d원n", kimCount, kimSales);
System.out.printf("[떡볶이] %d개: %d원n", toCount, toSales);
System.out.printf("[튀김] %d개: %d원n", twiCount, twiSales);
System.out.printf("[순대] %d개: %d원n", sunCount, sunSales);
System.out.printf("[만두] %d개: %d원n", manCount, manSales);
System.out.printf("[오뎅] %d개: %d원n", oCount, oSales);
System.out.println("==================");
return totalSales;
}
}
관련자료
-
이전
-
다음
댓글 0개
등록된 댓글이 없습니다.