207k views
5 votes
2. Using the Book class developed in HW 4, write an application, BookLibrary, that creates a library (array) of up to 10 books and gives the user three options: to add a book, to delete a book from the library or to change a book’s information (i.e. number of pages and/or title) in the library. a. If the user selects the add option, issue an error message if the library is full. Otherwise, prompt the user for a book title. If the book with that title exists, issue an error message (no duplicated books titles are allowed). Otherwise, prompt the user for the number of pages and add the book. b. If the user selects the delete option, issue an error message if the library is empty. Otherwise, prompt the user for a book title. If the book with that title does not exist, issue an error message. Otherwise, "delete" (do not access the book for any future processing). c. If the user selects the change option, issue an error message if the library is empty. Otherwise, prompt the user for a book title. If the requested book title does not exist, issue an error message. Otherwise, prompt the user for a new title and/or number of pages and change the book information.

1 Answer

5 votes

BookLibrary, that creates a library (array) of up to 10 books. If the user selects the add option, issue an error message if the library is full.If the book with that title exists, issue an error message

Step-by-step explanation:

import java.util.Scanner;

import java.util.Arrays;

public class Derro_BooksLibrary {

public static void main(String[] args) {

Derro_Books[] Library = new Derro_Books[10];

Scanner scan = new Scanner(System.in);

Derro_Books book1 = new Derro_Books("", 20);

Derro_Books book2 = new Derro_Books("hi", 10);

Library[0] = book1;

Library[1] = book2;

System.out.println("Enter 1 for add, 2 for delete, 3 for change, 4 to stop application: ");

int answer = scan.nextInt();

int counter = 0;

int z = 0;

while (true) {

if (answer == 1) {

for (Derro_Books book : Library) {

counter = counter + 1;

}

if (counter == 10) {

System.out.println("Library full");

}

System.out.println("Enter Title: ");

String title = scan.nextLine();

for (int i = 0; i < Library.length; i++) {

if (title == Library[i].getTitle()) {

System.out.println("No Duplicated Books Titles Are Allowed");

break;

}

else {

z++;

}

}

if (z < Library.length) {

System.out.println("Enter number of pages: ");

int pages = scan.nextInt();

Library[z + 1] = new Derro_Books(title, pages);

}

if (answer == 2){

if (Library.length == 0) {

System.out.println("Library is empty");

}

System.out.println("Enter title of book: ");

String title2 = scan.nextLine();

for (int d = 0; d < Library.length; d++) {

if (title2 == Library[d].getTitle())

// Delete book

System.out.println("Deleted book: " + title2);

else

System.out.println("Book does not exist");

}

}

if (answer == 3) {

if (Library.length == 0) {

System.out.println("Library is empty.");

} else {

System.out.println("Enter book title: ");

String title3 = scan.nextLine();

for (int y = 0; y < Library.length; y++) {

if (title3 != Library[y].getTitle())

continue;

else {

System.out.println("Enter number of pages: ");

int pages = scan.nextInt();

Library[y] = new Derro_Books(title3, pages);

}

}

}

}

String[] StringList = new String[10];

for (int y = 0; y < Library.length; y++) {

StringList[y] = Library[y].getTitle();

}

Arrays.sort(StringList);

for (String bookt : StringList) {

System.out.println(bookt + Library);

}

}

}

}

}

User Canecse
by
2.9k points