코딩/java

음식 정보 출력

런던전통손만두 2019. 3. 3. 15:42
반응형

public class Food {

int calory;

protected int price;

protected double weight;

 

public String toString() {

return "칼로리: " + calory +"\n가격: " + price + "\n무게: " + weight;

  }

}

 

 

public class Melon extends Food{

 String farmInfo;

 

public String toString() {

return "칼로리: " + calory +"\n가격: " + price + "\n무게: " + weight + "\n재배농장: " + farmInfo;

  }

}

 

 

public class Potato extends Food{

public int getUnitPrice() {

double Unitprice = price / weight;

return (int)Unitprice;

  }

 

public String toString() {

return "칼로리: " + calory +"\n가격: " + price + "\n무게: " + weight + "\n단위가격: " + getUnitPrice();

 }

}

 

 

public class Practice_67 {

 

public static void main(String[] args) {

Food f0 = new Food();

Melon f1 = new Melon();

Potato f2 = new Potato();

 

f0.calory = 100;

f0.price = 100;

f0.weight = 12.3;

 

f1.calory = 200;

f1.price = 200;

f1.weight = 5.5;

f1.farmInfo = "좋은 이웃들";

 

f2.calory = 300;

f2.price = 200;

f2.weight = 50.0;

 

System.out.println("음식의 정보입니다.");

System.out.println(f0.toString() + "\n");

System.out.println("멜론의 정보입니다.");

System.out.println(f1.toString() + "\n");

System.out.println("감자의 정보입니다.");

System.out.println(f2.toString() + "\n");

System.out.println("감자의 단위가격은 " + f2.getUnitPrice() + "입니다.");

 }

}

 

결과:

 

반응형