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

projects

dasdddddddddddaada

Uploaded by

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

projects

dasdddddddddddaada

Uploaded by

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

//main

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Date;
public class Lbms_Eiza {
public static void main(String[] args) {
Library library = new Library();
Book book1 = new Book("How to Win Friends", "F. Scott Fitzgerald", "123456");
Book book2 = new Book("Art of not loving", "George", "789012");
Book book3 = new Book("To Kill a Mockingbird" , "Harper Lee" , "978800");
Book book4 = new Book("1984", "George Orwell", "9780451524935");
Book book5 = new Book("Pride and Prejudice", "Jane Austen", "9780141199078");
Book book6 = new Book("The Great Gatsby", "F. Scott Fitzgerald",
"9780743273565");
Book book7 = new Book("Moby Dick", "Herman Melville", "9781503280786");
Book book8 = new Book("War and Peace", "Leo Tolstoy", "9781400079988");
Book book9 = new Book("The Catcher in the Rye", "J.D. Salinger",
"9780316769488");
Book book10 = new Book("Brave New World", "Aldous Huxley", "9780060850524");
Book book11 = new Book("The Hobbit", "J.R.R. Tolkien", "9780547928227");
library.addBook(book1);
library.addBook(book2);
library.addBook(book3);
library.addBook(book4);
library.addBook(book5);
library.addBook(book6);
library.addBook(book7);
library.addBook(book8);
library.addBook(book9);
library.addBook(book10);
library.addBook(book11);
System.out.println(library.books);

// Adding users
User user1 = new User("Alice", 1);
User user2 = new User("Bob", 2);
User user3 = new User("Charlie", 3);
User user4 = new User("David", 4);
User user5 = new User("Eve", 5);
User user6 = new User("Frank", 6);
User user7 = new User("Grace", 7);
User user8 = new User("Hannah", 8);
User user9 = new User("Ivy", 9);
User user10 = new User("Jack", 10);
// Adding all users to the library
library.addUser(user1);
library.addUser(user2);
library.addUser(user3);
library.addUser(user4);
library.addUser(user5);
library.addUser(user6);
library.addUser(user7);
library.addUser(user8);
library.addUser(user9);
library.addUser(user10);
System.out.println(library.users);
//electronics
Electronics E1 = new Electronics("Laptop", "E001");
Electronics E2 = new Electronics("Tablet", "E002");
library.addElectronics(E1);
library.addElectronics(E2);
System.out.println(library.electronics);
// Transaction
Transaction transaction = new Transaction("T001", user1, book1, new Date());
System.out.println("Transaction ID " + transaction.getTransactionId());
System.out.println(transaction.getUser());
System.out.println(transaction.getBook());
System.out.println(transaction.toString());
// Command loop
Scanner scanner = new Scanner(System.in);
String command;

//Scanner scanner = new Scanner(System.in);//

while (true) {
// Display menu
System.out.println("\nWelcome to the Library System!");
System.out.println("Available commands:");
System.out.println("1 - Borrow a book");
System.out.println("2 - Return a book");
System.out.println("3 - Add a new book");
System.out.println("4 - Add a new user");
System.out.println("5 - View all books");
System.out.println("6 - Exit");
System.out.print("Enter your choice: ");

// Get user input


int option = scanner.nextInt();
scanner.nextLine(); // Clear input buffer

switch (option) {
case 1: // Borrow a book
System.out.print("Enter user ID: ");
int borrowerId = scanner.nextInt();
scanner.nextLine(); // Clear input buffer
System.out.print("Enter book ISBN: ");
String borrowIsbn = scanner.nextLine();

Book borrowedBook = null;


for (Book book : library.books) {
if (book.getIsbn().equals(borrowIsbn)) {
borrowedBook = book;
break;
}
}

if (borrowedBook == null) {
System.out.println("Book not found.");
} else if (!borrowedBook.isAvailable()) {
System.out.println("Book is already borrowed.");
} else {
borrowedBook.setIssued(true);
borrowedBook.setAvailability(false);
System.out.println("You have successfully borrowed the
book: " + borrowedBook.getTitle());
}
break;
case 2: // Return a book
System.out.print("Enter book ISBN: ");
String returnIsbn = scanner.nextLine();

Book returnedBook = null;


for (Book book : library.books) {
if (book.getIsbn().equals(returnIsbn)) {
returnedBook = book;
break;
}
}

if (returnedBook == null) {
System.out.println("Book not found.");
} else if (returnedBook.isAvailable()) {
System.out.println("Book is not currently borrowed.");
} else {
returnedBook.setIssued(false);
returnedBook.setAvailability(true);
System.out.println("You have successfully returned the
book: " + returnedBook.getTitle());
}
break;

case 3: // Add a new book


System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter author: ");
String author = scanner.nextLine();
System.out.print("Enter ISBN: ");
String isbn = scanner.nextLine();

Book newBook = new Book(title, author, isbn);


library.addBook(newBook);
System.out.println("Book added: " + newBook);
break;

case 4: // Add a new user


System.out.print("Enter user name: ");
String userName = scanner.nextLine();
System.out.print("Enter user ID: ");
int userId = scanner.nextInt();
scanner.nextLine(); // Clear input buffer

User user = new User(userName, userId);


library.addUser(user);
System.out.println("User added: " + user);
break;

case 5: // View all books


System.out.println("Books in the library:");
for (Book book : library.books) {
System.out.println(book);
}
break;

case 6: // Exit
System.out.println("Exiting the Library System. Goodbye!");
scanner.close();
System.exit(0);

default: // Invalid option


System.out.println("Invalid choice. Please try again.");
break;
}
}
}
}
//library
import java.util.ArrayList;

public class Library {

ArrayList<Book> books = new ArrayList<>();


ArrayList<User> users = new ArrayList<>();
ArrayList<Electronics>electronics = new ArrayList<>();
private static int totalBooksIssued = 0;
public Library() {
this.books = new ArrayList<>();
this.users = new ArrayList<>();
this.electronics = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public void addUser(User user) {
users.add(user);
}
public void addElectronics(Electronics device) {
electronics.add(device);
}
public ArrayList<Electronics> getElectronics() {
return electronics;
}
public void issueBook(String isbn, String userId) {
for (Book book : books) {
if (book.getIsbn().equals(isbn) && book.isAvailable()) {
book.setAvailability(false);
System.out.println("Book issued successfully.");
return;
}
}
System.out.println("Book is not available.");
}
public void returnBook(String isbn) {
for (Book book : books) {
if (book.getIsbn().equals(isbn)) {
book.setAvailability(true);
System.out.println("Book returned successfully.");
return;
}
}
System.out.println("Book not found.");
}
}
//book
public class Book {
private String title;
private String author;
private String isbn;
private boolean isIssued;
private boolean isAvailable;
public Book(String title, String author, String isbn) {
this.title = title;
this.author = author;
this.isbn = isbn;
this.isIssued = false;
this.isAvailable = true;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getIsbn() {
return isbn;
}
public boolean isAvailable() {
return isAvailable;
}
public boolean isIssued() {
return isIssued;
}
public void setAvailability(boolean availability) {
isAvailable = availability;
}
public void setIssued(boolean issued) {
isIssued = issued;
}
@Override
public String toString() {
return "Title: " + title + ", Author: " + author + ", ISBN: " + isbn + ",
Issued: " + isIssued;
}
}
//user
public class User {
private String name;
private int userId;
private static int totalUsers = 0;
private Book borrowed;

public User(String name, int userId) {


this.name = name;
this.userId = userId;
this.borrowed = null;
totalUsers++;
}
public String getName() {
return name;
}

public int getUserId() {


return userId;
}
public Book getBorrowed() {
return borrowed;
}
public void setBorrowed(Book book) {
borrowed = book;

public static int getTotalUsers() { // Static method


return totalUsers;
}
@Override
public String toString() {
return "User ID: " + userId + ", Name: " + name;
}

}
//transaction
import java.util.Date;
public class Transaction {
private String transactionId;
private User user;
private Book book;
private Date issueDate;
private Date returnDate;

public Transaction(String transactionId, User user, Book book, Date issueDate)


{
this.transactionId = transactionId;
this.user = user;
this.book = book;
this.issueDate = issueDate;

public void setReturnDate(Date returnDate) {


this.returnDate = returnDate;
}

public String getTransactionId() {


return transactionId;
}

public User getUser() {


return user;
}

public Book getBook() {


return book;
}
@Override
public String toString() {
return "User" + user + " Book: " + book+ "issueDate" + issueDate;
}

}
//electronics
public class Electronics {
private String name;
private String serialNumber;
private boolean isAvailable;

public Electronics(String name, String serialNumber) {


this.name = name;
this.serialNumber = serialNumber;
this.isAvailable = true;
}

public String getName() {


return name;
}

public String getSerialNumber() {


return serialNumber;
}

public boolean isAvailable() {


return isAvailable;
}

public void setAvailable(boolean available) {


isAvailable = available;
}
@Override
public String toString() {
return "Name" + name + ", SerialNumber " + serialNumber;
}

You might also like