Question

Write a java application that should have a menu system along the following lines (you can...

Write a java application that should have a menu system along the following lines (you can have sub-menus if you deem that to be necessary)

1) Ask the user how many books their application should store • The application then creates a suitable storage component for the books (i.e. an array of Book objects)

2) Add book details • You as the reader can add details of a book here (i.e. a Book object’s properties are given values).

3) Display details of a book • The application should output the details of a book based on the user entering in the title of the book

4) Display all the Books • All the books should be displayed

5) End application • The user can exit the application

Functional Requirements Your Java program must have the following:

• A Book class with appropriate properties and methods (including get and set)

• Appropriate use of methods for the menu system • Appropriate use of loops 3 • Appropriate use of a toString method in the Book class

• Appropriate use of validation of user entry

• Appropriate use of comments

• An application class that will run the menu described in the scenario

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// Book.java

public class Book {
   // Instance data
   private int pages;
   private String title;

   // Constructor with parameters
   public Book(int p, String t) {
       pages = p;
       title = t;
   }

   // Getter methods
   public int getPages() {
       return pages;
   }

   public String getTitle() {
       return title;
   }

   // Setter methods
   public void setPages(int myPages) {
       pages = myPages;
   }

   public void setTitle(String myTitle) {
       title = myTitle;
   }

   // Printing method
   public String toString() {
       return "Title: \"" + title + "\" Pages: " + pages + " pages";
   }

   // Comparison methods
   public boolean equals(Book b) {
       if (pages == b.getPages() && title.equals(b.getTitle())) {
           return true;
       } else {
           return false;
       }
   }

   public int compareTo(Book b) {
       if (pages < b.getPages()) {
           return -1;
       } else if (pages > b.getPages()) {
           return 1;
       } else {
           return 0;
       }
   }
}

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

// Test.java

import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
       int size, cnt = 0;
       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);

       System.out.print("How many books you wnt to add ? ");
       size = sc.nextInt();

       Book b[] = new Book[size];

       while (true) {
           System.out.println("\nMenu");
           System.out.println("1.Add Book");
           System.out.println("2.Display Book");
           System.out.println("3.Display All");
           System.out.println("4.Quit");
           System.out.print("Enter Choice :");
           int choice = sc.nextInt();
           switch (choice) {
           case 1: {
               sc.nextLine();
               if (size != cnt) {
                   System.out.print("Enter Book title :");
                   String title = sc.nextLine();
                   System.out.print("Enter no of pages :");
                   int noOfPages = sc.nextInt();

                   Book bk = new Book(noOfPages, title);
                   b[cnt] = bk;
                   cnt++;
                   System.out.println("\n** Book Added Successfully **");
               } else {
                   System.out.println("\n** Array is full **");
               }

               continue;
           }
           case 2: {
               sc.nextLine();
               System.out.print("\nEnter Book title to search for :");
               String title = sc.nextLine();
               int pos = search(b, cnt, title);
               if(pos==-1)
               {
                   System.out.println("** Book not found **");
               }
               else
               {
                   System.out.println(b[pos]);
               }
               continue;
           }
           case 3: {
               System.out.println("\nDisplaying all books info :");
               for(int i=0;i<cnt;i++)
               {
                   System.out.println(b[i]);
               }
               continue;
           }
           case 4: {
               break;
           }
           default: {
               System.out.println("** Invalid Choice **");
               continue;
           }

           }
           break;
       }

   }

   private static int search(Book[] b, int cnt, String title) {
       for (int i = 0; i < cnt; i++) {
           if (b[i].getTitle().equalsIgnoreCase(title)) {
               return i;
           }
       }
       return -1;
   }

}

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

Output:

How many books you wnt to add ? 5

Menu
1.Add Book
2.Display Book
3.Display All
4.Quit
Enter Choice :1
Enter Book title :Database Management
Enter no of pages :450

** Book Added Successfully **

Menu
1.Add Book
2.Display Book
3.Display All
4.Quit
Enter Choice :1
Enter Book title :Ansi C
Enter no of pages :450

** Book Added Successfully **

Menu
1.Add Book
2.Display Book
3.Display All
4.Quit
Enter Choice :3

Displaying all books info :
Title: "Database Management" Pages: 450 pages
Title: "Ansi C" Pages: 450 pages

Menu
1.Add Book
2.Display Book
3.Display All
4.Quit
Enter Choice :2

Enter Book title to search for :Ansi C
Title: "Ansi C" Pages: 450 pages

Menu
1.Add Book
2.Display Book
3.Display All
4.Quit
Enter Choice :4

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Write a java application that should have a menu system along the following lines (you can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Subject: Java Program You are writing a simple library checkout system to be used by a...

    Subject: Java Program You are writing a simple library checkout system to be used by a librarian. You will need the following classes: Patron - A patron has a first and last name, can borrow a book, return a book, and keeps track of the books they currently have checked out (an array or ArrayList of books). The first and last names can be updated and retrieved. However, a patron can only have up to 3 books checked out at...

  • Java This application will be menu driven. The application should allow: 1. Collect student information and...

    Java This application will be menu driven. The application should allow: 1. Collect student information and store it in a binary data file. Each student is identified by an unique ID.   The user should be able to view/edit an existing student. Do not allow the user to edit student ID. 2. Collect Course information and store it in a separate data file. Each course is identified by an unique ID. The user should be able to view/edit an existing course...

  • Design and implement a Java data structure class named BookShelf which has an array to store...

    Design and implement a Java data structure class named BookShelf which has an array to store books and the size data member. The class should have suitable constructors, get/set methods, and the toString method, as well as methods for people to add a book, remove a book, and search for a book. Design and implement a Java class named Book with two data members: title and price. Test the two classes by creating a bookshelf object and five book objects....

  • Need help on following Java GUI problem: Write a program that lets a user display and...

    Need help on following Java GUI problem: Write a program that lets a user display and modify pictures. Create a window. Add four buttons so that clicking a particular button will shift the image by a small amount in the north, south, east or west direction inside the window. Add a menu bar with two menus: File and Image. The File menu should contain an Open menu item that the user can select to display JPEG and PNG files from...

  • JAVA Objective : • Learning how to write a simple class. • Learning how to use...

    JAVA Objective : • Learning how to write a simple class. • Learning how to use a prewritten class. • Understanding how object oriented design can aid program design by making it easier to incorporate independently designed pieces of code together into a single application. Instructions: • Create a project called Lab13 that contains three Java file called Book.java , Bookstore.java and TempleBookstore.java • I’ve provided a screenshot of how your project should look like. • Be sure to document...

  • (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...

    (2 bookmarks) In JAVA You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner. This program will have three classes: Candidate, Results and ElectionApp Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are...

  • ****Please read the following requirements and use java language w/ comments**** ****Can make this a multi...

    ****Please read the following requirements and use java language w/ comments**** ****Can make this a multi part question if needed**** Project requires a base class, a derived subclass, and an interactive driver (class with a main) that allows the user to utilize and manipulate the classes created. You may select one super class from the following:               Superclass                                    Example subclasses Ship class                                a) battleship, tugboat, icebreaker Person class                           b) manager, salaryworker, hourlyworker Aircraft class                         c) Learjet, cargoplane,...

  • You need to program a simple book library system. There are three java classes Book.java   //...

    You need to program a simple book library system. There are three java classes Book.java   // book object class Library.java   //library class A2.java //for testing The Book.java class represents book objects which contain the following fields title: a string which represents the book title. author: a string to hold the book author name year: book publication year isbn: a string of 10 numeric numbers. The book class will have also 3 constructors. -The default no argument constructor - A constructor...

  • I tried to complete a Java application that must include at a minimum: Three classes minimum...

    I tried to complete a Java application that must include at a minimum: Three classes minimum At least one class must use inheritance At least one class must be abstract JavaFX front end – as you will see, JavaFX will allow you to create a GUI user interface. The User Interface must respond to events. If your application requires a data backend, you can choose to use a database or to use text files. Error handling - The application should...

  • The matrix operations that you should include are Addition, Subtraction and Multiplication. The driver can be...

    The matrix operations that you should include are Addition, Subtraction and Multiplication. The driver can be as simple as asking the user to enter two matrices and then presenting a simple menu that allows the user to select the operation they want to test. Have the menu in a loop so that the user can test any other operation unless they choose to exit the menu. Also, provide the user an option to select two new matrices. Make sure that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT