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