-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
210 lines (176 loc) · 5.87 KB
/
Copy pathMain.java
File metadata and controls
210 lines (176 loc) · 5.87 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package projectjava;
import java.util.ArrayList;
import java.util.Scanner;
// Book Class
class Book {
private String title;
private String author;
private boolean isAvailable;
public Book(String title, String author) {
this.title = title;
this.author = author;
this.isAvailable = true;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public boolean isAvailable() {
return isAvailable;
}
public void borrowBook() {
if (isAvailable) {
isAvailable = false;
} else {
System.out.println("Book is not available.");
}
}
public void returnBook() {
isAvailable = true;
}
}
// Abstract User Class
abstract class User {
private String name;
private int borrowedBooks;
public User(String name) {
this.name = name;
this.borrowedBooks = 0;
}
public String getName() {
return name;
}
public int getBorrowedBooks() {
return borrowedBooks;
}
public void borrowBook() {
borrowedBooks++;
}
public void returnBook() {
borrowedBooks--;
}
public abstract int getMaxBooks();
}
// Student Class (Inherits from User)
class Student extends User {
private static final int MAX_BOOKS = 3;
public Student(String name) {
super(name);
}
@Override
public int getMaxBooks() {
return MAX_BOOKS;
}
}
// Teacher Class (Inherits from User)..
class Teacher extends User {
private static final int MAX_BOOKS = 5;
public Teacher(String name) {
super(name);
}
@Override
public int getMaxBooks() {
return MAX_BOOKS;
}
}
// Library Class
class Library {
private ArrayList<Book> books = new ArrayList<>();
public void addBook(Book book) {
books.add(book);
}
public void displayAvailableBooks() {
System.out.println("Available Books:");
for (Book book : books) {
if (book.isAvailable()) {
System.out.println(book.getTitle() + " by " + book.getAuthor());
}
}
}
public Book findBook(String title) {
for (Book book : books) {
if (book.getTitle().equalsIgnoreCase(title)) {
return book;
}
}
return null;
}
public void lendBook(User user, String title) {
Book book = findBook(title);
if (book != null && book.isAvailable()) {
if (user.getBorrowedBooks() < user.getMaxBooks()) {
book.borrowBook();
user.borrowBook();
System.out.println(user.getName() + " borrowed " + book.getTitle());
} else {
System.out.println(user.getName() + " cannot borrow more than " + user.getMaxBooks() + " books.");
}
} else {
System.out.println("Book is not available.");
}
}
public void returnBook(User user, String title) {
Book book = findBook(title);
if (book != null && !book.isAvailable()) {
book.returnBook();
user.returnBook();
System.out.println(user.getName() + " returned " + book.getTitle());
} else {
System.out.println("Book was not borrowed.");
}
}
}
// Main Class
public class Main {
public static void main(String[] args) {
Library library = new Library();
// Add some books to the library
library.addBook(new Book("1984", "George Orwell"));
library.addBook(new Book("To Kill a Mockingbird", "Harper Lee"));
library.addBook(new Book("The Great Gatsby", "F. Scott Fitzgerald"));
// Create some users
User student1 = new Student("Alice");
User teacher1 = new Teacher("Mr. Smith");
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\nLibrary Management System:");
System.out.println("1. Display available books");
System.out.println("2. Borrow a book");
System.out.println("3. Return a book");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
switch (choice) {
case 1:
library.displayAvailableBooks();
break;
case 2:
System.out.print("Enter your name (Alice or Mr. Smith): ");
String name = scanner.nextLine();
User user = name.equalsIgnoreCase("Alice") ? student1 : teacher1;
System.out.print("Enter the title of the book you want to borrow: ");
String borrowTitle = scanner.nextLine();
library.lendBook(user, borrowTitle);
break;
case 3:
System.out.print("Enter your name (Alice or Mr. Smith): ");
name = scanner.nextLine();
user = name.equalsIgnoreCase("Alice") ? student1 : teacher1;
System.out.print("Enter the title of the book you want to return: ");
String returnTitle = scanner.nextLine();
library.returnBook(user, returnTitle);
break;
case 4:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 4);
scanner.close();
}
}