-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.java
More file actions
131 lines (109 loc) · 4.02 KB
/
library.java
File metadata and controls
131 lines (109 loc) · 4.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
import java.util.*;
public class library{
// ===== Book Class =====
static class Book {
int id;
String title;
String author;
boolean isIssued;
Book(int id, String title, String author) {
this.id = id;
this.title = title;
this.author = author;
this.isIssued = false;
}
void display() {
System.out.println(id + " | " + title + " | " + author + " | "
+ (isIssued ? "Issued" : "Available"));
}
}
// ===== Library Class =====
static class Library {
ArrayList<Book> books = new ArrayList<>();
void addBook(int id, String title, String author) {
books.add(new Book(id, title, author));
System.out.println("✅ Book added successfully.");
}
void showBooks() {
if (books.isEmpty()) {
System.out.println("❌ No books in library.");
return;
}
System.out.println("\nID | Title | Author | Status");
for (Book b : books) {
b.display();
}
}
void issueBook(int id) {
for (Book b : books) {
if (b.id == id) {
if (!b.isIssued) {
b.isIssued = true;
System.out.println("✅ Book issued successfully.");
} else {
System.out.println("❌ Book already issued.");
}
return;
}
}
System.out.println("❌ Book not found.");
}
void returnBook(int id) {
for (Book b : books) {
if (b.id == id) {
if (b.isIssued) {
b.isIssued = false;
System.out.println("✅ Book returned successfully.");
} else {
System.out.println("❌ Book was not issued.");
}
return;
}
}
System.out.println("❌ Book not found.");
}
}
// ===== Main Method =====
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Library library = new Library();
while (true) {
System.out.println("\n===== COLLEGE LIBRARY SYSTEM =====");
System.out.println("1. Add Book");
System.out.println("2. Show All Books");
System.out.println("3. Issue Book");
System.out.println("4. Return Book");
System.out.println("5. Exit");
System.out.print("Enter choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.print("Enter Book ID: ");
int id = sc.nextInt();
sc.nextLine();
System.out.print("Enter Book Title: ");
String title = sc.nextLine();
System.out.print("Enter Author Name: ");
String author = sc.nextLine();
library.addBook(id, title, author);
break;
case 2:
library.showBooks();
break;
case 3:
System.out.print("Enter Book ID to issue: ");
library.issueBook(sc.nextInt());
break;
case 4:
System.out.print("Enter Book ID to return: ");
library.returnBook(sc.nextInt());
break;
case 5:
System.out.println("🙏 Thank you for using Library System.");
return;
default:
System.out.println("❌ Invalid choice!");
}
}
}
}