스크린샷 2025-08-08 오후 3.54.31.png

package com.haenin.section03.abstraction;

import java.util.Scanner;

public class Application {
    public static void main(String[] args) {
        Scanner sc = new Scanner((System.in));
        CarRacer racer = new CarRacer();

        int input = 0;
        do{
            System.out.println("===== 카레이싱 프로그램 =====");
            System.out.println("1. 시동걸기");
            System.out.println("2. 전진");
            System.out.println("3. 정지");
            System.out.println("4. 시동끄기");
            System.out.println("9. 프로그램 종료");
            System.out.println("메뉴 선택: ");
            input = sc.nextInt();
            switch (input){
                case 1: racer.startUp();
                    break;
                case 2: racer.stepAccelator();
                    break;
                case 3: racer.stepBreak();
                    break;
                case 4: racer.turnOff();
                    break;
                case 9:
                    System.out.println("프로그램을 이용해 주셔서 감사합니다. ");
                    return; // 필기. break문으로 스위치문을 빠져나오고
                            // 필기. while문 조건에서 9이기에 false빠져나와도 다음 구문이 계속 실행되기에 return으로 종료
                default:
                    System.out.println("잘못된 메뉴를 선택하셨습니다. \\n" +
                                       " 다시 입력해주세요: ");

            }
        }while(input !=9 );
    }
}

package com.haenin.section03.abstraction;

public class CarRacer {

    private Car myCar = new Car();

    public void startUp() {
        this.myCar.startUp();
    }

    public void stepAccelator() {
        this.myCar.go();
    }

    public void stepBreak() {
        this.myCar.stop();
    }

    public void turnOff() {
        this.myCar.turnOff();
    }
}

package com.haenin.section03.abstraction;

public class Car {

    private boolean isOn ;
    private int speed;

    // 필기. heap영역에 생성이 되었기 때문에 false를 넣지않아도 자동으로 false들어간 상태
    public void startUp() {
        if (this.isOn){
            System.out.println("이미 시동이 걸려 있습니다. ");
        }else {
            this.isOn = true;
            System.out.println("시동을 걸었습니다. 부릉릉~~!");
        }
    }

    public void go() {
      if(this.isOn){
          this.speed += 10;
          System.out.println("차가 움직입니다. 현재 속도는 "+ speed +"(km/h) 입니다.");
      }else{
          System.out.println("차의 시동을 먼저 걸어주세요");
      }

    }

    public void stop() {
        if(isOn){
          if(speed > 0){
              System.out.println("급 브레이크를 밟았습니다. 차가 멈춥니다.");
              speed=0;
          }else{
              System.out.println("차는 이미 멈춰있는 상태입니다. ");
          }
        }else{
            System.out.println("차의 시동이 걸려있지 않습니다. 시동을 먼저 걸어주세요.");
        }
    }

    public void turnOff() {
        if(isOn){
            if(speed > 0){
                System.out.println("달리는 상태에서는 시동을 끌 수 없습니다. 차를 멈춰 멈처주세요");
            }else{
                System.out.println("시동을 끕니다. 다시 운행하시려면 시동을 켜주세요!");
                isOn=false;
            }
        }else{
            System.out.println("차의 시동이 걸려있지 않습니다. 시동을 먼저 걸어주세요.");
        }

    }
}