Problem Solving

[Java] Requirements Q101: Barista/Coffee Class Design

Isaac S. Lee 2023. 8. 8. 10:44

📌Q101 Class

package com.test.question.q101;

import com.test.question.q92.Book;

public class Q101 {
	public static void main(String[] args) {
		/*
		● 요구사항
		음료를 판매하고 그 매출액과 판매량을 구하시오.
		
		● 조건
		static 멤버를 구현하시오.
		Barista
			바리스타 클래스
			행동
				Espresso makeEspresso(int bean)
					에스프레소 1잔을 만든다.
					int bean : 원두량(g)
					return Espreeso : 에스프레소 1잔
				Espresso[] makeEspressoes(int bean, int count)
					에스프레소 N잔을 만든다.
					int bean : 원두량(g)
					int count : 음료 개수(잔)
					return Espresso[] : 에스프레소 N잔
				Latte makeLatte(int bean, int milk)
					라테 1잔을 만든다.
					int baen : 원두량(g)
					int milk : 우유량(ml)
					return Latte : 라테 1잔
				Latte[] makeLattes(int bean, int milk, int count)
					라테 N잔을 만든다.
					int baen : 원두량(g)
					int milk : 우유량(ml)
					int count : 음료 개수(잔)
					return Latte[] : 라테 N잔
				Americano makeAmericano(int bean, int water, int ice)
					아메리카노 1잔을 만든다.
					int baen : 원두량(g)
					int water : 물량(ml)
					int ice : 얼음 개수(개)
					return Americano : 아메리카노 1잔
				Americano[] makeAmericanos(int bean, int water, int ice, int count)
					아메리카노 N잔을 만든다.
					int baen : 원두량(g)
					int water : 물량(ml)
					int ice : 얼음 개수(개)
					int count : 음료 개수(잔)
					return Americano[] : 아메리카노 N잔
				void result()
					판매 결과를 출력한다.
					음료 판매량(에스프레소 판매 개수, 라테 판매 개수, 아메리카노 판매 개수)
					원자재 소비량(원두 소비량, 물 소비량, 우유 소비량, 얼음 소비량)
					매출액(원두 판매액, 물 판매액, 우유 판매액, 얼음 판매액)
					
		Coffee
			공용 정보 클래스
			상태
				static bean
					총 원두량(g)
				static water
					총 물 용량(ml)
				static ice
					총 얼음 개수(개)
				static milk
					총 우유 용량(ml)
				static beanUnitPrice
					원두 단가(원)
					1g당 1원
				static waterUnitPrice
					물 단가(원)
					1ml당 0.2원
				static iceUnitPrice
					얼음 단가(원)
					1개당 3원
				static milkUnitPrice
					우유 단가(원)
					1ml당 4원
				static beanTotalPrice
					원두 총 판매액(원)
				static waterTotalPrice
					물 총 판매액(원)
				static iceTotalPrice
					얼음 총 판매액(원)
				static milkTotalPrice
					우유 총 판매액(원)
				static americano
					아메리카노 총 판매 개수(잔)
				static latte
					라테 총 판매 개수(잔)
				static espresso
					에스프레소 총 판매 개수(잔)
		Espresso
			에스프레소 클래스
			상태
				bean
					에스프레소 생산 시 들어가는 원두량(g)
			행동
				void drink()
					커피를 마신다.(출력)
		Latte
			라테 클래스
			상태
				bean
					라테 생산 시 들어가는 원두량(g)
				milk
					라테 생산 시 들어가는 우유량(ml)
			행동
				void drink()
					커피를 마신다.(출력)
		Americano
			아메리카노 클래스
			상태
				bean
					아메리카노 생산 시 들어가는 원두량(g)
				water
					아메리카노 생산 시 들어가는 물량(ml)
				ice
				아메리카노 생산 시 들어가는 얼음 개수(개)
			행동
				void drink()
				커피를 마신다.(출력)

		● 출력
		원두 30g으로 만들어진 에스프레소를 마십니다. //e1.drink();
		원두 30g, 우유 250ml으로 만들어진 라테를 마십니다. //l1.drink();
		원두 30g, 물 300ml, 얼음 20개로 만들어진 아메리카노를 마십니다. //a1.drink();
		
		//손님 4
		원두 25g으로 만들어진 에스프레소를 마십니다.
		원두 25g으로 만들어진 에스프레소를 마십니다.
		원두 25g으로 만들어진 에스프레소를 마십니다.
		원두 25g으로 만들어진 에스프레소를 마십니다.
		원두 25g으로 만들어진 에스프레소를 마십니다.
		원두 25g으로 만들어진 에스프레소를 마십니다.
		원두 25g으로 만들어진 에스프레소를 마십니다.
		원두 25g으로 만들어진 에스프레소를 마십니다.
		원두 25g으로 만들어진 에스프레소를 마십니다.
		원두 25g으로 만들어진 에스프레소를 마십니다.
		
		//손님 5
		원두 25g, 우유 300ml으로 만들어진 라테를 마십니다.
		원두 25g, 우유 300ml으로 만들어진 라테를 마십니다.
		원두 25g, 우유 300ml으로 만들어진 라테를 마십니다.
		원두 25g, 우유 300ml으로 만들어진 라테를 마십니다.
		원두 25g, 우유 300ml으로 만들어진 라테를 마십니다.
		
		//손님 6
		원두 20g, 물 350ml, 얼음 30개로 만들어진 아메리카노를 마십니다.
		원두 20g, 물 350ml, 얼음 30개로 만들어진 아메리카노를 마십니다.
		원두 20g, 물 350ml, 얼음 30개로 만들어진 아메리카노를 마십니다.
		원두 20g, 물 350ml, 얼음 30개로 만들어진 아메리카노를 마십니다.
		원두 20g, 물 350ml, 얼음 30개로 만들어진 아메리카노를 마십니다.
		원두 20g, 물 350ml, 얼음 30개로 만들어진 아메리카노를 마십니다.
		원두 20g, 물 350ml, 얼음 30개로 만들어진 아메리카노를 마십니다.
		원두 20g, 물 350ml, 얼음 30개로 만들어진 아메리카노를 마십니다.
		원두 20g, 물 350ml, 얼음 30개로 만들어진 아메리카노를 마십니다.
		원두 20g, 물 350ml, 얼음 30개로 만들어진 아메리카노를 마십니다.
		원두 20g, 물 350ml, 얼음 30개로 만들어진 아메리카노를 마십니다.
		원두 20g, 물 350ml, 얼음 30개로 만들어진 아메리카노를 마십니다.
		원두 20g, 물 350ml, 얼음 30개로 만들어진 아메리카노를 마십니다.
		원두 20g, 물 350ml, 얼음 30개로 만들어진 아메리카노를 마십니다.
		원두 20g, 물 350ml, 얼음 30개로 만들어진 아메리카노를 마십니다.
		
		//barista.result();
		=================================
		판매 결과
		=================================
		
		---------------------------------
		음료 판매량
		---------------------------------
		에스프레소 : 11잔
		아메리카노 : 16잔
		라테 : 6잔
		
		---------------------------------
		원자재 소비량
		---------------------------------
		원두 : 765g
		물 : 5,550ml
		얼음 : 470개
		우유 : 1,750ml
		
		---------------------------------
		매출액
		---------------------------------
		원두 : 765원
		물 : 1,110원
		얼음 : 1,410원
		우유 : 7,000원
		*/
		
		//바리스타
		Barista barista = new Barista();

		//손님 1
		//에스프레소 1잔 주문 - 원두 30g
		Espresso e1 = barista.makeEspresso(30);
		e1.drink();

		//손님 2
		//라테 1잔 주문 - 원두 30g, 우유 250ml
		Latte l1 = barista.makeLatte(30, 250);
		l1.drink();

		//손님 3
		//아메리카노 1잔 주문 - 원두 30g, 물 300ml, 각얼음 20개
		Americano a1 = barista.makeAmericano(30, 300, 20);
		a1.drink();

		//손님 4
		//에스프레소 10잔 주문 - 원두 25g
		Espresso[] e2 = barista.makeEspressoes(25, 10);

		for (Espresso e : e2) {
		      e.drink();
		}

		//손님 5
		//라테 5잔 주문 - 원두 25g, 우유 300ml
		Latte[] l2 = barista.makeLattes(25, 300, 5);

		for (Latte l : l2) {
		      l.drink();
		}

		//손님 6
		//아메리카노 15잔 주문 - 원두 20g, 물 350ml, 각얼음 30개
		Americano[] a2 = barista.makeAmericanos(20, 350, 30, 15);

		for (Americano a : a2) {
		      a.drink();
		}

		//결산
		barista.result();
	}
}

 

📌Barista Class

package com.test.question.q101;

// 커피 음료를 제조하고 판매 결과를 출력
class Barista {
	// 에스프레소 단품 주문 메소드
	public Espresso makeEspresso(int bean) {
		Espresso espresso = new Espresso(bean); // 에스프레소 객체 생성
		Coffee.countCoffee(espresso); // 에스프레소 판매량, 원자재 소비량 누적
		
		return espresso;
	}

	// 에스프레소 여러 잔 주문
	public Espresso[] makeEspressoes(int bean, int count) {
		Espresso[] espressoes = new Espresso[count]; // 에스프레소 배열 객체 생성
		for(int i = 0; i < espressoes.length; ++i) {
			Espresso espresso = new Espresso(bean); // 에스프레소 객체 생성
			Coffee.countCoffee(espresso); // 에스프레소 판매량, 원자재 소비량 누적
			espressoes[i] = espresso; // 배열에 에스프레소 객체 추가
		}
		
		return espressoes;
	}
	
	// 라테 단품 주문
	public Latte makeLatte(int bean, int milk) {
		Latte latte = new Latte(bean, milk); // 라테 객체 생성
		Coffee.countCoffee(latte); // 라테 판매량, 원자재 소비량 누적
		
		return latte;
	}

	// 라테 여러 잔 주문
	public Latte[] makeLattes(int bean, int milk, int count) {
		Latte[] lattes = new Latte[count]; // 라테 배열 객체 생성
		
		for (int i = 0; i < lattes.length; ++i) {
			Latte latte = new Latte(bean, milk); // 라테 객체 생성
			Coffee.countCoffee(latte); // 라테 판매량, 원자재 소비량 누적
			lattes[i] = latte; // 배열에 라테 객체 추가
		}
		
		return lattes;
	}
	
	// 아메리카노 단품 주문
	public Americano makeAmericano(int bean, int water, int ice) {
		Americano americano = new Americano(bean, water, ice); // 아메리카노 객체 생성
		Coffee.countCoffee(americano); // 아메리카노 판매량, 원자재 소비량 누적
		
		return americano;
	}

	// 아메리카노 여러 잔 주문
	public Americano[] makeAmericanos(int bean, int water, int ice, int count) {
		Americano[] americanos = new Americano[count]; // 아메리카노 배열 객체 생성
		
		for (int i = 0; i < americanos.length; ++i) {
			Americano americano = new Americano(bean, water, ice); // 아메리카노 객체 생성
			Coffee.countCoffee(americano); // 아메리카노 판매량, 원자재 소비량 누적
			americanos[i] = americano; // 배열에 아메리카노 객체 추가
		}
		
		return americanos;
	}
	
	// 결산
	public void result() {
		System.out.println("=================================");
		System.out.println("판매 결과");
		System.out.println("=================================");
		System.out.println("---------------------------------");
		System.out.println("음료 판매량");
		System.out.println("---------------------------------");
		System.out.printf("에스프레소 : %d잔\n", Coffee.getEspresso());
		System.out.printf("아메리카노 : %d잔\n", Coffee.getAmericano());
		System.out.printf("라테 : %d잔\n", Coffee.getLatte());
		System.out.println();
		System.out.println("---------------------------------");
		System.out.println("원자재 소비량");
		System.out.println("---------------------------------");
		System.out.printf("원두 : %,dg\n", Coffee.getBean());
		System.out.printf("물 : %,dml\n", Coffee.getWater());
		System.out.printf("얼음 : %,d개\n", Coffee.getIce());
		System.out.printf("우유 : %,dml\n", Coffee.getMilk());
		System.out.println();
		System.out.println("---------------------------------");
		System.out.println("매출액");
		System.out.println("---------------------------------");
		System.out.printf("원두 : %,d원\n", Coffee.getBeanTotalPrice());
		System.out.printf("물 : %,d원\n", Coffee.getWaterTotalPrice());
		System.out.printf("얼음 : %,d원\n", Coffee.getIceTotalPrice());
		System.out.printf("우유 : %,d원\n", Coffee.getMilkTotalPrice());
		System.out.println();
	}
}

 

📌Coffee Class

package com.test.question.q101;

// 커피 정보 및 판매 정보 관리
class Coffee {
	private static int bean;	// 원두량(g)
	private static int water;	// 물량(ml)
	private static int ice;		// 얼음 개수(개)
	private static int milk;	// 우유량(ml)
	
	private static int beanUnitPrice;		// 원두 단가(원) 1g:1원
	private static double waterUnitPrice;	// 물 단가(원) 1ml:0.2원
	private static int iceUnitPrice;		// 얼음 단가(원) 1개:3원
	private static int milkUnitPrice;		// 우유 단가(원) 1ml:4원
	
	private static int beanTotalPrice;		// 원두 총 판매액(원)
	private static int waterTotalPrice;		// 물 총 판매액(원)
	private static int iceTotalPrice;		// 얼음 총 판매액(원)
	private static int milkTotalPrice;		// 우유 총 판매액(원)
	
	private static int americano;	// 아메리카노 총 판매 개수(잔)
	private static int latte;		// 라테 총 판매 개수(잔)
	private static int espresso;	// 에스프레소 총 판매 개수(잔)
	
	static {
		// 정적 변수 초기화
		Coffee.beanUnitPrice = 1;
		Coffee.waterUnitPrice = 0.2;
		Coffee.iceUnitPrice = 3;
		Coffee.milkUnitPrice = 4;
		
		Coffee.bean = 0;
		Coffee.water = 0;
		Coffee.ice = 0;
		Coffee.milk = 0;
		
		Coffee.beanTotalPrice = 0;
		Coffee.waterTotalPrice = 0;
		Coffee.iceTotalPrice = 0;
		Coffee.milkTotalPrice = 0;
		
		Coffee.americano = 0;
		Coffee.latte = 0;
		Coffee.espresso = 0;
	}

	// 에스프레소 판매량 누적
	public static void countCoffee(Espresso espresso) {
		Coffee.espresso++;
		Coffee.bean += espresso.getBean();
		Coffee.beanTotalPrice += Coffee.beanUnitPrice * espresso.getBean();
	}

	// 라테 판매량 누적
	public static void countCoffee(Latte latte) {
		Coffee.latte++;
		Coffee.bean += latte.getBean();
		Coffee.milk += latte.getMilk();
		Coffee.beanTotalPrice += Coffee.beanUnitPrice * latte.getBean();
		Coffee.milkTotalPrice += Coffee.milkUnitPrice * latte.getMilk();
	}
	
	// 아메리카노 판매량 누적
	public static void countCoffee(Americano americano) {
		Coffee.americano++;
		Coffee.bean += americano.getBean();
		Coffee.water += americano.getWater();
		Coffee.ice += americano.getIce();
		Coffee.beanTotalPrice += Coffee.beanUnitPrice * americano.getBean();
		Coffee.waterTotalPrice += Coffee.waterUnitPrice * americano.getWater();
		Coffee.iceTotalPrice += Coffee.iceUnitPrice * americano.getIce();
	}
	
	public static int getBean() {
		return bean;
	}

	public static int getWater() {
		return water;
	}

	public static int getIce() {
		return ice;
	}

	public static int getMilk() {
		return milk;
	}

	public static int getBeanUnitPrice() {
		return beanUnitPrice;
	}

	public static double getWaterUnitPrice() {
		return waterUnitPrice;
	}

	public static int getIceUnitPrice() {
		return iceUnitPrice;
	}

	public static int getMilkUnitPrice() {
		return milkUnitPrice;
	}

	public static int getBeanTotalPrice() {
		return beanTotalPrice;
	}
	public static int getWaterTotalPrice() {
		return waterTotalPrice;
	}

	public static int getIceTotalPrice() {
		return iceTotalPrice;
	}

	public static int getMilkTotalPrice() {
		return milkTotalPrice;
	}

	public static int getAmericano() {
		return americano;
	}

	public static int getLatte() {
		return latte;
	}

	public static int getEspresso() {
		return espresso;
	}
}

 

📌Espresso Class

package com.test.question.q101;

class Espresso {
	private int bean; // // 에스프레소 생산 시 들어가는 원두량(g)

	Espresso(int bean){
		this.bean = bean;
	}
	
	public int getBean() {
		return bean;
	}

	public void drink() {
		System.out.printf("원두 %dg으로 만들어진 에스프레소를 마십니다.\n", bean);
	}
}

 

📌Latte Class

package com.test.question.q101;

class Latte {
	private int bean; // 라테 생산 시 들어가는 원두량(g)
	private int milk; // 라테 생산 시 들어가는 우유량(ml)
	
	Latte(int bean, int milk){
		this.bean = bean;
		this.milk = milk;
	}

	public int getBean() {
		return bean;
	}

	public int getMilk() {
		return milk;
	}

	public void drink() {
		System.out.printf("원두 %dg, 우유 %dml으로 만들어진 라테를 마십니다.\n", bean, milk);
	}
}

 

📌Americano Class

package com.test.question.q101;

class Americano {
	private int bean; // 아메리카노 생산 시 들어가는 원두량(g)
	private int water; // 아메리카노 생산 시 들어가는 물량(ml)
	private int ice; // 아메리카노 생산 시 들어가는 얼음 개수(개)
	
	Americano(int bean, int water, int ice){
		this.bean = bean;
		this.water = water;
		this.ice = ice;
	}

	public int getBean() {
		return bean;
	}

	public int getWater() {
		return water;
	}

	public int getIce() {
		return ice;
	}
	
	public void drink() {
		System.out.printf("원두 %dg, 물 %dml, 얼음 %d개로 만들어진 라테를 마십니다.\n", bean, water, ice);
	}
}