-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibraryManagementSystem.java
131 lines (107 loc) · 3.18 KB
/
LibraryManagementSystem.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.util.*;
abstract class LibraryItem{
private String itemId;
private String title;
private String author;
LibraryItem(String itemId, String title, String author){
this.itemId = itemId;
this.title = title;
this.author = author;
}
public String getItemId() {
return itemId;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public void getItemDetails() {
System.out.println("ID: " + itemId + ", Title: " + title + ", Author: " + author);
}
public abstract int getLoanDuration();
}
interface Reservable {
void reserveItem();
boolean checkAvailability();
}
// Subclass: Book
class Book extends LibraryItem implements Reservable {
public Book(String itemId, String title, String author) {
super(itemId, title, author);
}
@Override
public int getLoanDuration() {
return 14; // Books can be borrowed for 14 days
}
@Override
public void reserveItem() {
System.out.println("Book reserved: " + getTitle());
}
@Override
public boolean checkAvailability() {
return true; // Assuming books are always available for now
}
}
// Subclass: Magazine
class Magazine extends LibraryItem {
public Magazine(String itemId, String title, String author) {
super(itemId, title, author);
}
@Override
public int getLoanDuration() {
return 7; // Magazines can be borrowed for 7 days
}
}
// Subclass: DVD
class DVD extends LibraryItem implements Reservable {
public DVD(String itemId, String title, String author) {
super(itemId, title, author);
}
@Override
public int getLoanDuration() {
return 3; // DVDs can be borrowed for 3 days
}
@Override
public void reserveItem() {
System.out.println("DVD reserved: " + getTitle());
}
@Override
public boolean checkAvailability() {
return false; // Assuming DVDs are currently unavailable
}
}
public class LibraryManagementSystem {
public static void main(String[] args) {
List<LibraryItem> items = new ArrayList<>();
Book book = new Book("101", "Java", "James Gosling");
Magazine magazine = new Magazine("202", "India Tomorrow", "John");
DVD dvd = new DVD("303", "Inception", "Christopher Nolan");
items.add(book);
items.add(magazine);
items.add(dvd);
for (LibraryItem item : items) {
item.getItemDetails();
System.out.println("Loan Duration: " + item.getLoanDuration() + " days");
if (item instanceof Reservable) {
((Reservable) item).reserveItem();
System.out.println("Available: " + ((Reservable) item).checkAvailability());
}
System.out.println();
}
}
}
//SampleOutput
//ID: 101, Title: Java, Author: James Gosling
//Loan Duration: 14 days
//Book reserved: Java
//Available: true
//
//ID: 202, Title: India Tomorrow, Author: John
//Loan Duration: 7 days
//
//ID: 303, Title: Inception, Author: Christopher Nolan
//Loan Duration: 3 days
//DVD reserved: Inception
//Available: false