본문 바로가기

Java

Day06 - 03

public class Oop3 {
    public static void main(String[] args) {
        Fruit fruit1 = new Fruit();
        fruit1.info();
        fruit1.setName("사과");
        fruit1.setPrice(1000);
        fruit1.setColor("red");
        fruit1.setFrom("충주");
//       fruit1.setSize("소과");                   //자식에 추가된 필드를 수정할 수 없음
        fruit1.info();
        System.out.println(fruit1);



        Apple apple =new Apple();
        apple.setName("사과");                                                                //
        apple.setPrice(1000);
        apple.setColor("빨강");
        apple.setFrom("충주");
        apple.setSize("소과");                  //자식에 추가된 필드를 수정할 수 있음
        apple.info();
        System.out.println(apple);

        Banana banana =new Banana("바나나",1500,"yellow","필리핀","델몬트");
        System.out.println(banana);
    }
}

 

import java.util.Scanner;

public class Oop4 {                                                                                                     //public클래스 하나의 파일에 하나만
    public static void main(String[] args) {
        Fruit[] fruits =new Fruit[3];       //과일종류 담아줄 배열
        fruits[0] =new Apple("사과",1000,"red","충주","소과");
        fruits[1]=new Banana("바나나",1500,"yellow","필리핀","델몬트");                                      //1.플룻 2,과일 3메인 출력
        fruits[2]=new Orange("오렌지",2000,"orange","캘리포니아",8);

//        for(Fruit fr : fruits){
//            System.out.println(fr);             //toString info가찍힘
//        }

        Fruit[] order = new Fruit[10];          //구입한 과일 목록을 저장할 배열
        int cnt =0;                             //과일 구입 갯수
        //메뉴 물어보기
        Scanner sc = new Scanner(System.in);
        while(true){
            System.out.println("🥬🥦🍅🍒🍑🍐메뉴를 고르세요.🥬🥦🍅🍒🍑🍐");
            System.out.println("1.🍒구입");
            System.out.println("2.🍑구입");
            System.out.println("3.🍅구입");
            System.out.println("4.구입목록 보기");
            System.out.println("5. 과일 검색");
            System.out.println("6.프로그램 종료");

            int sel =sc.nextInt();
            if(sel==6)
            switch (sel) {
                case 1: case 2: case 3:
                    //저장할공간10개 플롯객체를 0번배열에 들어감
                    order[cnt++] = fruits[sel - 1];               //-1방번호-1 배열에 객체가 들어감       0번방에 집어놓고 1씩++
                    System.out.println(fruits[sel - 1].getName() + "가 담겼습니다."); //객체의 저장한name을가져옴

                    break;
                case 4:
                    System.out.println("구입한 과일👍");
                    for(int i=0; i<cnt; i++) {
                        System.out.println((i + 1) + " : " + order[i]);               //toString order의 객체
                    }
                        break;
                case 5:
                    System.out.println("검색할 과일을 선택하세요");
                    System.out.println("1.사과 2.바나나 3.오렌지");
                    int choice =sc.nextInt();
                    System.out.println(fruits[choice-1]);
                    break;
                default:
                    System.out.println("입력을 확인하세요");
            }
        }
    }
}

 

'Java' 카테고리의 다른 글

Day07  (0) 2023.02.05
Day06 - 04  (0) 2023.02.01
Day06 - 02  (0) 2023.02.01
Day06 - 01  (0) 2023.02.01
Day06  (0) 2023.02.01