장바구니에 여러종류의 상품객체를 담을 수 있다.
프로그램 시작과 함께 상품목록을 먼저 출력한다.
1.장바구니추가 : 제품번호를 입력해서 제품을 장바구니에 추가한다.
2.장바구니 목록 : 장바구니에 등록된 제품목록을 출력하고 전체 가격을 출력한다.
3.종료 : 프로그램을 종료한다.
Product : interface
NoteBook, Phone : class (no, name, price, dc할인률)
Cart : class
Main : class
Product.java
package kosta.cart;
public interface Product {
public int discountApply();
public void show();
public int getNo();
}
Phone.java
package kosta.cart;
public class Phone implements Product {
private int no;
private String name;
private int price;
private boolean flip;
public Phone() {
}
public Phone(int no, String name, int price, boolean flip) {
super();
this.no = no;
this.name = name;
this.price = price;
this.flip = flip;
}
@Override
public int discountApply() {
int price = 0;
price = (int)(this.price*0.8);
return price;
}
@Override
public void show() {
System.out.println("상품번호 : " + no);
System.out.println("상품명 : " + name);
System.out.println("상품가격 : " + price);
System.out.println("플립 : " + flip);
System.out.println();
}
@Override
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public boolean isFlip() {
return flip;
}
public void setFlip(boolean flip) {
this.flip = flip;
}
}
NoteBook.java
package kosta.cart;
public class NoteBook implements Product {
private int no;
private String name;
private int price;
private boolean graphic;
public NoteBook() {
}
public NoteBook(int no, String name, int price, boolean graphic) {
super();
this.no = no;
this.name = name;
this.price = price;
this.graphic = graphic;
}
@Override
public int discountApply() {
int price = 0;
price = (int)(this.price * 0.9);
return price;
}
@Override
public void show() {
System.out.println("상품번호 : " + no);
System.out.println("상품명 : " + name);
System.out.println("상품가격 : " + price);
System.out.println("그래픽 : " + graphic);
System.out.println();
}
@Override
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public boolean isGraphic() {
return graphic;
}
public void setGraphic(boolean graphic) {
this.graphic = graphic;
}
}
Cart.java
package kosta.cart;
public class Cart {
private Product[] arr;
private int total;
private int count;
public Cart() {
arr = new Product[10];
}
public void addCart(Product p) {
arr[count++] = p;
total += p.discountApply();
}
public void orderList() {
System.out.println("<******** 장바구니 목록 *******>");
for(int i = 0; i < count; i++) {
arr[i].show();
}
System.out.println("총금액 : " + total);
System.out.println("*****************************");
}
}
Main.java
package kosta.cart;
import java.util.Scanner;
public class Main {
public static void showProduct(Product[] arr) {
for(int i = 0; i < arr.length; i++) {
arr[i].show();
}
}
public static Product getProduct(int no, Product arr[]) {
Product p = null;
for(int i = 0; i < arr.length; i++) {
if(no == arr[i].getNo()) {
p = arr[i];
break;
}
}
return p;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Cart cart = new Cart();
Product arr[] = {
new Phone(1, "갤럭시22", 1000000, false),
new Phone(2, "갤럭시플립2", 1500000, true),
new NoteBook(3, "그램17", 2000000, true)
};
while(true) {
showProduct(arr); //상품목록 출력
System.out.println("1.장바구니 추가 2.장바구니 목록 3.종료");
System.out.print("선택 : ");
int menu = sc.nextInt();
switch (menu) {
case 1:
System.out.print("상품선택 : ");
int no = sc.nextInt();
Product p = getProduct(no, arr);
cart.addCart(p);
System.out.println("장바구니 추가완료");
break;
case 2:
cart.orderList();
break;
case 3:
System.out.println("종료");
return;
}
}
}
}