JAVA Programming/JAVA 문제

[19] 객체 협력 객체지향 문제

꾸준히개발하자 2020. 7. 14. 11:19

 

 

 

Menu

package comperation3;

public class Menu {
	// 각각의 메뉴들의 가격들을 상수로 정의
	
	public static int STARAMERICANO = 4000;
	public static int STARLATTE = 4100;

	public static final int CONGAMERICANO = 4300;
	public static final int CONGLATEE = 4200;
	
}

 

StarCaffee

package comperation3;

public class StarCaffee {
	
	int money;
	
	public String brewing(int money) { // if문 각각  지불한 가격에 맞게  출력 
		
		this.money += money; // 가게 수입 증가
		
		if(money == Menu.STARAMERICANO) {
			return "스타카페 아메리카노 구입";
		} else if (money == Menu.STARLATTE) {
			return "스타카페 라떼 구입";
		} else {
			return null;
		}
	}
}

 

CongCaffee

package comperation3;

public class CongCaffee {

	int money;
	
	public String brewing(int money) { // if문 각각  지불한 가격에 맞게  출력 
		
		this.money += money; // 가게 수입 증가
		if(money == Menu.CONGAMERICANO) {
			return "콩다방 아메리카노 구입";
		} else if (money == Menu.CONGLATEE) {
			return "콩다방 라떼 구입";
		} else {
			return null;
		}
	}
}

 

 

People 클래스 

나이와 돈을 가지고 있고  두 카페중 골라서 갈수있다. 

지불한 돈에 맞게 구입할수있다. 

 

package comperation3;

public class People {
	
	private String name;
	private int money;
	
	public People(String name, int money) {
		this.name = name; // 자신의 멤버변수 = 매개변수
		this.money = money;
	}
		
	public void buyStarCaffee(StarCaffee starCaffee , int money ) {
		String str = starCaffee.brewing(money); // 지불한 돈에 맞게 구입 
		if(str != null) {
			System.out.println(name +"님이 " + money + "으로 " + str);
			
		}	
	}
	
	public void buyCongCaffee(CongCaffee congCaffee , int money ) {
		String str = congCaffee.brewing(money);
		if(str != null) {
			System.out.println(name +"님이 " + money + "으로 " + str);
		}
	}
}

 

구현 클래스 

 

package comperation3;

import calendar.Menu;

public class Order {

	public static void main(String[] args) {
		People peoplekim = new People("김졸려",10000);
		People peoplelee = new People("이피곤",10000);
		
		StarCaffee starcaffee = new StarCaffee();
		CongCaffee congcaffee = new CongCaffee();
		
		peoplekim.buyStarCaffee(starcaffee, comperation3.Menu.STARAMERICANO);
		peoplelee.buyCongCaffee(congcaffee, comperation3.Menu.CONGAMERICANO);
		
	}

}