자바

JAVA STEP 28. 상속, Static 예제

IT의 큰손 2023. 2. 20. 20:59
728x90

예제 1) 요구사항 : 포장하는 직원 객체를 만드시오. 그 직원을 통해 연필, 지우개, 볼펜, 자를 포장하시오.

  • 조건
    • Static 멤버를 구현하시오.
    • Packer
      • 사무용품을 포장하는 직원
      • 상태
        • static pencilCount
        • 연필 포장 개수(개)
        • static eraserCount 
        • 지우개 포장 개수(개)
        • static ballPoinPenCount
        • 볼펜 포장 개수(개)
        • static rulerCount
        • 자 포장 개수(개)
      • 행동
        • void packing(Pencil pencil)
          • 연필을 검수하고 포장한다.
          • Pencil pencil : 연필
        • void packing(Eraser eraser)
          • 지우개를 검수하고 포장한다.
          • Eraser eraser : 지우개
        • void packing(BallPointPen ballPointPen)
          • 볼펜을 검수하고 포장한다.
          • BallPointPen ballPointPen : 볼펜
        • void packing(Ruler ruler)
          • 자를 검수하고 포장한다.
          • Ruler ruler : 자
        • void countPacking(int type)
          • 포장한 내용물 개수를 출력한다.
          • int type : 출력할 내용물의 종류
          • 0 : 모든 내용물
          • 1 : 연필
          • 2 : 지우개
          • 3 : 볼펜
          • 4 : 자
    • Pencil
      • 연필 클래스
      • 상태
        • hardness
        • 흑연 등급(4B, 3B, 2B, B, HB, H, 2H, 3H, 4H)
      • 행동
        • String info()
        • 연필의 정보를 반환한다.
    • Eraser
      • 지우개 클래스
      • 상태
        • size
        • 지우개 크기(Large, Medium, Small)
      • 행동
        • String info()
        • 지우개의 정보를 반환한다.
    • BallPointPen
      • 볼펜 클래스
      • 상태
        • thickness
        • 볼펜 심 두께(0.3mm, 0.5mm, 0.7mm, 1mm, 1.5mm)
        • color
        • 볼펜 색상(red, blue, green, black)
      • 행동
        • String info()
        • 볼펜의 정보를 반환한다.
    • Ruler
      • 자 클래스
      • 상태
        • length
        • 자 길이(30cm, 50cm, 100cm)
        • shape
        • 자 형태(줄자, 운형자, 삼각자)
      • 행동
        • String info()
        • 자의 정보를 반환한다
  • 소스코드
  •  
package com.test.question;

public class Q0100 {

	public static void main(String[] args) {
		//Packer : 사무용품을 포장하는 직원
		//포장하는 직원
		Packer packer = new Packer();

		//연필
		Pencil p1 = new Pencil();
		p1.setHardness("HB");
		packer.packing(p1);

		Pencil p2 = new Pencil();
		p2.setHardness("4B");
		packer.packing(p2);

		//지우개
		Eraser e1 = new Eraser();
		e1.setSize("Large");
		packer.packing(e1);

		//볼펜
		BallPointPen b1 = new BallPointPen();
		b1.setThickness(0.3);
		b1.setColor("black");
		packer.packing(b1);

		BallPointPen b2 = new BallPointPen();
		b2.setThickness(1.5);
		b2.setColor("red");
		packer.packing(b2);

		//자
		Ruler r1 = new Ruler();
		r1.setLength(30);
		r1.setShape("줄자");
		packer.packing(r1);

		//결과 확인
		packer.countPacking(0);
		packer.countPacking(1);
		packer.countPacking(2);
		packer.countPacking(3);
		packer.countPacking(4);
		
	}

}

class Pencil {
	
	private String hardness;

	public String getHardness() {
		return hardness;
	}

	public void setHardness(String hardness) {
		
		this.hardness = hardness;
	}
	
	public String info() {
		return String.format("%s 진하기 연필", this.hardness);
	}
	
}

class Eraser {
	private String Size;

	public String getSize() {
		return Size;
	}

	public void setSize(String size) {
		Size = size;
	}


	public String info() {
		return String.format("%s 사이즈 지우개", this.Size);
	}
}

class BallPointPen {
	private double thickness;
	private String color;
	
	public double getThickness() {
		return thickness;
	}
	public void setThickness(double thickness) {
		this.thickness = thickness;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	
	public String info() {
		return String.format("%s 색상 %.1f 볼펜", this.color, this.thickness);
	}
	
}

class Ruler {
	private int length;
	private String shape;
	
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	public String getShape() {
		return shape;
	}
	public void setShape(String shape) {
		this.shape = shape;
	}
	
	public String info() {
		return String.format("%dcm %s", this.length, this.shape);
	}
}

class Packer {
	private static int pencilCount;
	private static int eraserCount;
	private static int ballPointPenCount;
	private static int rulerCount;
	
	public void packing(Pencil pencil) {
		
		System.out.printf("포장 전 검수 : %s입니다.\n", pencil.info());
		
		if(pencil.getHardness().equals("4B")||pencil.getHardness().equals("3B")||pencil.getHardness().equals("2B")
				||pencil.getHardness().equals("B")||pencil.getHardness().equals("HB")||pencil.getHardness().equals("H")
				||pencil.getHardness().equals("2H")||pencil.getHardness().equals("3H")||pencil.getHardness().equals("4H")) {
			pencilCount++;
			System.out.println("포장을 완료했습니다.");
		}else {
			System.out.println("불량입니다. 포장을 실패했습니다.");
		}
	
	}//pencil
	
	public void packing(Eraser eraser) {
		System.out.printf("포장 전 검수 : %s입니다.\n", eraser.info());
		
		if(eraser.getSize().equals("Large")||eraser.getSize().equals("Medium")||eraser.getSize().equals("Small")) {
			eraserCount++;
			System.out.println("포장을 완료했습니다.");
		}else {
			System.out.println("불량입니다. 포장을 실패했습니다.");
		}
	}//eraser
	
	public void packing(BallPointPen BPP) {
		System.out.printf("포장 전 검수 : %s입니다.\n", BPP.info());
		
		if(BPP.getColor().equals("red")||BPP.getColor().equals("blue")
				||BPP.getColor().equals("green")||BPP.getColor().equals("black")){
			if(BPP.getThickness()==0.3||BPP.getThickness()==0.5||BPP.getThickness()==0.7
					||BPP.getThickness()==1||BPP.getThickness()==1.5) {
				ballPointPenCount++;
				System.out.println("포장을 완료했습니다.");
			}
		}else {
			System.out.println("불량입니다. 포장을 실패했습니다.");
		}
	}
	
	public void packing(Ruler ruler) {
		System.out.printf("포장 전 검수 : %s입니다.\n", ruler.info());
		
		if(ruler.getLength()==30||ruler.getLength()==50||ruler.getLength()==100) {
			if(ruler.getShape().equals("줄자")||ruler.getShape().equals("운형자")
					||ruler.getShape().equals("삼각자")) {
				rulerCount++;
				System.out.println("포장을 완료했습니다.");
			}
		}else {
			System.out.println("불량입니다. 포장을 실패했습니다.");
		}
	}
	
	public void countPacking(int type) {
		 
		System.out.println("=====================");
		System.out.println("포장 결과");
		System.out.println("=====================");
		
		if(type==0) {
			System.out.printf("연필 %d회\n", Packer.pencilCount);
			System.out.printf("지우개 %d회\n", Packer.eraserCount);
			System.out.printf("볼펜 %d회\n", Packer.ballPointPenCount);
			System.out.printf("자 %d회\n", Packer.rulerCount);
		}else if(type==1) {
			System.out.printf("연필 %d회\n", Packer.pencilCount);
		}else if(type==2) {
			System.out.printf("지우개 %d회\n", Packer.eraserCount);
		}else if(type==3) {
			System.out.printf("볼펜 %d회\n", Packer.ballPointPenCount);
		}else if(type==4) {
			System.out.printf("자 %d회\n", Packer.rulerCount);
		}
	}
	
}
  • 실행결과

예제 1 실행결과

 

예제 2) 요구사항 : 음료를 판매하고 그 매출액과 판매량을 구하시오.

  • 조건
    • 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()
        • 커피를 마신다.(출력)
  • 소스코드
  •  
package com.test.question;

public class Q0101 {

	public static void main(String[] args) {
		
		//바리스타
		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.makeAmericanoes(20, 350, 30, 15);

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

		//결산
		barista.result();

	}//main

}//main class

//*************************************************
class Coffee {
	
	public static int bean;					//총 원두량
	public static int water;				//총 물량
	public static int ice;					//총 얼음 개수
	public static int milk;					//총 우유 량
	
	public static int beanUnitPrice;		//원두 단가(원) 1g당 1원
	public static double waterUnitPrice;	//물 단가(원) 1ml당 0.2원
	public static int iceUnitPrice;			//얼음 단가(원) 1개당 3원
	public static int milkUnitPrice;		//우유 단가(원) 1ml당 4원
	
	public static int beanTotalPrice; 		//원두 총 판매액(원)
	public static int waterTotalPrice;		//물 총 판매액(원)
	public static int iceTotalPrice;		//얼음 총 판매액(원)
	public static int milkTotalPrice;		//우유 총 판매액(원)
	public static int TotalPrice;			//총 합계 판매액(원)
	
	public static int americano;			//아메리카노 총 판매 개수(잔)
	public static int latte;				//라떼 총 판매 개수(잔)
	public static int espresso;				//에스프레소 총 판매 개수(잔)
	
	static {
		Coffee.bean = 0;
		Coffee.water = 0;
		Coffee.ice = 0;
		Coffee.milk = 0;
		
		Coffee.beanUnitPrice = 1;
		Coffee.waterUnitPrice = 0.2;
		Coffee.iceUnitPrice = 3;
		Coffee.milkUnitPrice = 4;
		
		Coffee.beanTotalPrice = 0;
		Coffee.waterTotalPrice = 0;
		Coffee.beanTotalPrice = 0;
		Coffee.milkTotalPrice = 0;
		Coffee.TotalPrice = 0;
		
		Coffee.americano = 0;
		Coffee.latte = 0;
		Coffee.espresso = 0;
	}

	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 getTotalPrice() {
		return TotalPrice;
	}

	public static int getAmericano() {
		return americano;
	}

	public static int getLatte() {
		return latte;
	}

	public static int getEspresso() {
		return espresso;
	}
	
	public static void countCoffee(Espresso espresso) {
		Coffee.espresso++;
		Coffee.bean += espresso.getBean();
		Coffee.beanTotalPrice += Coffee.beanUnitPrice * espresso.getBean();
		Coffee.TotalPrice += beanTotalPrice;
	}
	
	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();
		Coffee.TotalPrice += beanTotalPrice;
	}
	
	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();
		Coffee.TotalPrice += beanTotalPrice;
	}
}

//*************************************************
class Espresso {
	private int bean;
	
	Espresso(int bean){
		this.bean = bean;
	}
	public int getBean() {
		return bean;
	}

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

//*************************************************
class Latte {
	private int bean;
	private int milk;
	
	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);
	}
}

//*************************************************
class Americano {
	private int bean;
	private int water;
	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 );
	}
	
}

//*************************************************
class Barista {
	
	public Espresso makeEspresso(int bean) {
		
		Espresso espresso = new Espresso(bean);
		Coffee.countCoffee(espresso);
		
		return espresso;
	}
	
	public Latte makeLatte(int bean, int milk) {
		
		Latte latte = new Latte(bean, milk);
		Coffee.countCoffee(latte);
		
		return latte;
	}
	
	public Americano makeAmericano(int bean, int water, int ice) {
		
		Americano americano = new Americano(bean, water, ice);
		Coffee.countCoffee(americano);
		
		return americano;
	}
	
	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[] 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[] makeAmericanoes(int bean, int water, int ice, int count) {
		
		Americano[] americanoes = new Americano[count];
		for(int i=0; i<americanoes.length; i++) {
			Americano americano = new Americano(bean, water, ice);
			Coffee.countCoffee(americano);
			americanoes[i] = americano;
		
		}
		return americanoes;
	
	}
	
	public void result() {
		System.out.println("========================================");
		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.printf("총 합계 금액 : %,d원\n", Coffee.getTotalPrice());
		System.out.println();
		
	
	}	
}
  • 실행결과
  •  

예제 2 실행결과 - 1
예제 2 실행결과 - 2

 

728x90