티스토리 뷰

[개발] - Java/후발대

후발대 20일차 전체 코드

완벽한 장면 2023. 3. 2. 21:38

후발대수업_20.

예외처리 (CustomException, Throws), 퀴즈, Thread

진행 내용 (수업자료)

실습코드

_06_CustomException

package com.sparta.hbd04.prac18;

public class _06_CustomException {
    public static void main(String[] args) {
        int age =17; //만 17세
        try {
            if(age < 19) {
//                System.out.println("만 19세 미만에게는 판매하지 않습니다.");
                throw new AgeLessThan19Exception("만 19세 미만에게는 판매하지 않습니다.");
            } else {
                System.out.println("주문하신 상품 여기 있습니다. ");
            }
        } catch (AgeLessThan19Exception e) {
            System.out.println("조금더 성장한 뒤에 오세요.");
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("모든 예외를 처리합니다.");
        }
    }
}

class AgeLessThan19Exception extends Exception {
    public AgeLessThan19Exception(String message) {
        super(message);
    }
}

 

_07_Throws

package com.sparta.hbd04.prac18;

import java.io.FileWriter;
import java.io.IOException;

public class _07_Throws {
    public static void main(String[] args) {
        try {
            writeFile();
        } catch (IOException e) {
//            throw new RuntimeException(e);
            e.printStackTrace();
            System.out.println("메인메소드에서 해결할게요");
        }
    }

    public static void writeFile() throws IOException {
//        try {
//            FileWriter writer = new FileWriter("test.txt");
//            throw new IOException("파일 쓰기에 실패했어요.");
//        } catch (IOException e) {
////            throw new RuntimeException(e);
//            e.printStackTrace();
//            System.out.println("writeFile 메소드 내에서 자체 해결했어요");
//        }

        FileWriter writer = new FileWriter("test.txt");
        throw new IOException("파일 쓰기에 실패했어요.");
    }
}

 

Prac18 (예외처리 퀴즈 실습)

package com.sparta.hbd04.prac18;

// 에러코드
// 0 (에러 없음), 1 (판매 시간 아님), 2 (매진

// 0 인 경우? 상품 구매를 완료하였습니다.
// 1 인 경우? 상품 구매 가능 시간이 아닙니다. / 상품 구매는 20시부터 가능합니다.
// 2 인 경우? 해당 상품은 매진되었습니다. / 다음 기회에 이용해주세요.

public class Prac18 {
    public static void main(String[] args) {
        int errorCode = 2;
        try {
            if (errorCode == 0) {
                System.out.println("상품 구매를 완료하였습니다.");
            } else if (errorCode == 1) {
                throw new NotOnSaleException("상품 구매 가능 시간이 아닙니다.");
            } else if (errorCode ==2) {
                throw new SoldOutException("해당 상품은 매진되었습니다.");
            }
        } catch (NotOnSaleException e) {
            System.out.println(e.getMessage());
            System.out.println("상품 구매는 20시부터 가능합니다.");
        } catch (SoldOutException e) {
            System.out.println(e.getMessage());
            System.out.println("다음 기회에 이용해 주세요. ");
        }
    }
}

class NotOnSaleException extends Exception {
    public NotOnSaleException(String message) {
        super(message);
    }
}

class SoldOutException extends Exception {
    public SoldOutException(String message) {
        super(message);
    }
}

 

-----------

쓰레드

package com.sparta.hbd04.prac01.prac20;

import com.sparta.hbd04.prac01.prac20.clean.CleanThread;

public class Thread {
    public static void main(String[] args) {
        // 하나의 프로세스 (Process)
        // 쓰레드 (Thread)

        // 1 3 5 7 9
        // 2 4 6 8 10


//        cleanByself();
        CleanThread cleanThread = new CleanThread();
        cleanThread.run(); // 직원 청소
clean.start();

        cleanByBoss(); // 사장 청소
    }

    public static void cleanByself() {
        System.out.println("-- 혼자서 청소 시작 --");
        for (int i = 1; i <= 10; i++) {
            System.out.println("(혼자) " + i + " 번방 청소 중");
        }
        System.out.println("-- 혼자 청소 끝 --");
    }

    public static void cleanByBoss() {
        System.out.println("-- 사장 청소 시작 --");
        for (int i = 1; i <= 10; i += 2) {
            System.out.println("(사장) " + i + " 번방 청소 중");
        }
        System.out.println("-- 사장 청소 끝 --");
    }

}

 

clean pkg > CleanThread

package com.sparta.hbd04.prac01.prac20.clean;

public class CleanThread extends Thread {
    public void run() {
        System.out.println("-- 직원 청소 시작 (Thread) --");
        for (int i = 2; i <= 10; i += 2) {
            System.out.println("(직원) " + i + " 번방 청소 중 (Thread)");
        }
        System.out.println("-- 직원 청소 끝 (Thread) --");
    }
}

 

728x90
반응형

'[개발] - Java > 후발대' 카테고리의 다른 글

예외처리 Quiz  (0) 2023.03.03
후발대 20일차 설명 추가  (0) 2023.03.02
후발대 19일차 설명 추가  (0) 2023.02.24
후발대 19일차 전체 코드  (0) 2023.02.24
과제 2주차 내용 정리  (0) 2023.02.22
Comments
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
250x250