직업은 선물 트레이더

[과제]JAVA. 미디어 대여 시뮬레이션

잊어버린 과거

컴퓨터공학과 2010151035 장용하 // 4월 16일 제출




선코드 후실행

<<class InterfaceExample>>


public class InterfaceExample {


        public static void main(String[] args) {


                Lendable arr[] = new Lendable[2];

                arr[0] = new SeparateVolume("883ㅇ326ㅂ2", "옆집착한쥐", "동물");

                arr[1] = new AppCDInfo("2002-1742", "XML을 위한 옆집쥐 강좌");

                // ㄴ Lendable 변수배열에 각각의 객체를 항당받고 있다.

                

                checkOutAll(arr, "우리집쥐", "20120421");

                checkInAll(arr);

                checkInAll(arr);

                // ㄴ 오류 발생지점. 이 때 함수 내부의 try catch문이 오류를 가만히 두지 않는다.

                // "~은 이미 ~입니다" 라는 말이 나와 Exception이 던져졌음을 알 수 있다.

        }

        static void checkOutAll(Lendable arr[], String borrower, String date) {

                for(int cnt = 0; cnt < arr.length; cnt++)

                        try {

                                arr[cnt].checkOut(borrower, date);

                        }

                        catch(Exception e) {

                                String msg = e.getMessage();

                                System.out.println(msg);

                        }                       

                        // ㄴ try catch문을 throw를 하고있는 checkOUt, checkIn 함수에 직접적으로 매치시켰다.    

        }


        static void checkInAll(Lendable arr[]) {

                for(int cnt = 0; cnt < arr.length; cnt++)

                        try {

                                arr[cnt].checkIn();

                        }

                        catch(Exception e) {

                                String msg = e.getMessage();

                                System.out.println(msg);

                        }                               

        }

        // ㄴ checkOutAll 과 checkInAll은 빌린것을 모두 반납하거나, 있는 책이나 CD를 모두 빌려오는 역할을 한다.

}

 

 

<<class CDInfo>>


class CDInfo {

        String registerNo;

        String title;

        CDInfo(String registerNo, String title) {

                this.registerNo = registerNo;

                this.title = title;

        }


}

 

 

 

<<interface Lendable>>


interface Lendable {

        final static byte STATE_BORROWED = 1;

        final static byte STATE_NORMAL = 0;

        //ㄴ STATE 변수에 가독성을 위한 1과 0을 할당. final static으로 변경과 재호출을 막고있다.


        abstract void checkOut(String borrower, String date) throws Exception;

        abstract void checkIn() throws Exception;

        // ㄴ Exception에 대한 처리를 할 예정인 두 메소드

}

 

 

 

<<class AppCDInfo>>


class AppCDInfo extends CDInfo implements Lendable {

                                // ㄴ Lendable 인터페이스를 구현하고있는 클래스의 모습.

        String borrower;

        String checkOutDate;

        byte state;

        AppCDInfo(String registerNo, String title) {

                super(registerNo, title);

        }


        public void checkOut(String borrower, String date) throws Exception {

                if(state != STATE_NORMAL)

                        throw new Exception(title + "CD 가 <<이미>> 대출되었습니다.");

                // 오류가 있을시 throw new Exception으로 Exception을 발생한다.

                

                this.borrower = borrower;

                this.checkOutDate = date;

                this.state = STATE_BORROWED;

                System.out.println("*" + title + "CD가 대출되었습니다.");

                System.out.println("대출인 : " + borrower);

                System.out.println("대출일 : " + date + "\n");

        }


        public void checkIn() throws Exception {

                if(state != STATE_BORROWED)

                        throw new Exception(title + "CD 가 <<이미>> 반납되었습니다.");

                this.borrower = null;

                this.checkOutDate = null;

                this.state = STATE_NORMAL;

                System.out.println("*" + title + "CD가 반납되었습니다. \n");


        }


}

 

<<class SeparateVolume>>


class SeparateVolume implements Lendable {

        

        String requestNo;

        String bookTitle;

        String writer;

        String borrower;

        String checkOutDate;

        byte state;

        SeparateVolume(String requestNo, String bookTitle, String writer) {

                this.requestNo = requestNo;

                this.bookTitle = bookTitle;

                this.writer = writer;

        }


        public void checkOut(String borrower, String date) throws Exception {

                if(state != STATE_NORMAL)

                        throw new Exception(bookTitle + "~ 이(가) <<이미>> 대출되었습니다.");

                

                this.borrower = borrower;

                this.checkOutDate = date;

                this.state = STATE_BORROWED;

                System.out.println("*" + bookTitle + " 이(가) 대출되었습니다.");

                System.out.println("대출인 : " + borrower);

                System.out.println("대출일 : " + date + "\n");


        }


        public void checkIn() throws Exception {

                if(state != STATE_BORROWED)

                        throw new Exception(bookTitle + "이(가) <<이미>> 반납되었습니다.");

                

                this.borrower = null;

                this.checkOutDate = null;

                this.state = STATE_NORMAL;

                System.out.println("*" + bookTitle + "이(가) 반납되었습니다. \n");

        }


}

 

 

 

자바. 책, CD 대여