본문 바로가기
study/Java

[Java] 22. Test2 풀이 ( 사과, 복숭아는 가격과 당도를 입력하여 객체를 생성하고, 콜라, 사이다는 가격, 용량을 비스킷, 쿠키는 가격,무게를 입력하여 객체를 생성하기)

by 금이패런츠 2022. 3. 21.
728x90
반응형
package chap7;
/*
Food, Fruit, Drink, Snack, Apple, Peach, 
Cock, Sidar, Biscuit, Cookie 클래스를 구현하기
 
모든 식료품(Food)은 가격(price)과 판매갯수(sellcnt), 보너스포인트(point)를 가지고 있습니다. 
식료품 객체를 생성하기 위해서는 가격을  입력받아야 합니다. 
식료품의 종류로는 과일(Fruit), 음료(Drink),과자(Snack)로 나누어 집니다. 
  과일은 당도(brix)를,음료는 용량(ml)를 과자는무게(gram) 정보를 가지고 있습니다.   
  과일에는 사과(Apple)와 복숭아(Peach)  음료에는 콜라(Cock) 사이다(Sidar)
  과자는   비스킷(Biscuit)과 쿠키(Cookie)가 있습니다.
  
  사과 복숭아는 가격과 당도를 입력하여 객체를 생성하고,
  콜라, 사이다는가격, 용량을 
  비스킷, 쿠키는 가격,무게를  입력하여  객체를 생성합니다.
*/
class Food {
	int price;
	int sellcnt;
	int point;

	Food(int price) {
		this.price = price;
	}
}

class Fruit extends Food {
	double brix;

	Fruit(int price, double brix) {
		super(price);
		this.brix = brix;
	}
}

class Drink extends Food {
	int ml;

	Drink(int price, int ml) {
		super(price);
		this.ml = ml;
	}
}

class Snack extends Food {
	int gram;

	Snack(int price, int gram) {
		super(price);
		this.gram = gram;
	}
}

class Apple extends Fruit {
	Apple(int price, double brix) {
		super(price, brix);
	}
	public String toString() {
		return "사과";
	}
}

class Peach extends Fruit {
	Peach(int price, double brix) {
		super(price, brix);
	}
	public String toString() {
		return "복숭아";
	}
}

class Cock extends Drink {
	Cock(int price, int ml) {
		super(price, ml);
	}
	public String toString() {
		return "콜라";
	}
}

class Sidar extends Drink {
	Sidar(int price, int ml) {
		super(price, ml);
	}
	public String toString() {
		return "사이다";
	}
}

class Biscuit extends Snack {
	Biscuit(int price, int gram) {
		super(price, gram);
	}
	public String toString() {
		return "비스켓";
	}
}

class Cookie extends Snack {
	Cookie(int price, int gram) {
		super(price, gram);
	}
	public String toString() {
		return "쿠키";
	}
}

public class Test0314_2 {
	public static void main(String[] args) {
		Apple apple = new Apple(1000,10.5);
		System.out.println("사과 가격:"+apple.price);
		System.out.println("사과 당도:"+apple.brix);
		Fruit peach = new Peach(3000,16.5);
		System.out.println("복숭아 가격:"+peach.price);
		System.out.println("복숭아 당도:"+peach.brix);
		Cock cock = new Cock(1500,500);
		System.out.println("콜라 가격:"+cock.price);
		System.out.println("콜라 용량:"+cock.ml);
		Cookie c = new Cookie(5000, 1000);
		System.out.println("쿠키 가격:" + c.price);
		System.out.println("쿠키 무게:" + c.gram);
	}
}
728x90
반응형