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

java 5th edition. Program using Strings chapter 8, Book Class and Client Create

ID: 3781773 • Letter: J

Question

java 5th edition. Program using Strings chapter 8, Book Class and Client

Create Book class:

Instance variables: title, author, publisher, ISBN, price, year publishe, and copies in stock

Instance methods: default constructor (use 0s and null strings), constructor to set all values to parameter values, a set method for each attribute, a get method for each attribute, a method to update the amount in stock, a method to print all attributes

Create Client program for Book class:

main() method

instantiate a book1 object with the default constructor

instantiate a book2 object with the values constructor (use these values: The Hobbit, Tolkien, a publisher, 123-456-789, 20.00, 1980, 27)

use the print method to print the values for book1

use the get methods to get and print all attributes for book2

display the main menu (below) until the user chooses Exit

a method to display the main menu (the code to handle the choice for this menu and operations can be in main() or a separate method) which includes:

***Bookstore Menu***

Select an item and hit enter

1. Change one or more fields for a book

2. Display current data for a book

3. Update the number in stock for a book

9 Exit

note: choice 3 should be able to increase or decrease stock, ask the user which one they need

a method to display a menu for changing the value of each attribute

***Change Field Menu***

Select an item to change and hit enter

1 Book Title

2 Book Author

3 Book Publisher

4 Book ISBN

5 Book Price

6 Book Year of Publication

7 Book number in Stock

9 Exit

a method to handle the choice for the Change Field Menu and operations

Run the client to test it and the class definition:

Be sure the output is easy to follow (use descriptive strings and whitespace).

When the menu display:

Test the first menu choice in the Bookstore Menu - Test each field change in the Change Field Menu for book1

Test choice 2 in the BookStore Menu to show that all field changes worked.

Test choice 3 in the Bookstore Menu at least twice for book1 - once for adding stock and once for subtracting stock

Choose 2 in the Bookstore menu again to show that the stock update method worked

Output:

***Change Field Menu***

Select an item to change and hit enter

1 Book Title

2 Book Author

3 Book Publisher

4 Book ISBN

5 Book Price

6 Book Year of Publication

7 Book Number in Stock

9 Exit

2

What do you want the author to be?

Alan Hayes

***Change Field Menu***

Select an item to change and hit enter

1 Book Title

2 Book Author

3 Book Publisher

4 Book ISBN

5 Book Price

6 Book Year of Publication

7 Book Number in Stock

9 Exit

3

What do you want the publisher to be?

Cengage

***Change Field Menu***

Select an item to change and hit enter

1 Book Title

2 Book Author

3 Book Publisher

4 Book ISBN

5 Book Price

6 Book Year of Publication

7 Book Number in Stock

9 Exit

4

What do you want the ISBN to be?

123456789

***Bookstore Menu***

Select an item and hit enter

1 Change one or more fields for a book

2 Display current data for a book

3 Update the number in stock for a title

9 Exit

2

Book Name: On Walden Pond

Author: Alan Hayes

Publisher: Cengage

ISBN: 123456789

Price: $ 75.50

Year Published: 2010

Copies in Stock: 0

***Change Field Menu***

Select an item to change and hit enter

1 Book Title

2 Book Author

3 Book Publisher

4 Book ISBN

5 Book Price

6 Book Year of Publication

7 Book Number in Stock

9 Exit

7

What do you want the number in stock to be?

53

Explanation / Answer

// Book.java

package chegg;

public class Book {
   public Book(String title, String author, String publisher, String iSBN,
           double price, int year, int copies) {
       super();
       this.title = title;
       this.author = author;
       this.publisher = publisher;
       ISBN = iSBN;
       this.price = price;
       this.year = year;
       this.copies = copies;
   }
  
   public Book() {
       this(null, null, null, null, 0.0, 0, 0);
   }
  
   String title;
   String author;
   String publisher;
   String ISBN;
   double price;
   int year;
   int copies;
  
   public String getTitle() {
       return title;
   }
   public void setTitle(String title) {
       this.title = title;
   }
   public String getAuthor() {
       return author;
   }
   public void setAuthor(String author) {
       this.author = author;
   }
   public String getPublisher() {
       return publisher;
   }
   public void setPublisher(String publisher) {
       this.publisher = publisher;
   }
   public String getISBN() {
       return ISBN;
   }
   public void setISBN(String iSBN) {
       ISBN = iSBN;
   }
   public double getPrice() {
       return price;
   }
   public void setPrice(double price) {
       this.price = price;
   }
   public int getYear() {
       return year;
   }
   public void setYear(int year) {
       this.year = year;
   }
   public int getCopies() {
       return copies;
   }
   public void setCopies(int copies) {
       this.copies = copies;
   }
  
   public void print() {
      
       System.out.println("Book Name: " + title);
       System.out.println("Author: " + author);
       System.out.println("Publisher: " + publisher);
       System.out.println("ISBN: " + ISBN);
       System.out.println("Price: $ " + price);
       System.out.println("Year Published: " + year);
       System.out.println("Copies in Stock: " + copies);
   }
  
}

//Test.java

package chegg;

import java.util.Scanner;

public class Test {

   public static void printBook(Book book) {
      
       System.out.println("Book Name: " + book.getTitle());
       System.out.println("Author: " + book.getAuthor());
       System.out.println("Publisher: " + book.getPublisher());
       System.out.println("ISBN: " + book.getISBN());
       System.out.println("Price: $ " + book.getPrice());
       System.out.println("Year Published: " + book.getYear());
       System.out.println("Copies in Stock: " + book.getCopies());
   }
  
   public static void printBookMenu()
   {
       System.out.println("***Bookstore Menu***");
       System.out.println("Select an item and hit enter");
       System.out.println("1. Change one or more fields for a book");
       System.out.println("2. Display current data for a book");
       System.out.println("3. Update the number in stock for a book");
       System.out.println("9 Exit");
   }
  
   public static void printChangeMenu()
   {
       System.out.println("***Change Field Menu***");
       System.out.println("Select an item to change and hit enter");
       System.out.println("1 Book Title");
       System.out.println("2 Book Author");
       System.out.println("3 Book Publisher");
       System.out.println("4 Book ISBN");
       System.out.println("5 Book Price");
       System.out.println("6 Book Year of Publication");
       System.out.println("7 Book number in Stock");
       System.out.println("9 Exit");

   }
  
   public static void main(String[] args) {
       Book book1 = new Book();
       Book book2 = new Book("The Hobbit",
                       "Tolkien",
                       "a publisher",
                       "123-456-789",
                       20.00, 1980, 27);
       book1.print();
       System.out.println();;
       printBook(book2);

       printBookMenu();
      
       Scanner sc = new Scanner(System.in);
      
       while(sc.hasNext() && sc.hasNextInt())
       {
           int choice = sc.nextInt();
           if (choice == 9)
           {
               break;
           }
          
           if (choice == 1)
           {
               printChangeMenu();
               int changeChoice = sc.nextInt();
               System.out.println("ChangedChoice is: " + changeChoice);
               switch(changeChoice){
               case 1:
                   System.out.println("What do you want the title to be?");
                   String title = sc.nextLine();
                   book1.setTitle(title);
                   break;
               case 2:
                   System.out.println("What do you want the author to be?");
                   String author = sc.nextLine();
                   book1.setAuthor(author);
                   break;
               case 3:
                   System.out.println("What do you want the publisher to be?");
                   String publisher = sc.nextLine();
                   book1.setPublisher(publisher);
                   break;
               case 4:
                   System.out.println("What do you want the ISBN to be?");
                   String ISBN = sc.nextLine();
                   book1.setISBN(ISBN);
                   break;
               case 5:
                   System.out.println("What do you want the price to be?");
                   double price = sc.nextDouble();
                   book1.setPrice(price);;
                   break;
               case 6:
                   System.out.println("What do you want the year to be?");
                   int year = sc.nextInt();
                   book1.setYear(year);
                   break;
               case 7:
                   System.out.println("What do you want the number in stock to be?");
                   int copies = sc.nextInt();
                   book1.setCopies(copies);
                   break;
               case 9:
                   break;
               }
           }
           if (choice == 2)
           {
               printBook(book1);
           }
           if (choice == 3)
           {
               System.out.println("***Change Stock Menu***");
               System.out.println("Select an item and hit enter");
               System.out.println("1 Increase stock");
               System.out.println("2 Decrease stock");
               System.out.println("9 Exit");
               int stockChoice = sc.nextInt();
               switch(stockChoice){
               case 1:
                   System.out.println("How many stock you want to increase");
                   int copies = sc.nextInt();
                   int stock = book1.getCopies();
                   book1.setCopies(copies+stock);
                   break;
               case 2:
                   System.out.println("How many stock you want to decrease");
                   int decCopies = sc.nextInt();
                   int inStock = book1.getCopies();
                   book1.setCopies(inStock-decCopies);
                   break;
               case 9:
                   break;
               }
           }
          
           printBookMenu();
       }
      
   }

  

}