Spring 시작 Day1

2023. 2. 4. 16:57Java Study Note

반응형

첫날은 객체지향에 대해 더 공부를 하였다. Spring  S.A 과제를 통해 객체지향에 이해도를 높었다. 

하루 종일 이것만 했는데과제를 완료하지 못했다. 실행이 안되는 거 같다.

무슨 원인인지 확인하고 추후에 완성본을 밑에 추가로 공유할 예정이다. 

==================================================================

드디어 실행 가능한 완성본을 올린다. 완성본은 깃허브에 공유를 하였다. 

https://github.com/wuzudog/SpringWeek/tree/master/src

제한 요건

1. **아래의 정보를 가지는 ‘Bus’ 클래스 모델링**
    - 포함해야 할 정보
        1. 최대 승객수
        2. 현재 승객수
        3. 요금
        4. 버스 번호
        5. 주유량
        6. 현재 속도
        7. 상태
            1.  운행, 차고지 행
    - 기능
        1. 운행
        2. 버스 상태 변경
        3. 승객 탑승
        4. 속도 변경
    
    **요구사항**
    
    - 버스 번호
        - 버스 객체 생성시 번호는 고유값으로 생성되어야 합니다.
    - 버스 상태 변경
        - 버스 객체 생성시 최초 상태는 **‘운행’** 상태가 되며
        - 주유량이 떨어지거나, 운행을 종료할 때 **‘차고지행’** 상태로 변경 합니다.
        - 10미만일 경우 ‘주유가 필요하다’는 메시지를 출력해 주세요
    - 승객 탑승
        - 승객 탑승은 **‘최대 승객수’** 이하까지 가능하며 **‘운행 중’**인 상태의 버스만 가능합니다.
        - 탑승시 현재 승객수가 증가되어야 합니다.
    - 속도 변경
        - 주유 상태를 체크하고 주유량이 10 이상이어야 운행할 수 있습니다.
            - 경고메시지
                - 주유량을 확인해 주세요.
                - print문으로 출력
        - 변경할 속도를 입력 받아 현재 속도에 추가 하거나 뺄 수 있어야 합니다.

---

## 레벨업 - 선택사항

1. **아래의 정보를 가지는 ‘Taxi’ 클래스 모델링** 
    - 포함해야 할 정보
        1. 택시 번호 
        2. 주유량
        3. 현재속도
        4. 목적지 
        5. 기본거리
        6. 목적지까지 거리
        7. 기본 요금
        8. 거리당 요금
        9. 상태 (운행 중, 일반, 운행불가)
    - **기능**
        1. 운행시작
        2. 승객 탑승
        3. 속도 변경
        4. 거리당 요금 추가
        5. 요금 결제
    - **요구 사항**
        - 운행 시작
            - 운행 시작전 주유상태를 체크 하고 주유량이 10 이상이어야 운행 가능
        - 승객탑승
            - 승객 탑승은 택시 상태가 ‘일반'일 때만 가능합니다.
            - 그 외 택시는 ‘탑승 불가’ 처리를 해주세요.
            - ‘일반’ 상태의 택시가 승객을 태우면 ‘운행 중’ 상태로 변경해 주세요
        - 속도 변경
            - 변경할 속도를 입력 받아 현재 속도에 추가 하거나 뺄 수 있어야 합니다.
        - 거리당 요금 추가
            - 기본 거리보다 먼 곳은 추가 요금이 붙습니다.
            - 기본 거리와 추가 요금은 자유롭게 산정해 주세요
        - 요금 결제
            - 최종 요금을 출력하는 것으로 합니다.

Spring  S.A  미완성본

public class Main{
    public static void main(String[] args){
        Bus b1 = new Bus();
        Bus b2 = new Bus();
        b1.setNumber(100);
        b2.setNumber(200);
        b1.addClient(2);//승객 추가
        b1.changeOil(-50);//주유량 변경
        b1.changeOil(10);//주유량 변경
        b1.addClient(45);//승객 추가
        b1.addClient(5);//승객 추가
        b1.changeOil(-55);//주유량 변경

    }
}
public abstract class Transport {//상위클래스 대중교통
    //캡술화와 접근 제어자 사용 페이지 245
    private int number;//번호
    private int oil;// 주유량 기본값
    private int speed;// 속도 기본값
    private int MaxClient; // 최대승객수
    private boolean status ; //주행상태

//    public transport(){
//        this.oil = 100; //주유량 초기값
//        this.speed =0; //  속도 초기값
//    }

    // get방식은 리소스 조회
    public int getNumber(){// get방식은 리소스 조회
        return number;
    }
    public void setNumber(int number){
        this.number = number;
    }
    public int getOil(){ //주유략
        return oil ;
    }
    public void setOil(int oil){
        this.oil = oil;
    }
    public int getSpeed(){ //주유략
        return speed ;
    }
    public void setSpeed(int oil){
        this.speed = oil;
    }
    public int getMaxClient(){//최대승객수
        return MaxClient ;
    }
    public void setMaxClient(int MaxClient){
        this.MaxClient = MaxClient;
    }

    public boolean getStatus(){// 운행상태
        return status ;
    }
    public void setStatus(boolean status){
        this.status = status;
    }

}
public class Bus extends Transport {
    private int price;//요금
    private int totalPrice; // 총 요금
    private int client; //현재 버스에 탑승한 승객수
    public int getPrice(){// 요금
        return price ;
    }
    public void setPrice(int price){
        this.price = price;
    }
//    public int getTotalPrice(){// 요금
//        return totalPrice ;
//    }
//    public void setTotalPrice(int totalPrice){
//        this.totalPrice = totalPrice;
//    }

    public int getClient(){// 현재 버스에 탑승한 승객수
        return client ;
    }
    public void setClient(int client){
        this.client = client;
    }
    public Bus(int number){//버스는 번호를 가져야 한다.
        super();
        this.setNumber(number);// 버스번호 초기화
        this.setOil(100);
        this.setS(0);
        this.setMaxClient(30); // 최대 승객수 초기화
        this.setStatus(true); // 운행 상태 초기화 true는  운행시작
        this.setPrice(1000);  // 요금 초기화
//        this.setTotalPrice(0);
        this.setClient(0); //현재 버스에 탑승한 승객수 초기화

        System.out.println(number + "번 버스객체 만들어짐");
    }

    public void addClient(int num){
        int tmp = this.getClient() + num;
        if(tmp> this.getMaxClient()){
            System.out.println("최대승객수 초과");
        }else{
            System.out.println("탑승 승객 수 = "+ num);
            System.out.println("잔여 승객 수 = " + (this.getMaxClient()-tmp));
            System.out.println("요금 확인 = "+ num * getPrice());
            return setClient(tmp);
        }


    }
    public void changeOil(int num){
        int tmp = getOil() + num; //남은 주유량
        if(tmp<=50) {
            setStatus(false);//차고지행으로 변경
            System.out.println("주유량: " + tmp);
            System.out.println("상태: 차고지행");
        }else{
            setStatus(true);//운행중
            System.out.println("주유량: " + tmp);
        }
        if(tmp<10){
            System.out.println("주유가 필요합니다");
        }
        return setOil(tmp);
    }



}
반응형

'Java Study Note' 카테고리의 다른 글

열거형(enum)  (0) 2023.05.09
Spring 시작 Day2  (0) 2023.02.05
2진법, 10진법, 16진법, bit  (0) 2023.02.01
연산자와 피연산자  (0) 2023.01.24
객체지향 프로그래밍 1  (0) 2023.01.23