Java 문제, 상품 주문 시스템 - 리팩토링
이전 시간에 자바로 상품 주문 시스템을 구현한 적이 있다. 이번에는 클래스를 만들어 리팩토링을 해보도록 한다.
이전 예시 코드
public class Main {
public class ProductOrder {
String productName;
int price;
int quantity;
}
public static void main(String[] args) {
// 여러 상품 주문 담는 클래스 배열
ProductOrder[] product = new
// 상품 주문 정보를 ProductOrder 타입으로 저장하기
ProductOrder kimchi = new ProductOrder();
kimchi.productName = "김치";
kimchi.price = 7000;
kimchi.quantity = 2;
ProductOrder coke = new ProductOrder();
coke.productName = "콜라박스";
coke.price = 22000;
coke.quantity = 1;
ProductOrder bean = new ProductOrder();
bean.productName = "콩나물";
bean.price = 1200;
bean.quantity = 4;
ProductOrder[] productOrder = {kimchi, coke, bean};
// 상품 주문 정보 및 최종 금액 출력하기
int totalPrice = 0;
for (ProductOrder order : productOrder) {
int productPrice = order.price * order.quantity;
totalPrice += productPrice;
System.out.println("상품명: " + order.productName + ", 가격: " + order.price + ", 수량: " + order.quantity);
}
System.out.println("결제 금액: " + totalPrice);
}
}
요구 사항
ProductOrder 클래스로 만들고 멤버 변수는 다음과 같이 포함한다.
- 상품명 (productName)
- 가격 (price)
- 주문 수량 (quantity)
예시 클래스 구조
public static class ProductOrder {
String productName;
int price;
int quantity;
}
그리고 여러 상품을 배열로 관리하고, 정보들을 출력하고, 최종 금액을 계산하여 출력한다. 필요한 메소드는 다음과 같다
- static ProductOrder createOrder(String productName, int price, int quantity)
- ProductOrder 를 생성하고 매개변수로 초기값을 설정. 생성한 이후 ProductOrder 를 반환한다.
- static void printOrders(ProductOrder[] orders)
- ProductOrder 배열을 받아 상품명, 가격, 수량을 출력한다.
- static int getTotalAmount(ProductOrder[] orders)
- ProductOrder 배열을 받아 총 결제 금액을 계산하고 계산 결과를 반환한다.
리팩토링 풀이
public class Main {
public static class ProductOrder {
String productName;
int price;
int quantity;
}
public static void main(String[] args) {
// 여러 상품 주문 담는 클래스 배열
// 상품 주문 정보를 ProductOrder 타입으로 저장하기
ProductOrder kimchi = createOrder("김치", 7000, 2);
ProductOrder coke = createOrder("콜라박스", 22000, 1);
ProductOrder bean = createOrder("콩나물", 1200, 4);
ProductOrder[] productOrder = { kimchi, coke, bean };
printOrders(productOrder);
// 상품 주문 정보 및 최종 금액 출력하기
int totalAmount = getTotalAmount(productOrder);
System.out.println("최종금액: " + totalAmount);
}
static ProductOrder createOrder(String productName, int price, int quantity) {
ProductOrder prod = new ProductOrder();
prod.productName = productName;
prod.price = price;
prod.quantity = quantity;
return prod;
}
static void printOrders(ProductOrder[] orders) {
for (ProductOrder order : orders) {
System.out.println("상품명: " + order.productName + ", 가격: " + order.price + ", 수량: " + order.quantity);
}
}
static int getTotalAmount(ProductOrder[] orders) {
int total = 0;
for (ProductOrder order : orders) {
total += order.quantity * order.price;
}
return total;
}
}
상품 주문 시스템 - 사용자 입력 받기
조금 더 개선하여 사용자 입력을 받도록 코드를 변경해보도록 한다.
요구사항
- 아래 입력, 출력 예시를 참고 후 적용하기
- 주문 수량을 받도록 하기
- 전체 수량을 알 수 있도록 "추가할 전체 주문 갯수를 입력해주세요." 사용자 입력을 유도하세요.
- 가격, 수량, 상품명을 입력받도록 한다.
- 상품 순서를 알 수 있게 "{n}번째 주문 정보를 주세요" 메시지 출력
- 입력이 끝나면 등록한 상품과 총 결제 금액을 출력하도록 한다.
입력, 출력 예시
상품 정보 시스템입니다.
추가할 전체 주문 갯수를 입력해주세요.3
1번째 주문 정보를 입력해주세요.
상품명: 김치
가격: 7000
수량: 2
2번째 주문 정보를 입력해주세요.
상품명: 콜라박스
가격: 22000
수량: 1
3번째 주문 정보를 입력해주세요.
상품명: 콩나물
가격: 1200
수량: 4
상품명: 김치, 가격: 7000, 수량: 2
상품명: 콜라박스, 가격: 22000, 수량: 1
상품명: 콩나물, 가격: 1200, 수량: 4
최종금액: 40800
사용자 입력 개선 풀이
import java.util.Scanner;
public class Main {
public static class ProductOrder {
String productName;
int price;
int quantity;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 여러 상품 주문 담는 클래스 배열
System.out.println("상품 정보 시스템입니다.");
System.out.print("추가할 전체 주문 갯수를 입력해주세요.");
int OrderCount = scanner.nextInt();
scanner.nextLine();
ProductOrder[] orders = new ProductOrder[OrderCount];
// 상품 주문 정보를 ProductOrder 타입으로 저장하기
//"김치", 7000, 2
//"콜라박스", 22000, 1
//"콩나물", 1200, 4
for (int i = 0; i < OrderCount; i++) {
System.out.println((i + 1) + "번째 주문 정보를 입력해주세요.");
System.out.print("상품명: ");
String s = scanner.nextLine();
System.out.print("가격: ");
int i1 = scanner.nextInt();
System.out.print("수량: ");
int i2 = scanner.nextInt();
scanner.nextLine();
orders[i] = createOrder(s, i1, i2);
}
printOrders(orders);
// 상품 주문 정보 및 최종 금액 출력하기
int totalAmount = getTotalAmount(orders);
System.out.println("최종금액: " + totalAmount);
}
static ProductOrder createOrder(String productName, int price, int quantity) {
ProductOrder prod = new ProductOrder();
prod.productName = productName;
prod.price = price;
prod.quantity = quantity;
return prod;
}
static void printOrders(ProductOrder[] orders) {
for (ProductOrder order : orders) {
System.out.println("상품명: " + order.productName + ", 가격: " + order.price + ", 수량: " + order.quantity);
}
}
static int getTotalAmount(ProductOrder[] orders) {
int total = 0;
for (ProductOrder order : orders) {
total += order.quantity * order.price;
}
return total;
}
}