JAVA PROGRAMMING HELP ME! PLEASE MAKE THIS PROGRAM RUN, I' HAVING HARD TIME TO FIX IT PLEASE. I WILL RATE YOU VERY GOOD, THANK YOU SO MUCH! SEE ATTACHED PHOTO FOR THE INSTRUCTIONS OR GUIDE
JAVA PROGRAMMING
HELP ME! PLEASE MAKE THIS PROGRAM RUN, I' HAVING HARD TIME TO FIX IT PLEASE. I WILL RATE YOU VERY GOOD, THANK YOU SO MUCH! SEE ATTACHED PHOTO FOR THE INSTRUCTIONS OR GUIDE
import java.util.*;
public class TestMain {
public static void main(String[] args) {
// TODO code application logic here
System.out.println("Menu:-\n1 – Add\n2 – Count\n3 – Print\n4 – Search\n5 – Delete\n6 - Exit");
Scanner sc = new Scanner(System.in);
int menuId=0;
CollectionBooks m = new CollectionBooks();
while (menuId!=6){
System.out.print("Your choice: ");
menuId=sc.nextInt();
switch(menuId){
case 1 :
System.out.print("Add-Book Id: ");
int bookId=sc.nextInt();
System.out.print("Add-Book Name: ");
sc.nextLine();
String book=sc.nextLine();
m.add(book,bookId);
break;
case 2:
System.out.println("Count= "+m.count());
break;
case 3:
m.printAll();
break;
case 4:
sc.nextLine();
System.out.println("Search Book: ");
String bookName=sc.nextLine();
Book b=new Book();
b.setName(bookName);
Book n=m.search(b);
System.out.println("BookName : "+n.getName()+" \nBook Id: "+n.getId());
break;
case 5:
System.out.println("Remove index: ");
m.remove(sc.nextInt());
break;
default:
System.out.println("Invalid Input ");
}
}
}
}
public class Book {
String name;
int id;
public Book() {
}
public Book(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id=id;
}
public void setName(String name){
this.name=name;
}
public int getId() {
return id;
}
}
public class CollectionBooks {
private ArrayList<Book> books;
public CollectionBooks() {
books = new ArrayList<Book>();
}
public void add(String book,int bookId) {
Book b=new Book(book,bookId);
books.add(b);
}
public void printAll() {
for(Book b:books){
System.out.println("Book Id: "+b.getId());
System.out.println("Book Name: "+b.getName());
}
}
public int count(){
return books.size();
}
public Book search(Object e){
for(Book b:books) {
if(b.equals(e))
return b;
}
return null;
}
public void remove(int index) {
books.remove(index);
}
}
Step by step
Solved in 4 steps with 4 images