당신은 온라인 상점의 주문 관리 시스템을 만들려고 한다.

Untitled

출력 예시

상품명: 두부, 가격: 2000, 수량: 2 상품명: 김치, 가격: 5000, 수량: 1 상품명: 콜라, 가격: 1500, 수량: 2 총 결제 금액: 12000

내 코드

package myspace;

class ProductOrder {
	String productName;
	int price;
	int quantity;
	
}
public class ProductOrderMain {

	public static void main(String[] args) {
		ProductOrder  order1 = new ProductOrder();
		order1.productName = "두부";
		order1.price = 2000;
		order1.quantity = 2;
		ProductOrder order2 = new ProductOrder();
		order2.productName = "김치";
		order2.price = 5000;
		order2.quantity = 1;
		ProductOrder order3 = new ProductOrder();
		order3.productName = "콜라";
		order3.price = 1500;
		order3.quantity = 2;
		ProductOrder[] product = {order1, order2, order3};
		
		😒
		int count = order1.price * 2 + order2.price * 1 + order3.price * 2 ;
		for (ProductOrder p : product) {
			System.out.println("상품명:"+ p.productName + ", 가격:" +p.price + ", 수량:" +p.quantity);
		}
		System.out.println("총 결제 금액: " + count);
	}

}

개선사항: 아래 총 금액을 계산하는 부분의 코드 개선

😊
int count = 0;
		for (ProductOrder p : product) {
			System.out.println("상품명:"+ p.productName + ", 가격:" +p.price + ", 수량:" +p.quantity);
			count += p.price*p.quantity;
		}
		System.out.println("총 결제 금액: " + count);
	}