Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
231 lines (187 loc) · 6.47 KB
/
Copy pathMain.java
File metadata and controls
231 lines (187 loc) · 6.47 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Library library = new Library();
// Create sample student and book objects
Student student1 = new Student(1, "AYUSh ", "ayushh@gmail.com");
Student student2 = new Student(2, "Vineet ", "vineetfazal@gmail.com");
Book book1 = new Book(101, "Java Programming", "Kamleeshhh");
Book book2 = new Book(102, "Sex Education", "Vaibhav Khare");
Book book3 = new Book(103, "How to do sex in different positions", "Shri Shubham Singh");
// Add students and books to the library
library.addStudent(student1);
library.addStudent(student2);
library.addBook(book1);
library.addBook(book2);
library.addBook(book3);
// Test the functionality
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nStudent Library 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: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline character
switch (choice) {
case 1:
library.displayAvailableBooks();
break;
case 2:
System.out.print("Enter the book title: ");
String title = scanner.nextLine();
Book foundBook = library.findBookByTitle(title);
if (foundBook != null) {
library.borrowBook(student1, foundBook);
} else {
System.out.println("Book with title '" + title + "' not found or not available");
}
break;
case 3:
System.out.print("Enter the book title: ");
title = scanner.nextLine();
foundBook = library.findBookByTitle(title);
if (foundBook != null) {
library.returnBook(student1, foundBook);
} else {
System.out.println("Book with title '" + title + "' not found");
}
break;
case 4:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
scanner.close() ;
}
}}}
class Student {
private int studentID;
private String name;
private String contactInfo;
public Student(int studentID, String name, String contactInfo) {
this.studentID = studentID;
this.name = name;
this.contactInfo = contactInfo;
}
// Getter and setter methods
public int getStudentID() {
return studentID;
}
public void setStudentID(int studentID) {
this.studentID = studentID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContactInfo() {
return contactInfo;
}
public void setContactInfo(String contactInfo) {
this.contactInfo = contactInfo;
}
}
class Book {
private int bookID;
private String title;
private String author;
private boolean available;
public Book(int bookID, String title, String author) {
this.bookID = bookID;
this.title = title;
this.author = author;
this.available = true; // Set book availability to true by default
}
// Getter and setter methods
public int getBookID() {
return bookID;
}
public void setBookID(int bookID) {
this.bookID = bookID;
}
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 boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) {
this.available = available;
}
}
class Library {
private List<Student> students;
private List<Book> books;
public Library() {
this.students = new ArrayList<>();
this.books = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
public void removeStudent(Student student) {
students.remove(student);
}
public void addBook(Book book) {
books.add(book);
}
public void removeBook(Book book) {
books.remove(book);
}
public Book findBookByTitle(String title) {
for (Book book : books) {
if (book.getTitle().equalsIgnoreCase(title) && book.isAvailable()) {
return book;
}
}
return null;
}
public Book findBookByAuthor(String author) {
for (Book book : books) {
if (book.getAuthor().equalsIgnoreCase(author) && book.isAvailable()) {
return book;
}
}
return null;
}
public void displayAvailableBooks() {
System.out.println("Available Books:");
for (Book book : books) {
if (book.isAvailable()) {
System.out.println("ID: " + book.getBookID() + ", Title: " + book.getTitle() + ", Author: " + book.getAuthor());
}
}
}
public void borrowBook(Student student, Book book) {
if (book.isAvailable()) {
book.setAvailable(false);
System.out.println("Book with ID " + book.getBookID() + " has been borrowed by " + student.getName());
} else {
System.out.println("Book with ID " + book.getBookID() + " is not available for borrowing");
}
}
public void returnBook(Student student, Book book) {
if (!book.isAvailable()) {
book.setAvailable(true);
System.out.println("Book with ID " + book.getBookID() + " has been returned by " + student.getName());
} else {
System.out.println("Book with ID " + book.getBookID() + " is already available");
}
}
}