A Microproject Report
On
“Library Management System”
Prepared By
Handa Mayank H.(236200316074) – 4A
Gondaliya Hiten D. (236200316069)-4A
Dabhi Gautam M.(236200316040)-4A
Guided By
Lect. Shraddha Parmar
Information Technology Department
Government Polytechnic – Rajkot
In partial fulfilment of requirement for
Information technology Department
Object Oriented Programming with JAVA(4341602)
Semester: 4th
GOVERNMENT POLYTECHNIC – RAJKOT
INFORMATION TECHNOLOGY DEPARTMENT
Certificate
This is to certify that below students of fourth semester of diploma in
Information Technology of the Institute Government Polytechinc –
Rajkot has successfully completed the microproject in Object
Oriented Programming with Java (4341602) for the academic year
2024-25.
Sr Er No Name Class-
No Div
1. 2362003160 Handa Mayank h IT-A
74
IT-A
2. 2362003160 Gondaliya Hiten
69 d IT-A
2362003160
3. 40 Dabhi Gautam m
Place: G.P. Rajkot (Subject Faculty)
Lect. Shraddha
Date:-
Parmar
Microproject Code
Package Importing:
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
Create User Define Exception:
class EmptyBookTitleException extends Exception {
public EmptyBookTitleException() {
super("Please add book title");
}
}
class EmptyBookAuthorException extends Exception {
public EmptyBookAuthorException() {
super("Please add book author");
}
}
Use of enum Class:
enum BookCategory {
PYTHON,
JAVA,
MACHINE_LEARNING,
SCIENCE,
MATHEMATICS
}
Create Class for Books:
Class Book {
private String title;
private String author;
private boolean isIssued;
private BookCategory category;
public Book(String title, String author, BookCategory category) throws
EmptyBookTitleException, EmptyBookAuthorException {
if (title == null || title.trim().isEmpty()) {
throw new EmptyBookTitleException();
}
if (author == null || author.trim().isEmpty()) {
throw new EmptyBookAuthorException();
}
this.title = title;
this.author = author;
this.category = category;
this.isIssued = false;
}
// Constructor to create a Book object from a line in the file
public Book(String line) {
String[] parts = line.split(",");
this.title = parts[0].trim();
this.author = parts[1].trim();
this.category = BookCategory.valueOf(parts[2].trim());
this.isIssued = Boolean.parseBoolean(parts[3].trim());
}
public BookCategory getCategory() {
return category;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public boolean isIssued() {
return isIssued;
}
public void issueBook() {
this.isIssued = true;
}
public void returnBook() {
this.isIssued = false;
}
@Override
public String toString() {
return "Title: " + title + ", Author: " + author + ", Category: " + category +
", Issued: " + (isIssued ? "Yes" : "No");
}
public String toFileString() {
return title + "," + author + "," + category + "," + isIssued;
}
}
Create Class For
Library:
class Library {
private ArrayList<Book> books;
//private static final String
FILENAME = "library_data.txt";
public Library() {
books = new ArrayList<>();
loadBooksFromFiles();
}
private void loadBooksFromFiles()
{
File categoriesDir = new
File("CATEGORIES/");
if (!categoriesDir.exists()) {
return;
}
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());
}
}
}
}
public void saveBooksToFiles() {
File categoriesDir = new
File("CATEGORIES/");
if (!categoriesDir.exists()) {
categoriesDir.mkdirs();
}
for (BookCategory category :
BookCategory.values()) {
try (BufferedWriter writer =
new BufferedWriter(new FileWriter(
"CATEGORIES/" +
category.toString().toLowerCase() +
"_books.txt"))) {
for (Book book : books) {
if (book.getCategory() ==
category) {
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.");
}
public void addBook(Book book) {
books.add(book);
System.out.println("Book added:
" + book.getTitle());
saveBooksToFiles();
}
public void issueBook(String title) {
try {
if (title == null ||
title.trim().isEmpty()) {
throw new
IllegalArgumentException("Book title
cannot be empty.");
}
boolean found = false;
for (Book book : books) {
if
(book.getTitle().equalsIgnoreCase(title
)) {
found = true;
if (!book.isIssued()) {
book.issueBook();
System.out.println("Book issued: " +
book.getTitle());
saveBooksToFiles();
return;
} else {
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());
}
}
public void returnBook(String title)
{
try {
if (title == null ||
title.trim().isEmpty()) {
throw new
IllegalArgumentException("Book title
cannot be empty.");
}
boolean found = false;
for (Book book : books) {
if
(book.getTitle().equalsIgnoreCase(title
)) {
found = true;
if (book.isIssued()) {
book.returnBook();
System.out.println("Book returned: " +
book.getTitle());
saveBooksToFiles();
return;
} else {
System.out.println("Book was not
issued.");
return;
}
}
}
if (!found) {
System.out.println("Book
not found.");
}
} catch
(IllegalArgumentException e) {
System.err.println("Error
returning book: " + e.getMessage());
}
}
public void viewBooks() {
if (books.isEmpty()) {
System.out.println("No books
available in the library.");
return;
}
System.out.println("\n=== Books
By Categories ===");
for (BookCategory category :
BookCategory.values()) {
System.out.println("\n--- " +
category + " Books ---");
boolean hasBooks = false;
for (Book book : books) {
if (book.getCategory() ==
category) {
System.out.println(book);
hasBooks = true;
}
}
if (!hasBooks) {
System.out.println("No
books in this category");
}
}
System.out.println("\
n========================");
Create Main Class:
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
Add book in Package:
D:\it\Java\micro>javac Library2.java
D:\it\Java\micro>java Library2
Library Management System:
1. Add Book
2. Issue Book
3. Return Book
4. View Books
5. Exit
Enter your choice: 1
Enter book title: Python
Enter book author: Mayank
Select book category:
1. Python
2. Java
3. Machine Learning
4. Science
5. Mathematics
Enter category number: 1
Book added: Python
Library data saved to CATEGORIES package.
Library Management System:
1. Add Book
2. Issue Book
3. Return Book
4. View Books
5. Exit
Enter your choice: 1
Enter book title: Java
Enter book author: Hiten
Select book category:
1. Python
2. Java
3. Machine Learning
4. Science
5. Mathematics
Enter category number: 2
Book added: Java
Library data saved to CATEGORIES package.
Library Management System:
1. Add Book
2. Issue Book
3. Return Book
4. View Books
5. Exit
Enter your choice: 1
Enter book title: Machine Learning
Enter book author: Gautam
Select book category:
1. Python
2. Java
3. Machine Learning
4. Science
5. Mathematics
Enter category number: 3
Book added: Machine Learning
Library data saved to CATEGORIES package.
Retrive Books From Package:
Library Management System:
1. Add Book
2. Issue Book
3. Return Book
4. View Books
5. Exit
Enter your choice: 4
=== Books By Categories ===
--- PYTHON Books ---
Title: python, Author: s, Category: PYTHON, Issued: Yes
Title: Python, Author: Mayank, Category: PYTHON, Issued: No
--- JAVA Books ---
Title: java, Author: s, Category: JAVA, Issued: No
Title: Java, Author: Hiten, Category: JAVA, Issued: No
--- MACHINE_LEARNING Books ---
Title: machine learning, Author: kk, Category: MACHINE_LEARNING,
Issued: No
Title: Machine Learning, Author: Gautam, Category: MACHINE_LEARNING,
Issued: No
--- SCIENCE Books ---
Title: science, Author: b, Category: SCIENCE, Issued: No
--- MATHEMATICS Books ---
Title: maths, Author: rp, Category: MATHEMATICS, Issued: No
========================
Issueing Books :
Library Management System:
1. Add Book
2. Issue Book
3. Return Book
4. View Books
5. Exit
Enter your choice: 2
Enter book title to issue: Java
Book issued: java
Library data saved to CATEGORIES
package.
Library Management System:
1. Add Book
2. Issue Book
3. Return Book
4. View Books
5. Exit
Enter your choice: 4
=== Books By Categories ===
--- PYTHON Books ---
Title: python, Author: s, Category:
PYTHON, Issued: Yes
Title: Python, Author: Mayank,
Category: PYTHON, Issued: No
--- JAVA Books ---
Title: java, Author: s, Category: JAVA,
Issued: Yes
Title: Java, Author: Hiten, Category:
JAVA, Issued: No
--- MACHINE_LEARNING Books ---
Title: machine learning, Author: kk,
Category: MACHINE_LEARNING,
Issued: No
Title: Machine Learning, Author:
Gautam, Category:
MACHINE_LEARNING, Issued: No
--- SCIENCE Books ---
Title: science, Author: b, Category:
SCIENCE, Issued: No
--- MATHEMATICS Books ---
Title: maths, Author: rp, Category:
MATHEMATICS, Issued: No
Returning Books :
Library Management System:
1. Add Book
2. Issue Book
3. Return Book
4. View Books
5. Exit
Enter your choice: 3
Enter book title to return: python
Book returned: python
Library data saved to CATEGORIES
package.
Library Management System:
1. Add Book
2. Issue Book
3. Return Book
4. View Books
5. Exit
Enter your choice: 4
=== Books By Categories ===
--- PYTHON Books ---
Title: python, Author: s, Category:
PYTHON, Issued: No
Title: Python, Author: Mayank,
Category: PYTHON, Issued: No
--- JAVA Books ---
Title: java, Author: s, Category: JAVA,
Issued: Yes
Title: Java, Author: Hiten, Category:
JAVA, Issued: No
--- MACHINE_LEARNING Books ---
Title: machine learning, Author: kk,
Category: MACHINE_LEARNING,
Issued: No
Title: Machine Learning, Author:
Gautam, Category:
MACHINE_LEARNING, Issued: No
--- SCIENCE Books ---
Title: science, Author: b, Category:
SCIENCE, Issued: No
--- MATHEMATICS Books ---
Title: maths, Author: rp, Category:
MATHEMATICS, Issued: No
Entering Null Value In
Author :
Library Management System:
1. Add Book
2. Issue Book
3. Return Book
4. View Books
5. Exit
Enter your choice: 1
Enter book title: science
Enter book author:
Select book category:
1. Python
2. Java
3. Machine Learning
4. Science
5. Mathematics
Enter category number:
4
Error: Please add book author
Library Management System:
1. Add Book
2. Issue Book
3. Return Book
4. View Books
5. Exit
Enter your choice: 1
Enter book title: maths
Enter book author:
Select book category:
1. Python
2. Java
3. Machine Learning
4. Science
5. Mathematics
Enter category number: 5
Error: Please add book author
Entering Null Value In Title :
Library Management System:
1. Add Book
2. Issue Book
3. Return Book
4. View Books
5. Exit
Enter your choice: 1
Enter book title:
Enter book author: Null
Select book category:
1. Python
2. Java
3. Machine Learning
4. Science
5. Mathematics
Enter category number: 2
Error: Please add book title
Exiting From Code :
Library Management System:
1. Add Book
2. Issue Book
3. Return Book
4. View Books
5. Exit
Enter your choice: 5
Exiting...
Thank You