Skip to content

Commit dd53260

Browse files
third program
1 parent 2f8dc67 commit dd53260

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

BookManager.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import javax.swing.*;
2+
3+
class Book {
4+
private String title;
5+
private String author;
6+
private String isbn;
7+
8+
public Book(String title, String author, String isbn) {
9+
this.title = title;
10+
this.author = author;
11+
this.isbn = isbn;
12+
}
13+
14+
public String getTitle() {
15+
return title;
16+
}
17+
18+
public String getAuthor() {
19+
return author;
20+
}
21+
22+
public String getIsbn() {
23+
return isbn;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return "Book [Title: " + title + ", Author: " + author + ", ISBN: " + isbn + "]";
29+
}
30+
}
31+
32+
public class BookManager {
33+
public static void main(String[] args) {
34+
Book[] books = new Book[100]; // Fixed array for storing books
35+
int bookCount = 0; // Track the number of books added
36+
String menu = "1. Add Book\n2. Remove Book\n3. View All Books\n4. Exit";
37+
38+
while (true) {
39+
String choice = JOptionPane.showInputDialog(menu);
40+
switch (choice) {
41+
case "1": // Add Book
42+
if (bookCount >= books.length) {
43+
JOptionPane.showMessageDialog(null, "Library is full! Cannot add more books.");
44+
break;
45+
}
46+
String title = JOptionPane.showInputDialog("Enter Book Title:");
47+
String author = JOptionPane.showInputDialog("Enter Book Author:");
48+
String isbn = JOptionPane.showInputDialog("Enter Book ISBN:");
49+
books[bookCount++] = new Book(title, author, isbn);
50+
JOptionPane.showMessageDialog(null, "Book added successfully!");
51+
break;
52+
53+
case "2": // Remove Book
54+
String isbnToRemove = JOptionPane.showInputDialog("Enter ISBN of the Book to Remove:");
55+
boolean found = false;
56+
for (int i = 0; i < bookCount; i++) {
57+
if (books[i] != null && books[i].getIsbn().equals(isbnToRemove)) {
58+
books[i] = null; // Mark the book as null
59+
found = true;
60+
break;
61+
}
62+
}
63+
if (found) {
64+
JOptionPane.showMessageDialog(null, "Book removed successfully!");
65+
} else {
66+
JOptionPane.showMessageDialog(null, "Book not found!");
67+
}
68+
break;
69+
70+
case "3": // View All Books
71+
String allBooks = "Books in Library:\n";
72+
boolean hasBooks = false;
73+
for (int i = 0; i < bookCount; i++) {
74+
if (books[i] != null) {
75+
allBooks += books[i].toString() + "\n"; // Concatenate book details
76+
hasBooks = true;
77+
}
78+
}
79+
JOptionPane.showMessageDialog(null, hasBooks ? allBooks : "No books in library.");
80+
break;
81+
82+
case "4": // Exit
83+
System.exit(0);
84+
break;
85+
86+
default:
87+
JOptionPane.showMessageDialog(null, "Invalid choice! Try again.");
88+
}
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)