기본햄버거 생성

package composite;

import lombok.Data;

@Data // getter, setter 생성
public class Burger {
	private int price;
	private String desc;

	public Burger() {
		this(1500, "기본버거");
	}

	public Burger(int price, String desc) {
		this.price = price;
		this.desc = desc;
		System.out.println(desc + "가 만들어졌습니다.");
	}
	
	public static void main(String[] args) {
		System.out.println(new Burger().getPrice());
	}
}

 

코카콜라 만들기

package composite;

import lombok.Data;

@Data
public class Coke {
	private int price;
	private String desc;
	
	public Coke() {
		this(1500, "코카콜라");
	}
	
	public Coke(int price, String desc) {
		this.price = price;
		this.desc = desc;
		System.out.println(desc + "가 만들어졌습니다.");
	}
	
	public static void main(String[] args) {
		System.out.println(new Coke().getPrice());
	}
}

 

감자칩 만들기

package composite;

import lombok.Data;

@Data
public class FrenchFried {
	private int price;
	private String desc;
	
	public FrenchFried() {
		this(2000, "감자칩");
	}
	
	public FrenchFried(int price, String desc) {
		this.price = price;
		this.desc = desc;
		System.out.println(desc + "이 만들어졌습니다.");
	}
	
	public static void main(String[] args) {
		System.out.println(new FrenchFried().getPrice());
	}
}

 

Burger를 상속받아서 BigBurger 만들기

package composite;

import lombok.Data;

@Data
public class BigBurger extends Burger {
	public BigBurger() {
		super(4000, "빅버거");
	}
	
	public BigBurger(int price, String desc) {
		super(price, desc);
	}
	
	public static void main(String[] args) {
		System.out.println(new BigBurger().getPrice());
	}
}

 

Burger를 상속받아서 ShrimpBurger 만들기

package composite;

import lombok.Data;

@Data
public class ShrimpBurger extends Burger {

	public ShrimpBurger() {
		super(3500, "쉬림프버거");
	}

	public ShrimpBurger(int price, String desc) {
		super(price, desc);
	}
	
	public static void main(String[] args) {
		System.out.println(new ShrimpBurger().getPrice());
	}
}

 

콤포지션을 이용하여 클래스 안에 클래스를 사용하기

BigBurgerSet만들기

package composite;

import lombok.Data;

// 자바는 다중 상속이 안됨 (부모가 여럿이 될 수 없음)

@Data
public class BigBurgerSet {
	// 콤포지션(중요) - 상속이 아닌 만들어 놓은 클래스를 가져오는 것
	private BigBurger bigBurger;
	private Coke coke;
	private FrenchFried frenchFried;
	
	public BigBurgerSet() {
		this(new BigBurger(), new Coke(), new FrenchFried());
	}
	
	public BigBurgerSet(BigBurger bigBurger) {
		this.bigBurger = bigBurger;
		this.coke = new Coke();
		this.frenchFried = new FrenchFried();
	}

	public BigBurgerSet(BigBurger bigBurger, Coke coke, FrenchFried frenchFried) {
		this.bigBurger = bigBurger;
		this.coke = coke;
		this.frenchFried = frenchFried;
	}
	
	public static void main(String[] args) {
		System.out.println(new BigBurgerSet().getBigBurger());
	}

}

 

ShrimpBurgerSet  만들기

package composite;

import lombok.Data;

@Data
public class ShrimpBurgerSet {

	// 콤포지션(중요) - 상속이 아닌 만들어 놓은 클래스를 가져오는 것
	private ShrimpBurger shrimpBurger;
	private Coke coke;
	private FrenchFried frenchFried;
	
	public ShrimpBurgerSet() {
		this(new ShrimpBurger(), new Coke(), new FrenchFried());
	}
	
	public ShrimpBurgerSet(ShrimpBurger shrimpBurger) {
		this(shrimpBurger, new Coke(), new FrenchFried());
	}
	
	public ShrimpBurgerSet(Coke coke) {
		this(new ShrimpBurger(), coke, new FrenchFried());
	}
	
	public ShrimpBurgerSet(FrenchFried frenchFried) {
		this(new ShrimpBurger(), new Coke(), frenchFried);
	}
	
	public ShrimpBurgerSet(ShrimpBurger shrimpBurger, Coke coke) {
		this(shrimpBurger, coke, new FrenchFried());
	}
	
	public ShrimpBurgerSet(ShrimpBurger shrimpBurger, FrenchFried frenchFried) {
		this(shrimpBurger, new Coke(), frenchFried);
	}
	
	public ShrimpBurgerSet(Coke coke, FrenchFried frenchFried) {
		this(new ShrimpBurger(), coke, frenchFried);
	}
	
	
	// 생성자가 제일 많은 것이 기준
	public ShrimpBurgerSet(ShrimpBurger shrimpBurger, Coke coke, FrenchFried frenchFried) {
		this.shrimpBurger = shrimpBurger;
		this.coke = coke;
		this.frenchFried = frenchFried;
	}
	
	public static void main(String[] args) {
		System.out.println(new ShrimpBurgerSet().getShrimpBurger());
	}
}

 

LotteriaApp 만들기 (햄버거 운영)

package composite;

public class LotteriaApp {
	public static void main(String[] args) {
		// 빅버거 세트
		BigBurgerSet set1 = new BigBurgerSet();
		
		System.out.println();
		
		// 기본버거
		Burger burger1 = new Burger();
		
		System.out.println();
		
		// 콜라
		Coke coke1 = new Coke();
		
		System.out.println();
		
		
		// BigBurgerSet
		BigBurgerSet set2 = new BigBurgerSet(
				new BigBurger(3000, "빅버거할인"),
				new Coke(),
				new FrenchFried()	
				);
		
		System.out.println();
		
		BigBurgerSet set3 = new BigBurgerSet(
				new BigBurger(2000, "빅버거할인"),
				new Coke(),
				new FrenchFried()
				);
		
		
		System.out.println();
		
		
		// 새우버거 세트 추가요
		ShrimpBurgerSet set4 = new ShrimpBurgerSet();
		
		System.out.println();
		
		
		BigBurgerSet set5 = new BigBurgerSet(
				new BigBurger(2000, "빅버거할인")
				);
		
		
		System.out.println();
	}
}

 

 

+ Recent posts