Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a program in Java to does the following. a. The program has main menu a

ID: 3879056 • Letter: 1

Question

1. Write a program in Java to does the following.

a. The program has main menu as follows.

My Application Menu

1. List all books information.

2. List the prices of most/least expensive book

3. Search titles of books by keyword

4. List book titles with prices above average

5. Add new book

0. Exit the program

==================================

Enter Choice>>

b. Write separate method for each menu item and incorporate the code accordingly.

Used Classes: Integer.parseInt, Double.parseDouble, String(split, indexOf, charAt, substring, ..), Scanner, PrintWriter, File, …

2. Extend the above application as follows

a. Extend the menu as follows.

My Application Menu

6. List all books by price range

7. List book (s) with largest number of pages (more than one book might be there)

8. List/print a unique list of authors (US style) (no duplicates Last Name, First Name)

9. List the authors who published 2 or more books

10. Delete a Book

11. Search authors and titles

0. Exit the program

==================================

Enter Choice>>

a. Write a method for each separate menu item and incorporate the code in the application.

Explanation / Answer

import java.util.*;

import java.lang.*;

import java.io.*;

class Main

{
static ArrayList<book> al = new ArrayList<>();
static class book{

String name;

int price;

int pages;

String author;

public book(String name, int price, int pages, String author){

this.name = name;

this.price=price;

this.pages=pages;

this.author=author;

}

}// sort the books by price

static class Sortbyprice implements Comparator<book>

{

public int compare(book a, book b)

{

return a.price - b.price;

}

}

//sort the books by pages

static class Sortbypages implements Comparator<book>

{

public int compare(book a, book b)

{

return a.pages - b.pages;

}

}

public static void main (String[] args) throws java.lang.Exception

{
int f=0; // take a flag for exiting the loop
while(true){
Scanner in = new Scanner(System.in);

System.out.print("Enter Choice>> ");

// al.add(new book("Lifes", 40, 443, "kevin"));

// al.add(new book("On his blindness", 50, 643, "Keats"));

// al.add(new book("Lifes", 40, 443, "kevin"));

int choice = in.nextInt();

switch(choice){

case 1 :

for(int i=0; i<al.size(); i++){

System.out.println("name: "+al.get(i).name+" price: "+al.get(i).price+" pages"+al.get(i).pages+

" author"+al.get(i).author);
f=1;
}
break;

case 2 :

Collections.sort(al, new Sortbyprice());

System.out.println("most expensive book"+al.get(al.size()).name);

System.out.println("least expensive book"+al.get(0).name);
f=1;
break;

case 3 :

System.out.println("Enter keyword to search book");

String s = in.next();

for(int i=0; i<al.size(); i++)

{

if(al.get(i).name.equals(s) || al.get(i).author.equals(s) || al.get(i).price==Integer.parseInt(s)

|| al.get(i).pages==Integer.parseInt(s)){

System.out.println(al.get(i).name);

}

}
f=1;
break;

case 4 :

int sum=0;

for(int i=0; i<al.size(); i++){

sum=sum+(Integer)al.get(i).price;

}

sum=sum/al.size();

for(int i=0; i<al.size(); i++){

if(al.get(i).price>=sum)

System.out.println(al.get(i).price);

}
f=1;
break;

case 5:

System.out.println("add a new book");

System.out.print("name ");

String name = in.next();

System.out.println();

System.out.print("price ");

int price = in.nextInt();

System.out.println();

System.out.print("pages ");

int pages = in.nextInt();

System.out.println();

System.out.print("author ");

String author = in.next();

System.out.println();

al.add(new book(name, price, pages, author));
f=1;
break;

case 7 :

Collections.sort(al, new Sortbypages());

int p = al.get(al.size()).pages;

for(int i=al.size()-1; i>=0; i++){

if((Integer)al.get(i).pages==p)

System.out.println(al.get(i).pages);

else

break;

}
f=1;
break;
case 0 :
break;
}
if(f==1)
continue;
else
break;
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote