ProductTest 제품구매

zhyun·2020년 8월 31일
0

java

목록 보기
5/11
class Product{
    String name; //물건이름
    int price; // 물건가격
    int bonusPoint; // (마일리지) 보너스포인트
    
    public Product(String name, int price){
    	this.name = name;
        this.price = price;
        bonusPoint = price/10;
    }

}

class Notebook extends Product{
	NoteBook(String name, int price){
        	super(name, price);
    }
    
    @Override
	public String toString() {
		return "NoteBook";
	}
	
}

class Styler extends Product{
	public Styler(String name, int price){
		super(name,price);
	}
	
	@Override
	public String toString(){
		return "Styler";
	}
}


class Fridge extends Product{
	public Fridge(String name, int price){
		super(name, price);
	}

	@Override
	public String toString() {
		return "냉장고";
	}

}

class Buyer{
    String name; // 구매자 이름
    int money; // 구매자 소유돈
    int mileage; // 마일리지
    Vetor item = new Vector();
    
    public Buyer(String name, int money){
    	this.name = name;
        this.money = money;
    }
    
    void buy(Product pd){ // buy 메서드
    	if(money < pd.price){ // 내돈이 제품가격보다 적을때
    		System.out.println("돈 가져와");
        	return;
        } // 내돈이 더 많을때
        money -= pd.price; // 돈에서 제품 가격 빼고
        mileage += pd.bonusPoint; // 마일리지에서 제품 보너스포인트 더하고
        item.add(pd); // Vector에 객체 추가
        
        System.out.println(name+"고객님 "+pd+"를 구매해주셔셔 감사합니다");
    }
    
    void summary(){ // 영수증 메서드
    	System.out.println();		
		System.out.println("\t영\t수\t증");
		System.out.println("구매목록");
        if(item.size() == 0){ // Vector에 저장된 객체의 개수가 0일때
    		System.out.println("구매하신 물건이 없습니다.");
            return;
    }
	int sum = 0;
    for(int idx = 0; idx < item.size() ; idx++){
    	if(item.get(idx) instanceof Product){ //item.get(idx)가 Product 참조
        	Product pd = (Product)item.get(idx); // down-casting
            System.out.println("\t"+pd.name+ "\t"+ pd.price+"만원");
            sum+=pd.price; // 합계에 제품 가격 계속 더해
        }
    }
    System.out.println("-----------총합 : " + sum +"만원----------"+"\n"+
					name+" 고객님의 남은돈은 "+money+"만원이고\n마일리지는 						"+mileage+"만원입니다.\n오늘도 좋은하루 보내십쇼 호갱님~!");
	}
	void refund (Product pd){
    	if(item.isEmpty()){ // Vector가 비어있는지 검사
        	System.out.println("구매하신 물건이 없습니다.)"
            return;
        }
        if(item.remove(pd)){ // Vector에 저장되어 있는 객체 제거 (방 뒤에서부터)
            money += pd.price;
            mileage -= pd.bonusPoint;
            System.out.println(pd+"를 반품하셨습니다.");
        }else{
            System.out.println();
            System.out.println(pd+"를 구매하신 적이 없습니다.");
        }
     }  
  }
  
  public class ProductTest {
	public static void main(String[] args) { //출근
		NoteBook nb = new NoteBook("Mac", 200);
		Styler st = new Styler("LG", 100); 
		Fridge f = new Fridge("SAMSUNG",500); 
		
		Buyer b = new Buyer("이운주", 1000);
		
//		b.buy(f); // 냉장고 구매
		b.buy(nb); // 노트북 구매
		b.buy(nb); // 돈 모자름
		b.buy(st);
		b.buy(st);
		
		b.summary();
		
		b.refund(f); //냉장고 반품
//		b.refund(st);
//		b.refund(nb);
		
		
	}

}
profile
HI :)

0개의 댓글