-
Notifications
You must be signed in to change notification settings - Fork 0
/
Book.java
71 lines (58 loc) · 2.2 KB
/
Book.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Abstract class representing a book
abstract class Book {
private String title;
private String author;
private int publicationYear;
// Constructor
public Book(String title, String author, int publicationYear) {
this.title = title;
this.author = author;
this.publicationYear = publicationYear;
}
// Abstract method to be implemented by subclasses
public abstract double calculateLateFee();
// Getters and setters for common attributes
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 int getPublicationYear() {
return publicationYear;
}
public void setPublicationYear(int publicationYear) {
this.publicationYear = publicationYear;
}
// Common method
public void displayInfo() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Publication Year: " + publicationYear);
}
// Main class for testing
public static void main(String[] args) {
// Creating instances of different types of books
FictionBook fictionBook = new FictionBook("The Great Gatsby", "F. Scott Fitzgerald", 1925);
NonFictionBook nonFictionBook = new NonFictionBook("Sapiens: A Brief History of Humankind",
"Yuval Noah Harari", 2011);
ReferenceBook referenceBook = new ReferenceBook("The Elements of Style",
"William Strunk Jr. and E.B. White", 1918);
// Displaying information for each book
System.out.println("Fiction Book:");
fictionBook.displayInfo();
System.out.println("Late Fee: $" + fictionBook.calculateLateFee());
System.out.println("\nNon-Fiction Book:");
nonFictionBook.displayInfo();
System.out.println("Late Fee: $" + nonFictionBook.calculateLateFee());
System.out.println("\nReference Book:");
referenceBook.displayInfo();
System.out.println("Late Fee: $" + referenceBook.calculateLateFee());
}
}