0% found this document useful (0 votes)
8 views

library managent

Uploaded by

fa23-bse-210
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

library managent

Uploaded by

fa23-bse-210
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

LibraryManagementSystem.

java without username and password

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;
}

// Public getters and setters for encapsulation


public String getTitle() {
return title;
}

public void setTitle(String title) {


this.title = title;
}

public String getAuthor() {


return author;
}

public void setAuthor(String author) {


this.author = author;
}

public String getIsbn() {


return isbn;
}

public void setIsbn(String isbn) {


this.isbn = isbn;
}

public boolean isBorrowed() {


return isBorrowed;
}

public void setBorrowed(boolean borrowed) {


isBorrowed = borrowed;
}

// Abstract method to enforce implementation in subclasses


public abstract void displayDetails();
}

// Interface for purchasable items


interface Purchasable {
double getPrice();

void purchase();
}

// Concrete class: Book


class Book extends LibraryItem implements Purchasable {
private double price;

// Constructor
public Book(String title, String author, String isbn, double price) {
super(title, author, isbn);
this.price = price;
}

// Implement interface methods


@Override
public double getPrice() {
return 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"));
}
}

// Concrete class: Magazine


class Magazine extends LibraryItem {
private String publicationDate;

// Constructor
public Magazine(String title, String author, String isbn, String
publicationDate) {
super(title, author, isbn);
this.publicationDate = publicationDate;
}

// Getter for publication date


public String getPublicationDate() {
return 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"));
}
}

// Concrete class: Journal


class Journal extends LibraryItem {
private String fieldOfStudy;

// Constructor
public Journal(String title, String author, String isbn, String fieldOfStudy)
{
super(title, author, isbn);
this.fieldOfStudy = fieldOfStudy;
}

// Getter for field of study


public String getFieldOfStudy() {
return 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<>();
}

public void addItem(LibraryItem item) {


items.add(item);
saveItemsToFile();
}

public void removeItem(String isbn) {


boolean removed = items.removeIf(item ->
item.getIsbn().equals(isbn));
if (removed) {
System.out.println("Item with ISBN " + isbn + " removed.");
saveItemsToFile();
} else {
System.out.println("Item with ISBN " + isbn + " not found.");
}
}

public void showItems() {


System.out.println("Library Items:");
for (LibraryItem item : items) {
item.displayDetails();
}
}

public LibraryItem findItemByIsbn(String isbn) {


for (LibraryItem item : items) {
if (item.getIsbn().equals(isbn)) {
return item;
}
}
return null;
}

public void borrowItem(String isbn) {


LibraryItem item = findItemByIsbn(isbn);
if (item != null) {
if (item.isBorrowed()) {
System.out.println("Item \"" + item.getTitle() + "\" is already
borrowed.");
} else {
item.setBorrowed(true);
System.out.println("You borrowed \"" + item.getTitle() + "\".");
saveItemsToFile();
}
} else {
System.out.println("Item with ISBN " + isbn + " not found.");
}
}

public void returnItem(String isbn) {


LibraryItem item = findItemByIsbn(isbn);
if (item != null) {
if (!item.isBorrowed()) {
System.out.println("Item \"" + item.getTitle() + "\" was not
borrowed.");
} else {
item.setBorrowed(false);
System.out.println("You returned \"" + item.getTitle() + "\".");
saveItemsToFile();
}
} else {
System.out.println("Item with ISBN " + isbn + " not found.");
}
}

private void saveItemsToFile() {


try (BufferedWriter writer = new BufferedWriter(new
FileWriter("library_items.txt"))) {
for (LibraryItem item : items) {
String type = item.getClass().getSimpleName();
writer.write(type + "," + item.getTitle() + "," + item.getAuthor()
+ "," + item.getIsbn() + "," + item.isBorrowed());
writer.newLine();
}
} catch (IOException e) {
System.err.println("Error saving library data to file: " +
e.getMessage());
}
}
}

// 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: ");

int choice = scanner.nextInt();


scanner.nextLine(); // Consume newline

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;

public User(String userId, String password) {


this.userId = userId;
this.password = password;
}

public String getUserId() {


return userId;
}

public String getPassword() {


return password;
}

@Override
public String toString() {
return userId + "," + password;
}
}

// Abstract Class for Library Items


abstract class LibraryItem {
private String title;
private String author;
private String isbn;
private boolean isBorrowed;

public LibraryItem(String title, String author, String isbn) {


this.title = title;
this.author = author;
this.isbn = isbn;
this.isBorrowed = false;
}

public String getTitle() {


return title;
}

public String getAuthor() {


return author;
}

public String getIsbn() {


return isbn;
}

public boolean isBorrowed() {


return isBorrowed;
}

public void setBorrowed(boolean borrowed) {


isBorrowed = borrowed;
}

public abstract String getType();

public abstract String additionalInfo();

public abstract void displayDetails();

@Override
public String toString() {
return getType() + "," + title + "," + author + "," + isbn + "," +
(isBorrowed ? "true" : "false") + "," + additionalInfo();
}
}

// Book Class
class Book extends LibraryItem {
private double price;

public Book(String title, String author, String isbn, double price) {


super(title, author, isbn);
this.price = 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;

public Magazine(String title, String author, String isbn, String


publicationDate) {
super(title, author, isbn);
this.publicationDate = 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;

public Journal(String title, String author, String isbn, String fieldOfStudy)


{
super(title, author, isbn);
this.fieldOfStudy = 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();
}

public void addItem(LibraryItem item) {


items.add(item);
System.out.println("Item added successfully!");
saveItemsToFile();
}
public void removeItem(String isbn) {
boolean removed = items.removeIf(item ->
item.getIsbn().equals(isbn));
if (removed) {
System.out.println("Item with ISBN " + isbn + " removed.");
saveItemsToFile();
} else {
System.out.println("Item with ISBN " + isbn + " not found.");
}
}

public void showItems() {


if (items.isEmpty()) {
System.out.println("No items in the library.");
} else {
System.out.println("Library Items:");
for (LibraryItem item : items) {
item.displayDetails();
}
}
}

public LibraryItem findItemByIsbn(String isbn) {


for (LibraryItem item : items) {
if (item.getIsbn().equals(isbn)) {
return item;
}
}
return null;
}

public void borrowItem(String isbn) {


LibraryItem item = findItemByIsbn(isbn);
if (item != null) {
if (item.isBorrowed()) {
System.out.println("Item \"" + item.getTitle() + "\" is already
borrowed.");
} else {
item.setBorrowed(true);
System.out.println("You borrowed \"" + item.getTitle() + "\".");
saveItemsToFile();
}
} else {
System.out.println("Item with ISBN " + isbn + " not found.");
}
}
public void returnItem(String isbn) {
LibraryItem item = findItemByIsbn(isbn);
if (item != null) {
if (!item.isBorrowed()) {
System.out.println("Item \"" + item.getTitle() + "\" was not
borrowed.");
} else {
item.setBorrowed(false);
System.out.println("You returned \"" + item.getTitle() + "\".");
saveItemsToFile();
}
} else {
System.out.println("Item with ISBN " + isbn + " not found.");
}
}

private void loadItemsFromFile() {


try (BufferedReader reader = new BufferedReader(new
FileReader(libraryFile))) {
String line;
while ((line = reader.readLine()) != null) {
String[] data = line.split(",");
String type = data[0];
String title = data[1];
String author = data[2];
String isbn = data[3];
boolean isBorrowed = Boolean.parseBoolean(data[4]);
switch (type) {
case "Book":
items.add(new Book(title, author, isbn,
Double.parseDouble(data[5])));
break;
case "Magazine":
items.add(new Magazine(title, author, isbn, data[5]));
break;
case "Journal":
items.add(new Journal(title, author, isbn, data[5]));
break;
}
items.get(items.size() - 1).setBorrowed(isBorrowed);
}
} catch (IOException e) {
System.out.println("No existing library data found. Starting fresh.");
}
}
private void saveItemsToFile() {
try (BufferedWriter writer = new BufferedWriter(new
FileWriter(libraryFile))) {
for (LibraryItem item : items) {
writer.write(item.toString());
writer.newLine();
}
} catch (IOException e) {
System.err.println("Error saving library items to file.");
}
}
}

// LibraryManagementSystem Class
public class LibraryManagementSystem {
private List<User> userCredentials;
private final String userFile = "user_credentials.txt";

public LibraryManagementSystem() {
userCredentials = new ArrayList<>();
loadUsersFromFile();
}

private void loadUsersFromFile() {


try (BufferedReader reader = new BufferedReader(new
FileReader(userFile))) {
String line;
while ((line = reader.readLine()) != null) {
String[] data = line.split(",");
userCredentials.add(new User(data[0], data[1]));
}
} catch (IOException e) {
System.out.println("No existing user data found. Starting with
default credentials.");
userCredentials.add(new User("admin", "123"));
}
}

private void saveUsersToFile() {


try (BufferedWriter writer = new BufferedWriter(new
FileWriter(userFile))) {
for (User user : userCredentials) {
writer.write(user.toString());
writer.newLine();
}
} catch (IOException e) {
System.err.println("Error saving user credentials to file.");
}
}

private boolean authenticate(Scanner scanner) {


int attempts = 3;

while (attempts > 0) {


System.out.print("Enter User ID: ");
String userId = scanner.nextLine();
System.out.print("Enter Password: ");
String password = scanner.nextLine();

for (User user : userCredentials) {


if (user.getUserId().equals(userId) &&
user.getPassword().equals(password)) {
System.out.println("Authentication successful! Welcome, " +
userId + ".");
return true;
}
}

attempts--;
System.out.println("Invalid credentials. Attempts remaining: " +
attempts);
}

System.out.println("Authentication failed. Exiting system.");


return false;
}

public void run() {


Library library = new Library();
Scanner scanner = new Scanner(System.in);

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: ");

int choice = scanner.nextInt();


scanner.nextLine();

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();
}
}
}

public static void main(String[] args) {


LibraryManagementSystem system = new
LibraryManagementSystem();
system.run();
}
}

You might also like