다중 상속이 가능한 인터페이스를 활용하여 도서 관리 프로그램을 작성해 보자
Lendable 인터페이스
CheckIn, CheckOut 함수를 추상 메소드로 가지는 대출가능 관리 인터페이스 Lendable 선언
public interface Lendable {
final static byte BORROWED = 1; //상수지정
final static byte NORMAL = 0;
abstract void checkOut(String borrower, String date) throws Exception;
//abstract 생략가능, 추상메소드에는 public 생략가능
abstract void checkIn();
}
CDInfo 클래스
CD 정보를 가지는 클래스
public class CDInfo {
String regNo; //관리번호
String title;
CDInfo(String reqNo, String title){
this.regNo = reqNo;
this.title = title;
}
}
SepVolume 클래스
인터페이스 Lendable을 구현하는 단행본 관리 SepVolum 클래스 작성
public class SepVolume implements Lendable{
String reqNo; //청구번호
String bookTitle; //제목
String writer; //저자
String borrower; //대출인
String checkOutDate; //대출일
byte state; //대출상태
public SepVolume(String reqNo, String bookTitle, String writer) {
this.reqNo = reqNo;
this.bookTitle = bookTitle;
this.writer = writer;
}
public void checkOut(String borrower, String date) throws Exception {
//추상메소드의 접근지정자보다 같거나 커야함
//public > protected > package(선언 안함) > private
if (state != NORMAL)
throw new Exception("* 대출불가:" + bookTitle);
this.borrower = borrower;
this.checkOutDate = date;
this.state = BORROWED;
System.out.println("*" + bookTitle + "(이)가 대출됨");
System.out.println("대출인: " + borrower);
System.out.println("대출일자: " + date);
}
public void checkIn() {
this.borrower = borrower;
this.checkOutDate = null;
this.state = NORMAL;
System.out.println("*" + bookTitle + "(이)가 반납됨\n");
}
}
AppCDInfo 클래스
CD 클래스 ( CDInfo ) 를 상속하면서 동시에 Lendable 인터페이스를 구현하는 부록 CD클래스 AppCDInfo 작성
public class AppCDInfo extends CDInfo implements Lendable{
String bor; //대출인
String date; //대출일
byte state; //대출상태
AppCDInfo(String regNo, String title){
super(regNo, title); //CDInfo 생성자
}
@Override
public void checkOut(String borrower, String date) throws Exception {
if(state != NORMAL)
throw new Exception("* 대출불가: " + title);
this.bor = borrower;
this.date = date;
this.state = BORROWED;
System.out.println("*" + title + " CD(이)가 대출됨.");
System.out.println("대출인: " + bor);
System.out.println("대출일자: " + date);
}
@Override
public void checkIn() {
this.bor = null;
this.date = null;
this.state = NORMAL;
System.out.println("*" + title + " CD(이)가 반납됨.");
}
}
Main1
단행본 관리 클래스 SepVolume만 활용하여 작성한 도서 관리 프로그램
public class InterExam01 {
public static void main(String[] args) {
SepVolume obj1 = new SepVolume("863ㅂ774개", "개미", "베르베르");
try {
obj1.checkOut("김영숙", "20060315");
obj1.checkIn();
} catch (Exception e){
System.out.println(e.getMessage());
}
AppCDInfo obj2 = new AppCDInfo("2005-7001", "Redhat Fedora");
try {
obj2.checkOut("박희경", "20060317");
obj2.checkIn();
} catch (Exception e){
System.out.println(e.getMessage());
}
}
}
Main2
단행본 관리 클래스 SepVolume과 부록 CD 관리 클래스 AppCDInfo를 모두 활용하여 작성한 도서 관리 프로그램
public class InterExam02 {
public static void main(String[] args) {
Lendable[] arr = new Lendable[3];
arr[0] = new SepVolume("863ㅇ", "푸코의 진자", "에코");
arr[1]= new SepVolume("609.2", "서양 미술사", "곰브리치");
arr[2] = new AppCDInfo("02-17", "XML을 위한 자바");
checkOutAll(arr, "abc", "20221110");
}
static void checkOutAll(Lendable[] obj, String borrower, String date) {
for(int i=0; i<obj.length; i++) {
try {
obj[i].checkOut(borrower, date);
} catch (Exception e){
System.out.println(e.getMessage());
}
}
}
}
'Java' 카테고리의 다른 글
[Java] 제네릭 프로그래밍, ArrayList (0) | 2022.12.18 |
---|---|
[Java] 패키지 (0) | 2022.12.04 |
[Java] Interface (0) | 2022.12.04 |
[Java] 상속 - 메시지 전송 프로그램 (0) | 2022.12.04 |
[Java] 상속 - 계좌 프로그램 (0) | 2022.12.02 |