Backend/JAVA

[JAVA] 상품 구매 및 반품 프로그램

howdyoon 2023. 3. 26. 22:08

package oop0322;

public class Test04_buyer {

public static void main(String[] args) {
    //상품 구매 및 반품 프로그램

    //1)상품 만들기
    //->Product   클래스
    //->SmartTV   클래스
    //->Notebook  클래스
    //->HandPhone 클래스

    //2)상품 진열하기
    SmartTV tv=new SmartTV();
    Notebook note=new Notebook();
    HandPhone phone=new HandPhone();

    //3)상품 구매하기
    //->Buyer클래스
    Buyer kim=new Buyer();
    kim.buy(tv);    //100
    kim.buy(note);  //200
    kim.buy(phone); //150

    kim.buy(note);  //200
    kim.buy(note);  //200
    kim.buy(note);  //잔액부족		
    kim.buy(note);  //잔액부족		

    kim.disp();
    System.out.println("---------------");

    //4)반품하기
    //->Order 클래스
    //->(Buyer클래스 복사해서 수정)
    //->상품을 중복해서 구매하지 않았다는 전제하에 반품
    Order lee=new Order();
    lee.buy(tv);
    lee.buy(phone);
    lee.buy(note);

    lee.disp();


    System.out.println("<<<<< 반품");
    lee.refund(note);

    System.out.println("<<<<< 결과");
    lee.disp();
		
	}//main() end
}//class end

 

[ Buyer클래스 ]

package oop0322;

//상품 구매 관련 클래스
class Buyer {
	private int myMoney=1000; //나의 총 재산
	private int myMileage=0;  //나의 마일리지 점수(bonusPoint 누적)
	private int i=0;
	
	//상품을 구매했을때 어떤 상품을 구매했는지 저장 (나의 구매상품 목록)
	Product[] item=new Product[10];
       /*
		                Product
		+------------+-------------+-------------+--
		|  SmartTV   |   Notebook  |  HandPhone  |  ~~
		+------------+-------------+-------------+--
		  item[0]       item[1]       item[2]      ~~
       */
	
	public Buyer() {}
	
	/*
		메소드 오버로드 (함수명 중복 정의)
		public void buy(SmartTV a) {}
		public void buy(Notebook a) {}
		public void buy(HandPhone a) {}
	*/
	
	public void buy(Product p) {//다형성
		//Product->SmartTV, Notebook, HandPhone클래스의 부모클래스
		
		if(this.myMoney<p.price) {
			System.out.println("잔액이 부족합니다!!");
			return;
		}//if end
		
		item[i++]=p; //구매상품 저장
		this.myMoney=this.myMoney-p.price;         //나의 재산은 감소
		this.myMileage=this.myMileage+p.bonusPoint;//나의 마일리지는 증가
		
	}//buy() end
	
	public void disp() {
		
    //문제) 구매 상품 목록과 구매한 상품의 총 합계금액을 구하시오
    //     (item 배열 활용)

    //구매한 상품 목록
    StringBuilder shoplist=new StringBuilder();

    //사용금액
    int hap=0;

    for(int n=0; n<item.length; n++) {
        if(item[n]==null) {
            break;
        }//if end
        shoplist.append(item[n].toString()+" "); //상품명
        hap=hap+item[n].price;
    }//for end		

    System.out.println("구매 상품 목록 : " + shoplist);
    System.out.println("사용금액 : " + hap);
    System.out.println("나의 남은 재산 : " + this.myMoney);
    System.out.println("나의 마일리지 : "  + this.myMileage);
	}//disp() end
	
}//class end

[ Order클래스 ]

package oop0322;

import java.util.Vector;

//상품 구매 관련 클래스
class Order {
	private int myMoney=1000; //나의 총 재산
	private int myMileage=0;  //나의 마일리지 점수(bonusPoint 누적)
	private int i=0;
	
	//상품을 구매했을때 어떤 상품을 구매했는지 저장 (나의 구매상품 목록)
	private Vector<Product> item=new Vector<>();
	
	public Order() {}
	
	public void buy(Product p) {
		
		if(this.myMoney<p.price) {
			System.out.println("잔액이 부족합니다!!");
			return;
		}//if end
		
		item.add(p); //구매상품 저장
		this.myMoney=this.myMoney-p.price;         //나의 재산은 감소
		this.myMileage=this.myMileage+p.bonusPoint;//나의 마일리지는 증가
		
	}//buy() end
	
	public void disp() {
		
		if(item.isEmpty()) {
			System.out.println("구매 상품 없음!!");
			return;
		}//if end
		
		//문제) 구매 상품 목록과 구매한 상품의 총 합계금액을 구하시오
		//     (item 벡터 활용)
		
		//구매한 상품 목록
		StringBuilder shoplist=new StringBuilder();
		
		//사용금액
		int hap=0;
		
		for(int n=0; n<item.size(); n++) {
			Product p=item.get(n);
			shoplist.append(p.toString()+" "); //상품명
			hap=hap+p.price;
		}//for end		
		
        System.out.println("구매 상품 목록 : " + shoplist);
        System.out.println("사용금액 : " + hap);
        System.out.println("나의 남은 재산 : " + this.myMoney);
        System.out.println("나의 마일리지 : "  + this.myMileage);
	}//disp() end
	
	
	public void refund(Product p) {
		if(item.remove(p)) {
			System.out.println(p.toString() + " 반품되었습니다~");
			this.myMoney  =this.myMoney+p.price;
			this.myMileage=this.myMileage-p.bonusPoint;
		}else {
			System.out.println("구매내역에 상품이 없습니다!!");
		}//if end
	}//refund() end
	
	
}//class end

 

[ Product클래스 ]

package oop0322;

class Product extends Object{
			 //extends Object 생략가능
	public int price;	   //상품가격
	public int bonusPoint; //마일리지
	
	public Product() {} //default constructor
	public Product(int price) {
		//this.멤버변수=지역변수
		this.price=price;
		//상품가격의 10%를 보너스 점수 책정
		this.bonusPoint=(int)(price*0.1);
	}//end
}//class end


class SmartTV extends Product {
	public SmartTV() {
		super(100);      //상품가격 price=100, bonusPoint=10
	}
	
	@Override
	public String toString() {
		return "스마트TV"; //상품명
	}
}//class end


class Notebook extends Product {
	public Notebook() {
		super(200);    //상품가격 price=200, bonusPoint=20
	}
	
	@Override
	public String toString() {
		return "노트북"; //상품명
	}
}//class end

class HandPhone extends Product {
	public HandPhone() {
		super(150);    //상품가격 price=150, bonusPoint=15
	}
	
	@Override
	public String toString() {
		return "핸드폰"; //상품명
	}
}//class end