class Food{
private String menu;
private int price;
private int no;
public Food(String menu,int price,int no) {
this.menu=menu;
this.price=price;
this.no=no;
}
public void getData() {
System.out.println("[주문"+this.no+"]");
System.out.println("메뉴: "+this.menu);
System.out.println("가격: "+this.price);
}
}
public class FoodTestReview {
public static void main(String[] args) {
//[주문1]
//메뉴명: 피자
//가격:15000원
//-----------
Food[] fd=new Food[3];
fd[0]=new Food("피자", 15000, 1);
fd[1]=new Food("파스타", 35000, 2);
fd[2]=new Food("수제버거", 8000, 3);
for(int i=0;i<fd.length;i++)
{
Food f=fd[i];
f.getData();
System.out.println("----------");
}
//결과
[주문1]
메뉴: 피자
가격: 15000
----------
[주문2]
메뉴: 파스타
가격: 35000
----------
[주문3]
메뉴: 수제버거
가격: 8000
----------
~~~~~~~~~~~~~~~~~~~~
(예제)
<클래스1-Menu>
public class Menu {
private String food;
private int price;
private int no;
private String tasty;
public Menu(int no,String food,int price,String tasty) {
this.no=no;
this.food=food;
this.price=price;
this.tasty=tasty;
}
public static void showTitle() {
System.out.println("No\t음식\t가격\t맛평가");
System.out.println("-------------------");
}
public void getData() {
System.out.println(no+"\t"+food+"\t"+price+"\t"+tasty);
}
<클래스2-MenuArrays>
public class MenuArrays {
public static void main(String[] args) {
//배열로 객체 처리하여 4개 출력
//no. 음식명 가격 맛평가
//1. 막국수 8000 추천메
Menu[] mn=new Menu[4];
mn[0]=new Menu(1, "막국수", 8000, "추천메뉴");
mn[1]=new Menu(2, "감자전", 5000, "인기메뉴");
mn[2]=new Menu(3, "메밀전", 7000, "1위메뉴");
mn[3]=new Menu(4, "들기름국수", 6000, "수요미식회추천");
Menu.showTitle();
for(int i=0;i<mn.length;i++)
{
Menu m=mn[i];
m.getData();
}
//결과
No 음식 가격 맛평가
-------------------
1 막국수 8000 추천메뉴
2 감자전 5000 인기메뉴
3 메밀전 7000 1위메뉴
4 들기름국수 6000 수요미식회추천
'JAVA' 카테고리의 다른 글
210813_상속 (0) | 2021.08.13 |
---|---|
210813_스캐너+클래스+메서드 (0) | 2021.08.13 |
210813_메서드 (0) | 2021.08.13 |
210813_오버로딩 (0) | 2021.08.13 |
210812_call by (0) | 2021.08.12 |