library managent
library managent
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// Abstract class
abstract class LibraryItem {
// Private fields for encapsulation
private String title;
private String author;
private String isbn;
private boolean isBorrowed;
// Constructor
public LibraryItem(String title, String author, String isbn) {
this.title = title;
this.author = author;
this.isbn = isbn;
this.isBorrowed = false;
}
void purchase();
}
// Constructor
public Book(String title, String author, String isbn, double price) {
super(title, author, isbn);
this.price = price;
}
@Override
public void purchase() {
if (isBorrowed()) {
System.out.println("Book \"" + getTitle() + "\" is already borrowed
and cannot be purchased.");
} else {
System.out.println("You purchased \"" + getTitle() + "\" by " +
getAuthor() + ". Total Cost: Rs " + price);
}
}
// Override displayDetails
@Override
public void displayDetails() {
System.out.println("Type: Book, Title: " + getTitle() + ", Author: " +
getAuthor() + ", ISBN: " + getIsbn() +
", Price: Rs " + price + ", Borrowed: " + (isBorrowed() ? "Yes" :
"No"));
}
}
// Constructor
public Magazine(String title, String author, String isbn, String
publicationDate) {
super(title, author, isbn);
this.publicationDate = publicationDate;
}
// Override displayDetails
@Override
public void displayDetails() {
System.out.println("Type: Magazine, Title: " + getTitle() + ", Author: "
+ getAuthor() + ", ISBN: " + getIsbn() +
", Publication Date: " + publicationDate + ", Borrowed: " +
(isBorrowed() ? "Yes" : "No"));
}
}
// Constructor
public Journal(String title, String author, String isbn, String fieldOfStudy)
{
super(title, author, isbn);
this.fieldOfStudy = fieldOfStudy;
}
// Override displayDetails
@Override
public void displayDetails() {
System.out.println("Type: Journal, Title: " + getTitle() + ", Author: " +
getAuthor() + ", ISBN: " + getIsbn() +
", Field of Study: " + fieldOfStudy + ", Borrowed: " +
(isBorrowed() ? "Yes" : "No"));
}
}
// Library class
class Library {
private List<LibraryItem> items;
public Library() {
items = new ArrayList<>();
}
// Main class
public class LibraryManagementSystem {
public static void main(String[] args) {
Library library = new Library();
Scanner scanner = new Scanner(System.in);
while (true) {
try {
System.out.println("\nLibrary System Menu:");
System.out.println("1. Add a Book");
System.out.println("2. Add a Magazine");
System.out.println("3. Add a Journal");
System.out.println("4. Display All Items");
System.out.println("5. Borrow an Item");
System.out.println("6. Return an Item");
System.out.println("7. Remove an Item");
System.out.println("8. Exit");
System.out.print("Enter your choice: ");
switch (choice) {
case 1:
System.out.print("Enter Book Title: ");
String bookTitle = scanner.nextLine();
System.out.print("Enter Book Author: ");
String bookAuthor = scanner.nextLine();
System.out.print("Enter Book ISBN: ");
String bookIsbn = scanner.nextLine();
System.out.print("Enter Book Price: ");
double bookPrice = scanner.nextDouble();
library.addItem(new Book(bookTitle, bookAuthor, bookIsbn,
bookPrice));
System.out.println("Book added successfully!");
break;
case 2:
System.out.print("Enter Magazine Title: ");
String magTitle = scanner.nextLine();
System.out.print("Enter Magazine Author: ");
String magAuthor = scanner.nextLine();
System.out.print("Enter Magazine ISBN: ");
String magIsbn = scanner.nextLine();
System.out.print("Enter Publication Date: ");
String pubDate = scanner.nextLine();
library.addItem(new Magazine(magTitle, magAuthor,
magIsbn, pubDate));
System.out.println("Magazine added successfully!");
break;
case 3:
System.out.print("Enter Journal Title: ");
String journalTitle = scanner.nextLine();
System.out.print("Enter Journal Author: ");
String journalAuthor = scanner.nextLine();
System.out.print("Enter Journal ISBN: ");
String journalIsbn = scanner.nextLine();
System.out.print("Enter Field of Study: ");
String fieldOfStudy = scanner.nextLine();
library.addItem(new Journal(journalTitle, journalAuthor,
journalIsbn, fieldOfStudy));
System.out.println("Journal added successfully!");
break;
case 4:
library.showItems();
break;
case 5:
System.out.print("Enter ISBN of the Item to Borrow: ");
String borrowIsbn = scanner.nextLine();
library.borrowItem(borrowIsbn);
break;
case 6:
System.out.print("Enter ISBN of the Item to Return: ");
String returnIsbn = scanner.nextLine();
library.returnItem(returnIsbn);
break;
case 7:
System.out.print("Enter ISBN of the Item to Remove: ");
String removeIsbn = scanner.nextLine();
library.removeItem(removeIsbn);
break;
case 8:
System.out.println("Exiting Library System. Goodbye!");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please try again.");
}
} catch (Exception e) {
System.err.println("Error: Invalid input. Please try again.");
scanner.nextLine(); // Clear buffer
}
}
}
}
Library Management system with username
and password
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// User Class
class User {
private String userId;
private String password;
@Override
public String toString() {
return userId + "," + password;
}
}
@Override
public String toString() {
return getType() + "," + title + "," + author + "," + isbn + "," +
(isBorrowed ? "true" : "false") + "," + additionalInfo();
}
}
// Book Class
class Book extends LibraryItem {
private double price;
@Override
public String getType() {
return "Book";
}
@Override
public String additionalInfo() {
return String.valueOf(price);
}
@Override
public void displayDetails() {
System.out.println("Type: Book, Title: " + getTitle() + ", Author: " +
getAuthor() +
", ISBN: " + getIsbn() + ", Price: Rs " + price + ", Borrowed: " +
(isBorrowed() ? "Yes" : "No"));
}
}
// Magazine Class
class Magazine extends LibraryItem {
private String publicationDate;
@Override
public String getType() {
return "Magazine";
}
@Override
public String additionalInfo() {
return publicationDate;
}
@Override
public void displayDetails() {
System.out.println("Type: Magazine, Title: " + getTitle() + ", Author: "
+ getAuthor() +
", ISBN: " + getIsbn() + ", Publication Date: " + publicationDate
+
", Borrowed: " + (isBorrowed() ? "Yes" : "No"));
}
}
// Journal Class
class Journal extends LibraryItem {
private String fieldOfStudy;
@Override
public String getType() {
return "Journal";
}
@Override
public String additionalInfo() {
return fieldOfStudy;
}
@Override
public void displayDetails() {
System.out.println("Type: Journal, Title: " + getTitle() + ", Author: " +
getAuthor() +
", ISBN: " + getIsbn() + ", Field of Study: " + fieldOfStudy +
", Borrowed: " + (isBorrowed() ? "Yes" : "No"));
}
}
// Library Class
class Library {
private List<LibraryItem> items;
private final String libraryFile = "library_items.txt";
public Library() {
items = new ArrayList<>();
loadItemsFromFile();
}
// LibraryManagementSystem Class
public class LibraryManagementSystem {
private List<User> userCredentials;
private final String userFile = "user_credentials.txt";
public LibraryManagementSystem() {
userCredentials = new ArrayList<>();
loadUsersFromFile();
}
attempts--;
System.out.println("Invalid credentials. Attempts remaining: " +
attempts);
}
if (!authenticate(scanner)) {
return;
}
while (true) {
try {
System.out.println("\nLibrary System Menu:");
System.out.println("1. Add a Book");
System.out.println("2. Add a Magazine");
System.out.println("3. Add a Journal");
System.out.println("4. Display All Items");
System.out.println("5. Borrow an Item");
System.out.println("6. Return an Item");
System.out.println("7. Remove an Item");
System.out.println("8. Exit");
System.out.print("Enter your choice: ");
switch (choice) {
case 1:
System.out.print("Enter Book Title: ");
String bookTitle = scanner.nextLine();
System.out.print("Enter Book Author: ");
String bookAuthor = scanner.nextLine();
System.out.print("Enter Book ISBN: ");
String bookIsbn = scanner.nextLine();
System.out.print("Enter Book Price: ");
double bookPrice = scanner.nextDouble();
library.addItem(new Book(bookTitle, bookAuthor, bookIsbn,
bookPrice));
break;
case 2:
System.out.print("Enter Magazine Title: ");
String magTitle = scanner.nextLine();
System.out.print("Enter Magazine Author: ");
String magAuthor = scanner.nextLine();
System.out.print("Enter Magazine ISBN: ");
String magIsbn = scanner.nextLine();
System.out.print("Enter Publication Date: ");
String pubDate = scanner.nextLine();
library.addItem(new Magazine(magTitle, magAuthor,
magIsbn, pubDate));
break;
case 3:
System.out.print("Enter Journal Title: ");
String journalTitle = scanner.nextLine();
System.out.print("Enter Journal Author: ");
String journalAuthor = scanner.nextLine();
System.out.print("Enter Journal ISBN: ");
String journalIsbn = scanner.nextLine();
System.out.print("Enter Field of Study: ");
String fieldOfStudy = scanner.nextLine();
library.addItem(new Journal(journalTitle, journalAuthor,
journalIsbn, fieldOfStudy));
break;
case 4:
library.showItems();
break;
case 5:
System.out.print("Enter ISBN of the Item to Borrow: ");
String borrowIsbn = scanner.nextLine();
library.borrowItem(borrowIsbn);
break;
case 6:
System.out.print("Enter ISBN of the Item to Return: ");
String returnIsbn = scanner.nextLine();
library.returnItem(returnIsbn);
break;
case 7:
System.out.print("Enter ISBN of the Item to Remove: ");
String removeIsbn = scanner.nextLine();
library.removeItem(removeIsbn);
break;
case 8:
System.out.println("Exiting Library System. Goodbye!");
scanner.close();
return;
default:
System.out.println("Invalid choice. Please try again.");
}
} catch (Exception e) {
System.err.println("Error: Invalid input. Please try again.");
scanner.nextLine();
}
}
}