-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.java
More file actions
176 lines (155 loc) · 5.02 KB
/
Copy pathLibrary.java
File metadata and controls
176 lines (155 loc) · 5.02 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
import java.util.Scanner;
class Book {
private int bookId;
private String title;
private String author;
private boolean isAvailable;
public Book(int bookId, String title, String author) {
this.bookId = bookId;
this.title = title;
this.author = author;
this.isAvailable = true;
}
public Book(int bookId, String title) {
this.bookId = bookId;
this.title = title;
this.author = "Unknown";
this.isAvailable = true;
}
public int getBookId() {
return bookId;
}
public String getTitle() {
return title;
}
public boolean isAvailable() {
return isAvailable;
}
public void borrowBook() {
if (isAvailable) {
isAvailable = false;
System.out.println("Book borrowed successfully.");
} else {
System.out.println("Book already borrowed.");
}
}
public void returnBook() {
if (!isAvailable) {
isAvailable = true;
System.out.println("Book returned successfully.");
} else {
System.out.println("Book was not borrowed.");
}
}
public void displayBookDetails() {
System.out.println("---------------------------------");
System.out.println("Book ID : " + bookId);
System.out.println("Title : " + title);
System.out.println("Author : " + author);
System.out.println("Status : " + (isAvailable ? "Available" : "Borrowed"));
}
}
public class Library {
static Scanner sc = new Scanner(System.in);
static Book[] books = new Book[10]; // Fixed size array
static int count = 0;
public static void main(String[] args) {
int choice;
do {
System.out.println("\n====== Library Management System ======");
System.out.println("1. Add Book");
System.out.println("2. Display All Books");
System.out.println("3. Search Book");
System.out.println("4. Borrow Book");
System.out.println("5. Return Book");
System.out.println("6. Exit");
System.out.print("Choose option: ");
choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
addBook();
break;
case 2:
displayBooks();
break;
case 3:
searchBook();
break;
case 4:
borrowBook();
break;
case 5:
returnBook();
break;
case 6:
System.out.println("Exiting system... Goodbye!");
break;
default:
System.out.println("Invalid choice. Try again.");
}
} while (choice != 6);
}
static void addBook() {
if (count >= books.length) {
System.out.println("Library is full. Cannot add more books.");
return;
}
System.out.print("Enter Book ID: ");
int id = sc.nextInt();
sc.nextLine();
System.out.print("Enter Title: ");
String title = sc.nextLine();
System.out.print("Enter Author: ");
String author = sc.nextLine();
books[count] = new Book(id, title, author);
count++;
System.out.println("Book added successfully.");
}
static void displayBooks() {
if (count == 0) {
System.out.println("No books available.");
return;
}
for (int i = 0; i < count; i++) {
books[i].displayBookDetails();
}
}
static void searchBook() {
System.out.print("Enter Book ID to search: ");
int id = sc.nextInt();
boolean found = false;
for (int i = 0; i < count; i++) {
if (books[i].getBookId() == id) {
books[i].displayBookDetails();
found = true;
break;
}
}
if (!found) {
System.out.println("Book not found.");
}
}
static void borrowBook() {
System.out.print("Enter Book ID to borrow: ");
int id = sc.nextInt();
for (int i = 0; i < count; i++) {
if (books[i].getBookId() == id) {
books[i].borrowBook();
return;
}
}
System.out.println("Book not found.");
}
static void returnBook() {
System.out.print("Enter Book ID to return: ");
int id = sc.nextInt();
for (int i = 0; i < count; i++) {
if (books[i].getBookId() == id) {
books[i].returnBook();
return;
}
}
System.out.println("Book not found.");
}
}