내가 처음 작성한 코드 :
public class ProductOrder{
String productName;
int price;
int quantity;
}
public class ProductOrderMain2{
public static void main(String[], args){
ProductOrder orders = new ProductOrder[];
// 이렇게 하면 배열의 요소에 생성할 객체의 주소를 저장하게 됨
public static ProductOrder createOrder (String productName, int price, int quantity){
this.productName = productName;
this.price = price;
this.quantity = quantity;
}
orders[0] = new createOrder(두부, 2000, 2);
orders[1] = new createOrder(김치, 5000, 1);
orders[2] = new createOrder(콜라, 1500, 2);
public static void printOrders(ProductOrder[] orders){
for(int i =0; i<order.length; i++; ){
println("상품명: "+order.productNAme[i]+", 가격: "+order.price[i]+", 수량: "+order.quantity[i]+\n);
}
}
public static int getTotalAmount(ProductOrder[] orders){
int total;
for(int i=0; i<order.lenth; i++;){
total += order.price[i];
}
println("총 결제 금액: "+total+);
}
}
}
틀린 부분 :
1. 참조형 배열을 선언하는 법을 몰랐다.
2. 배열을 선언할때는 꼭 크기를 지정해주어야한다.
3. static을 붙였기 때문에 this를 쓸 수 없다.
4. 모든 메서드에 public을 붙였다. (public이 붙어야하는줄 알았음..)
5. 향상된 for문 쓸 줄 몰라서 걍 for문 씀
6. 총 가격 구하는 getTotalAmount에서 가격x수량 했어야하는데 걍 모든 price를 합해버린..뭐하노
7. getTotalAmount는 값을 반환해 주어야하며 총 결제 금액을 return만 할 뿐 출력해주지 않음. 값이 반환된다면 별도로 출력하는 문이 필요함
수정된 코드
public class ProductOder{
String productName;
int price;
int quantity;
}
public class ProductOderMain2{
public static void main(String[], args){
ProductOder[] orders = new ProductOrder[3];
orders[0] = new createOder(두부, 2000, 2);
orders[1] = new createOder(김치, 5000, 1);
orders[2] = new createOder(콜라, 1500, 2);
printOrders(orders);
getTotalAmount(orders); //반환값을 받아야함
int totalAmount = getTotalAmount(orders);
System.out.println("총 결제 금액: "+totalAmount+);
static ProductOder createOder (String productName, int price, int quantity){
ProductOder.productName = productName;
ProductOder.price = price;
ProductOder.quantity = quantity;
}
static void printOrders(ProductOder[] orders){
for(ProductOrder order : orders){
System.out.println("상품명: "+order.productNAme[i]+", 가격: "+oder.price[i]+", 수량: "+oder.quantity[i]+\n);
}
}
static int getTotalAmount(ProductOrder[] orders){
int total;
for(ProductOrder order : orders){
total += order.price * order.quantity;
}
return ("총 결제 금액: "+total+); //반환타입이 있음
}
}
}
상품명와 가격, 수량을 입력받아 출력해보기
public class ProductOder{
String productName;
int price;
int quantity;
}
public class ProductOderMain2{
public static void main(String[], args){
Scanner scanner = new Scanner(System.in);
System.out.println("주문의 개수를 입력하세요 : ");
int number = sanner.nextLine();
sanner.nextLine(); //입력버퍼를 지우기 위한 코드
ProductOrder orders = new ProductOrder[number]; //입력받은 크기만큼 배열 만듦
for(int i=0; i<=number; i++;){
System.out.println((i+1)+"번째 주문정보를 입력하세요 : ");
System.out.println("상품명 : ");
String productName = sanner.nextLine();
System.out.println("가격 : ");
int price = sanner.nextLine();
System.out.println("수량 : ");
int quantity = sanner.nextLine();
orders[i-1] = new createOrder(productName, price, quantity);
}
printOrders(orders);
getTotalAmount(orders); //반환값을 받아야함
int totalAmount = getTotalAmount(orders);
System.out.println("총 결제 금액: "+totalAmount+);
static ProductOrder createOrder (String productName, int price, int quantity){
ProductOrder.productName = productName;
ProductOrder.price = price;
ProductOrder.quantity = quantity;
}
static void printOrders(ProductOder[] orders){
for(ProductOrder order : orders){
System.out.println("상품명: "+order.productNAme[i]+", 가격: "+oder.price[i]+", 수량: "+oder.quantity[i]+\n);
}
}
static int getTotalAmount(ProductOrder[] orders){
int total;
for(ProductOrder order : orders){
total += order.price * order.quantity;
}
return ("총 결제 금액: "+total+); //반환타입이 있음
}
}
}