Can someone help me with this java problem? Thanks There is a Book shelf in the
ID: 3834526 • Letter: C
Question
Can someone help me with this java problem? Thanks
There is a Book shelf in the library, which has 2 shelf, each shelf contains 4 sections and each section can have 1 book only.
String[] shelf1 = {“Math1”,”History2”,”Art3”,”literature4” };
String[] shelf2 = {“Physics1”,”Biology2”,”Chemistry3”, “Science4“};
User wants to put a Book in the shelf. The java program asks the user to add the name of the Book, then asks which shelf it belongs to(example- user chooses shelf2) , then it asks which section it belongs to(example- user chooses Biology2).Then it adds the book to that specific section. Each section can contain one book only. If the shelf is not available the programs tells the user that it is not available. The program also gives the user an option to view which sections are available.
Explanation / Answer
Code :
package booklibrary;
import java.util.Scanner;
public class BookLibrary {
static String[] shelf1 = {"Math1","History2","Art3","literature4" };
static String[] shelf2 = {"Physics1","Biology2","Chemistry3", "Science4"};
static String[] shelf1_Books = {"NA", "NA", "NA", "NA"}; // ith index in shelf_book array will store book name in ith section
static String[] shelf2_Books = {"NA", "NA", "NA", "NA"};
public static void main(String[] args) {
// TODO code application logic here
boolean flag = true;
int choice, shelf, section;
String bookname;
Scanner sc= new Scanner(System.in);
while(flag){
System.out.println("1. Add a book 2. Check available shelf 3. Exit");
choice = sc.nextInt();
switch(choice){
case 1 :
System.out.println(" Enter a book name : ");
bookname = sc.next();
System.out.println(" Enter shelf to add book (1 or 2):");
shelf = sc.nextInt();
System.out.println(" Select section from following options :");
switch(shelf){
case 1:
System.out.println(" Sections for shelf 1 " );
System.out.println("1. "+ shelf1[0] + " ");
System.out.println("2. "+ shelf1[1] + " ");
System.out.println("3. "+ shelf1[2] + " ");
System.out.println("4. "+ shelf1[3] + " ");
section = sc.nextInt();
if(shelf1_Books[section-1].equals("NA")){
shelf1_Books[section-1] = bookname;
System.out.println(" Book has been added successfully .");
}
else{
System.out.println(" Shelf and section chosen are not empty. Try again!");
}
break;
case 2:
System.out.println(" Sections for shelf 2 " );
System.out.println("1. "+ shelf2[0] + " ");
System.out.println("2. "+ shelf2[1] + " ");
System.out.println("3. "+ shelf2[2] + " ");
System.out.println("4. "+ shelf2[3] + " ");
section = sc.nextInt();
if(shelf2_Books[section-1].equals("NA")){
shelf2_Books[section-1] = bookname;
System.out.println(" Book has been added successfully .");
}
else{
System.out.println(" Shelf and section chosen are not empty. Try again!");
}
break;
default : System.out.println(" Please enter valid choice .");
break;
}
break;
case 2 :
System.out.println(" Details of both shelf and books in it : ");
System.out.println("----------------------------------------");
System.out.println(" Shelf 1 :");
System.out.println(" Category ---------- Book name ");
for(int i =0; i<4; i++){
System.out.println(" "+shelf1[i] +" - - "+ shelf1_Books[i]);
}
System.out.println("----------------------------------------");
System.out.println(" Shelf 2 :");
System.out.println(" Category ---------- Book name");
for(int i =0; i<4; i++){
System.out.println(" "+shelf2[i] +" - - "+ shelf2_Books[i]);
}
break;
case 3:
flag = false;
break;
default : System.out.println(" Please enter valid choice .");
break;
}
}
}
}
Output :
1. Add a book
2. Check available shelf
3. Exit
1
Enter a book name :
newBook1
Enter shelf to add book (1 or 2):
1
Select section from following options :
Sections for shelf 1
1. Math1
2. History2
3. Art3
4. literature4
2
Book has been added successfully .
1. Add a book
2. Check available shelf
3. Exit
2
Details of both shelf and books in it :
----------------------------------------
Shelf 1 :
Category ---------- Book name
Math1 - - NA
History2 - - newBook1
Art3 - - NA
literature4 - - NA
----------------------------------------
Shelf 2 :
Category ---------- Book name
Physics1 - - NA
Biology2 - - NA
Chemistry3 - - NA
Science4 - - NA
1. Add a book
2. Check available shelf
3. Exit
3
BUILD SUCCESSFUL (total time: 47 seconds)
if you have any doubts, you can ask in comment section.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.