Trouble with a problem in Java called FirstnameLastname I replaced Firstname wit
ID: 3686458 • Letter: T
Question
Trouble with a problem in Java called FirstnameLastname I replaced Firstname with my first name and Lastname with my last name). Now I just need to figure out an ArrayList called BookList. It should display a menu to allow the user to choose from the following options:
Enter 1 to add a book
Enter 2 to edit a book
Enter 3 to remove a book from the list: Enter 4 to display the list of books: Enter 5 to quit:
The program should use a case/switch statement and switch on the users choice. The program should continue until the user enters 5 to quit. Case 1 should use BookList.add to add books to the ArrayList. Case 2 should use BookList.set to edit books to the ArrayList. Case 3 should use BookList.remove to remove a name from the list (Ask the user to enter the index number of the book to delete from ArrayList). Case 4 should use a for loop to display all books in the ArrayList along with their index number. Any help would be appreciated . display should be a menu list output of what the user types in . Thanks Jason :)
Explanation / Answer
BookList.java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BookList {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
java.util.Scanner in = new java.util.Scanner(System.in);
List list = new ArrayList();
while(true){
System.out.println("Choose your Option: 1) Add a Book 2) Edit a Book 3) Remove a Book 4) Display the list of Books 5) Quit");
int choice = in.nextInt();
switch(choice){
case 1: addBook(list, in); break;
case 2: editBook(list, in); break;
case 3: removeBook(list, in); break;
case 4: displayBooks(list, in); break;
case 5: System.exit(0); break;
}
}
}
public static void addBook(List list, Scanner in){
System.out.println("Please enter book name");
String name = in.next();
list.add(name);
System.out.println("Given book added ");
}
public static void editBook(List list, Scanner in){
System.out.println("Please enter book name");
String name = in.next();
System.out.println("Please enter the index where this book to be inserted");
int n = in.nextInt();
if(n > list.size()-1){
System.out.println("Given index is too high. The size f book list is "+list.size());
}
else{
list.set(n, name);
}
System.out.println("Given book updated with the index");
}
public static void removeBook(List list, Scanner in){
if(list!=null && list.size() > 0){
System.out.println("Please enter book name");
String name = in.next();
list.remove(name);
System.out.println("Given Book removed from the list");
}
else{
System.out.println("No books Avalable");
}
}
public static void displayBooks(List list, Scanner in){
System.out.println("-------------List of Books Display-----------------");
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i));
}
}
}
Output:
Choose your Option:
1) Add a Book
2) Edit a Book
3) Remove a Book
4) Display the list of Books
5) Quit
1
Please enter book name
aaaaa
Given book added
Choose your Option:
1) Add a Book
2) Edit a Book
3) Remove a Book
4) Display the list of Books
5) Quit
4
-------------List of Books Display-----------------
aaaaa
Choose your Option:
1) Add a Book
2) Edit a Book
3) Remove a Book
4) Display the list of Books
5) Quit
1
Please enter book name
bbbbbb
Given book added
Choose your Option:
1) Add a Book
2) Edit a Book
3) Remove a Book
4) Display the list of Books
5) Quit
4
-------------List of Books Display-----------------
aaaaa
bbbbbb
Choose your Option:
1) Add a Book
2) Edit a Book
3) Remove a Book
4) Display the list of Books
5) Quit
1
Please enter book name
ccccccc
Given book added
Choose your Option:
1) Add a Book
2) Edit a Book
3) Remove a Book
4) Display the list of Books
5) Quit
2
Please enter book name
ddddddd
Please enter the index where this book to be inserted
1
Given book updated with the index
Choose your Option:
1) Add a Book
2) Edit a Book
3) Remove a Book
4) Display the list of Books
5) Quit
4
-------------List of Books Display-----------------
aaaaa
ddddddd
ccccccc
Choose your Option:
1) Add a Book
2) Edit a Book
3) Remove a Book
4) Display the list of Books
5) Quit
3
Please enter book name
ccccccc
Given Book removed from the list
Choose your Option:
1) Add a Book
2) Edit a Book
3) Remove a Book
4) Display the list of Books
5) Quit
4
-------------List of Books Display-----------------
aaaaa
ddddddd
Choose your Option:
1) Add a Book
2) Edit a Book
3) Remove a Book
4) Display the list of Books
5) Quit
5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.