-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBook.java
138 lines (119 loc) · 4.48 KB
/
Book.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
132
133
134
135
136
137
138
import java.util.Scanner;
import java.util.ArrayList;
class Book {
private String title;
private String author;
private String ISBN;
private boolean isAvailable;
public Book(String title, String author, String ISBN) {
this.title = title;
this.author = author;
this.ISBN = ISBN;
this.isAvailable = true; // New books are initially available
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getISBN() {
return ISBN;
}
public boolean isAvailable() {
return isAvailable;
}
public void borrowBook() {
isAvailable = false;
}
public void returnBook() {
isAvailable = true;
}
@Override
public String toString() {
return "Title: " + title + ", Author: " + author + ", ISBN: " + ISBN + ", Available: " + (isAvailable ? "Yes" : "No");
}
}
class Library {
private ArrayList<Book> inventory = new ArrayList<>();
public void addBook(String title, String author, String ISBN) {
inventory.add(new Book(title, author, ISBN));
}
public void borrowBook(String ISBN) {
for (Book book : inventory) {
if (book.getISBN().equals(ISBN)) {
if (book.isAvailable()) {
book.borrowBook();
System.out.println("You have successfully borrowed the book.");
return;
} else {
System.out.println("Sorry, the book is currently unavailable.");
return;
}
}
}
System.out.println("No book found with the provided ISBN.");
}
public void returnBook(String ISBN) {
for (Book book : inventory) {
if (book.getISBN().equals(ISBN)) {
if (!book.isAvailable()) {
book.returnBook();
System.out.println("You have successfully returned the book.");
return;
} else {
System.out.println("You cannot return a book that you didn't borrow.");
return;
}
}
}
System.out.println("No book found with the provided ISBN.");
}
public void displayAvailableBooks() {
System.out.println("Available Books:");
for (Book book : inventory) {
if (book.isAvailable()) {
System.out.println(book);
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Library library = new Library();
library.addBook("The Great Gatsby", "Amit Garg", "9780743273565");
library.addBook("Irrfan Khan: A Life in Movies", "Shubhra Gupta", "9780061120084");
library.addBook("Ambedkar: A Life", "Shashi Tharoor", "9780451524935");
library.addBook("Jadunama", "Javed Akhtar", "9780451327356");
while (true) {
System.out.println("\nLibrary Management System Menu:");
System.out.println("1. Borrow a Book");
System.out.println("2. Return a Book");
System.out.println("3. View Available Books");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
switch (choice) {
case 1:
System.out.print("Enter the ISBN of the book you want to borrow: ");
String borrowISBN = scanner.nextLine();
library.borrowBook(borrowISBN);
break;
case 2:
System.out.print("Enter the ISBN of the book you want to return: ");
String returnISBN = scanner.nextLine();
library.returnBook(returnISBN);
break;
case 3:
library.displayAvailableBooks();
break;
case 4:
System.out.println("Exiting Library Management System. Goodbye!");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please select a valid option.");
}
}
}
}