아래 클래스 다이어그램을 참고하여 책을 대여해주는 업체를 위한 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]));
}
}

'Java > 과제' 카테고리의 다른 글
chapter 10) 예외처리example - Exercise11 (0) | 2022.09.02 |
---|---|
chapter 10) 예외처리example - BankAccount 클래스 (0) | 2022.09.02 |
chapter 08) 인터페이스example - BankAccount 클래스 변경 (0) | 2022.08.31 |
chapter 08) 인터페이스example - Shape 클래스 변경하기 (0) | 2022.08.31 |
chapter 07) 상속example - 은행 관련 클래스 확장하기 (0) | 2022.08.29 |