top of page

Java Programs Examples with Output - Designing Library Management System


Java Programs Examples with Output

Create a Library Management System using core Java programming concepts. This project will involve designing and implementing classes to represent books, library members, and transactions. The system should allow users to add and remove books, check in and check out books, and manage member information.


Below is a simplified example of a Library Management System in Java. The code includes model classes for Book, Member, and Transaction, as well as a Library class to manage the interactions.


// Book class

class Book {

private String title;

private String author;

private String ISBN;

private boolean available;

// Constructor

public Book(String title, String author, String ISBN) {

this.title = title;

this.author = author;

this.ISBN = ISBN;

this.available = true;

}

// Getters and setters

public String getTitle() {

return title;

}

public String getAuthor() {

return author;

}

public String getISBN() {

return ISBN;

}

public boolean isAvailable() {

return available;

}

public void setAvailable(boolean available) {

this.available = available;

}

}


// Member class

class Member {

private String name;

private int memberId;

// Constructor

public Member(String name, int memberId) {

this.name = name;

this.memberId = memberId;

}

// Getters

public String getName() {

return name;

}

public int getMemberId() {

return memberId;

}

}


Core Java Programming

Java Back-End Development


// Transaction class

class Transaction {

private Book book;

private Member member;

private String transactionType; // "CheckOut" or "CheckIn"

// Constructor

public Transaction(Book book, Member member, String transactionType) {

this.book = book;

this.member = member;

this.transactionType = transactionType;

}

// Getters

public Book getBook() {

return book;

}

public Member getMember() {

return member;

}

public String getTransactionType() {

return transactionType;

}

}


// Library class

class Library {

private List<Book> books;

private List<Member> members;

private List<Transaction> transactions;

// Constructor

public Library() {

this.books = new ArrayList<>();

this.members = new ArrayList<>();

this.transactions = new ArrayList<>();

}

// Methods for managing books, members, and transactions

public void addBook(Book book) {

books.add(book);

}

public void removeBook(Book book) {

books.remove(book);

}

public void registerMember(Member member) {

members.add(member);

}

public void checkoutBook(Book book, Member member) {

if (book.isAvailable()) {

book.setAvailable(false);

transactions.add(new Transaction(book, member, "CheckOut"));

System.out.println("Book checked out successfully.");

} else {

System.out.println("Sorry, the book is not available for checkout.");

}

}

public void checkinBook(Book book, Member member) {

if (!book.isAvailable()) {

book.setAvailable(true);

transactions.add(new Transaction(book, member, "CheckIn"));

System.out.println("Book checked in successfully.");

} else {

System.out.println("This book is already available in the library.");

}

}

public void displayBooks() {

System.out.println("Books in the library:");

for (Book book : books) {

System.out.println("Title: " + book.getTitle() + ", Author: " + book.getAuthor() +

", ISBN: " + book.getISBN() + ", Available: " + book.isAvailable());

}

}

public void displayMembers() {

System.out.println("Library members:");

for (Member member : members) {

System.out.println("Name: " + member.getName() + ", Member ID: " + member.getMemberId());

}

}

// Additional methods as needed

}


// Main program

public class Main {

public static void main(String[] args) {

// Sample usage of the Library Management System

// Create books

Book book1 = new Book("The Catcher in the Rye", "J.D. Salinger", "978-0-316-76948-0");

Book book2 = new Book("To Kill a Mockingbird", "Harper Lee", "978-0-06-112008-4");

// Create members

Member member1 = new Member("John Doe", 1);

Member member2 = new Member("Jane Smith", 2);

// Create a library

Library library = new Library();

// Add books to the library

library.addBook(book1);

library.addBook(book2);

// Register members

library.registerMember(member1);

library.registerMember(member2);

// Display available books and library members

library.displayBooks();

library.displayMembers();

// Check out a book

library.checkoutBook(book1, member1);

// Display updated book status

library.displayBooks();

// Check in the book

library.checkinBook(book1, member1);

// Display updated book status

library.displayBooks();

}

}


This code provides a basic structure for a Library Management System. It includes classes for books, members, transactions, and the library itself, along with a simple main program demonstrating how to use the system. Feel free to enhance and modify it based on your specific requirements.


41 views0 comments

Comments


bottom of page