MICRO Report
MICRO Report
On
Prepared By
Handa Mayank H.(236200316074) – 4A
Guided By
Lect. Shraddha Parmar
Information Technology Department
Government Polytechnic – Rajkot
Certificate
2362003160
3. 40 Dabhi Gautam m
Package Importing:
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
Class Book {
private String title;
private String author;
private boolean isIssued;
private BookCategory category;
@Override
public String toString() {
return "Title: " + title + ", Author: " + author + ", Category: " + category +
", Issued: " + (isIssued ? "Yes" : "No");
}
public Library() {
books = new ArrayList<>();
loadBooksFromFiles();
}
books.clear();
for (BookCategory category :
BookCategory.values()) {
File categoryFile = new
File(categoriesDir,
category.toString().toLowerCase() +
"_books.txt");
if (categoryFile.exists()) {
try (BufferedReader reader =
new BufferedReader(new
FileReader(categoryFile))) {
String line;
while ((line =
reader.readLine()) != null) {
books.add(new
Book(line));
}
} catch (IOException e) {
System.err.println("Error
loading " + category + " books: " +
e.getMessage());
}
}
}
}
writer.write(book.toFileString());
writer.newLine();
}
}
} catch (IOException e) {
System.err.println("Error
saving books to file: " +
e.getMessage());
}
}
System.out.println("Library data
saved to CATEGORIES package.");
}
System.out.println("Book is already
issued.");
return;
}
}
}
if (!found) {
System.out.println("Book
not found.");
}
} catch
(IllegalArgumentException e) {
System.err.println("Error
issuing book: " + e.getMessage());
}
}
System.out.println("\n=== Books
By Categories ===");
for (BookCategory category :
BookCategory.values()) {
System.out.println("\n--- " +
category + " Books ---");
boolean hasBooks = false;
if (!hasBooks) {
System.out.println("No
books in this category");
}
}
System.out.println("\
n========================");
class Library2 {
public static void main(String[]
args) {
Scanner scanner = new
Scanner(System.in);
Library library = new Library();
while (true) {
System.out.println("\nLibrary
Management System:");
System.out.println("1. Add
Book");
System.out.println("2. Issue
Book");
System.out.println("3. Return
Book");
System.out.println("4. View
Books");
System.out.println("5. Exit");
System.out.print("Enter your
choice: ");
int choice;
try {
choice = scanner.nextInt();
scanner.nextLine();
} catch
(java.util.InputMismatchException e)
{
System.err.println("Invalid
input. Please enter a number between 1
and 5.");
scanner.nextLine();
continue;
}
switch (choice) {
case 1:
System.out.print("Enter
book title: ");
String title =
scanner.nextLine();
System.out.print("Enter
book author: ");
String author =
scanner.nextLine();
System.out.println("Select
book category:");
System.out.println("1.
Python");
System.out.println("2.
Java");
System.out.println("3.
Machine Learning");
System.out.println("4.
Science");
System.out.println("5.
Mathematics");
System.out.print("Enter
category number: ");
int categoryChoice =
scanner.nextInt();
scanner.nextLine();
BookCategory category;
switch (categoryChoice) {
case 1: category =
BookCategory.PYTHON; break;
case 2: category =
BookCategory.JAVA; break;
case 3: category =
BookCategory.MACHINE_LEARNIN
G; break;
case 4: category =
BookCategory.SCIENCE; break;
case 5: category =
BookCategory.MATHEMATICS;
break;
default:
System.out.println("Invalid category.
Setting to PYTHON.");
category =
BookCategory.PYTHON;
}
try {
library.addBook(new
Book(title, author, category));
} catch
(EmptyBookTitleException |
EmptyBookAuthorException e) {
System.out.println("Error: " +
e.getMessage());
}
break;
case 2:
System.out.print("Enter
book title to issue: ");
String issueTitle =
scanner.nextLine();
library.issueBook(issueTitle);
break;
case 3:
System.out.print("Enter
book title to return: ");
String returnTitle =
scanner.nextLine();
library.returnBook(returnTitle);
break;
case 4:
library.viewBooks();
break;
case 5:
System.out.println("Exiting...");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice.
Please enter a number between 1 and
5.");
}
}
}
}
Output
D:\it\Java\micro>javac Library2.java
D:\it\Java\micro>java Library2
========================
Issueing Books :
Returning Books :
Thank You