0% found this document useful (0 votes)
15 views4 pages

Manrique & Mejias Exercise2

The document is a Java program for a Library Management System that allows users to input details for four books, including their ID, title, author, and price. It provides a simple menu for users to view books by author, title, or price by ID, and includes validation for the book ID input. The program utilizes a Book class to store book information and methods to display the book details based on user selections.

Uploaded by

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

Manrique & Mejias Exercise2

The document is a Java program for a Library Management System that allows users to input details for four books, including their ID, title, author, and price. It provides a simple menu for users to view books by author, title, or price by ID, and includes validation for the book ID input. The program utilizes a Book class to store book information and methods to display the book details based on user selections.

Uploaded by

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

package librarymanagementsystem_01;

import java.util.Scanner;

// Book class to store book information


class Book {
String bookID;
String title;
String author;
double price;

// Constructor to initialize book details


public Book(String bookID, String title, String author, double price) {
this.bookID = bookID;
this.title = title;
this.author = author;
this.price = price;
}
}

public class LibraryManagementSystem {

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);

System.out.println("------------------------------------------------------------------------------");
System.out.println("Hello Human! Welcome to Library Management System.");
System.out.println("\n*please provide the 4 set of book details that are needed for the
system");
System.out.println("------------------------------------------------------------------------------");
// Create an array to hold 4 Book objects
Book[] books = new Book[4];

// Input details for 4 books


for (int i = 0; i < 4; i++) {
System.out.println("Enter details for book " + (i + 1) + ":");

String bookID;
while (true) {
System.out.print("Book ID (numbers only):");
bookID=scan.nextLine();
if (bookID.matches("\\d+")) {
break;
}else {
System.out.println("Invalid BookID. Please enter numbers only.");
}
}
System.out.print("Title: ");
String title = scan.nextLine();
System.out.print("Author: ");
String author = scan.nextLine();
System.out.print("Price: ");
double price = scan.nextDouble();
scan.nextLine(); // Consume the leftover newline

// Store the book in the array


books[i] = new Book(bookID, title, author, price);
}

// Simple menu for viewing book details


while (true) {
System.out.println("------------------------------------------------------------------------------");
System.out.println();
System.out.println();
System.out.println(" |------------------------------------------------|");
System.out.println(" | Menu |");
System.out.println(" |------------------------------------------------|");
System.out.println(" | 1. View All Books by Author |");
System.out.println(" | 2. View All Books by Title |");
System.out.println(" | 3. View Book Price by ID |");
System.out.println(" | 4. Exit |");
System.out.println(" |------------------------------------------------|");
System.out.println();
System.out.println();
System.out.println("------------------------------------------------------------------------------");
System.out.print("Choose an option: ");
int choice = scan.nextInt();
scan.nextLine();

if (choice == 4) {
System.out.println("Exiting...");
break;
}

switch (choice) {
case 1 -> {
System.out.println();
System.out.println("Displaying all books by Author:");
System.out.println();
viewAllBooks(books);
}

case 2 -> {
System.out.println();
System.out.println("Displaying all books by Title:");
System.out.println();
viewAllBooksByTitle(books);
}

case 3 -> {
System.out.println();
System.out.print("Displaying all book price by ID:");
System.out.println();
viewBookPriceByID(books);
}

default -> System.out.println("Invalid option. Please try again.");


}
}

scan.close();
}

// Method to view all books


public static void viewAllBooks(Book[] books) {
for (Book book : books) {
System.out.println("------------------------------------------------------------------------------");
System.out.println( " Title: " + book.title + " --> " + " Author: " + book.author + "\n");

}
}
public static void viewAllBooksByTitle(Book[] books) {
for (Book book : books) {
System.out.println("------------------------------------------------------------------------------");
System.out.println( " Author: " + book.author + " --> " + " Title: " + book.title + "\n" );

}
}
public static void viewBookPriceByID(Book[] books) {
for (Book book : books) {

System.out.println("------------------------------------------------------------------------------");
System.out.println("Book ID: " + book.bookID + " --> " + " Price: " + book.price + "
--> " + "Title: " + book.title );
}
}

You might also like