본문 바로가기

Java/과제

chapter 08) 인터페이스example - Book 클래스

아래 클래스 다이어그램을 참고하여 책을 대여해주는 업체를 위한 Book 클래스와 서브클래스들을 만들어 보자.

package practice3;

public abstract class Book {
	private int number;
	private String title;
	private String author;
	private static int countOfBooks = 0;

	public Book(String title, String author) {
		this.title = title;
		this.author = author;

		number = countOfBooks;
		countOfBooks++;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public abstract int getLateFee(int lateDays);

	public boolean equals(Object o) {
		if (this.title.equals(((Book) o).title) && this.author.equals(((Book) o).author)) {
			return true;
		}
		return false;

	}

	public int hashCode() {
		return 0;
	}

	@Override
	public String toString() {
		return String.format("관리번호: %d번, 제목: %s, 작가: %s(일주일 연체료: %,d원) ", number, title, author, getLateFee(7));
	}
}

package practice3;

public class Novel extends Book {

	public Novel(String title, String author) {
		super(title, author);
	}

	@Override
	public int getLateFee(int lateDays) {
		return 300*lateDays;
	}
}
package practice3;

public class Poet extends Book {

	public Poet(String title, String author) {
		super(title, author);
	}

	@Override
	public int getLateFee(int lateDays) {
		return 200*lateDays;
	}
}
package practice3;

public class ScienceFiction extends Book {

	public ScienceFiction(String title, String author) {
		super(title, author);
	}

	@Override
	public int getLateFee(int lateDays) {
		return 600*lateDays;
	}
}
package practice3;

public class BookTest {
	public static void main(String[] args) {
		System.out.println("<소장 도서 목록>");
		Book[] books = new Book[6];
		books[0] = new Novel("칼의 노래", "김훈");
		books[1] = new Novel("칼의 노래", "김훈");
		books[2] = new Poet("이상한 가역반응", "이상");
		books[3] = new Poet("하늘과 바람과 별의 시", "윤동주");
		books[4] = new ScienceFiction("유년기의 끝", "아서 C. 클라크");
		books[5] = new ScienceFiction("스페이스 오디세이", "아서 C. 클라크");
		for(Book b : books) {
			System.out.println(b);
		}
		System.out.println("1번 책과 2번 책은 같은 책인가요?" + books[0].equals(books[1]));
	}
}

Recent Posts
Popular Posts
Tags
더보기
Recent Comments