Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have an error showing at line 200 and i have exhausted my knowledge on how to find/fix. Can you please help?? Please see bold

I have an error showing at line 200 and i have exhausted my knowledge on how to find/fix. Can you please help?? Please see bold below for " Inventory inventory = new Inventory();"

package CMIS242ASG1AfetseM;

//import utility packages import java.security.InvalidParameterException; import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner;

public class CMIS242ASG1AfetseM { //create book class public class Book { private String id; private String title; private double price; private boolean isDigit(String str) { try { Long.parseLong(str); } catch(NumberFormatException e) { return false; } return true; } //set methods public void setId(String id) { if(id.length() != 5) { throw new InvalidParameterException("Please note the length of Book ID should be 5 digits."); } else if(!isDigit(id)) { throw new InvalidParameterException("Please note the Book ID should consist of only digits."); } this.id = id; } public void setTitle(String title) { this.title = title; } public void setPrice(double price) { this.price = price; } //access methods public String getId() { return id; } public String getTitle() { return title; } public double getPrice() { return price; } //set override for string @Override public String toString() { return "Book [ID=" + id + ", Title=" + title + ", Price=" + price + "]"; }

//create inventory class public class Inventory { //create arraylist ArrayList bookList = new ArrayList(); // add a book to inventory public void add() { Book book = new Book(); Scanner scan = new Scanner(System.in); boolean done = false; while(!done) { System.out.print("Please Enter Book ID(5-digits): "); String id = scan.next(); try { book.setId(id); done = true; } catch(InvalidParameterException e) { System.out.println(e.getMessage()); } scan.nextLine(); } System.out.print("Please Enter Book Title: "); String title = scan.nextLine(); book.setTitle(title);

System.out.print("Please Enter Book Price: "); double price = scan.nextDouble(); book.setPrice(price); bookList.add(book); scan.close(); } //remove a book from inventory public void remove() {

Scanner scan = new Scanner(System.in); boolean done = false; while(!done) { System.out.print("Please Enter Book ID(5-digits): "); String id = scan.next(); Iterator it = bookList.iterator(); while(it.hasNext()) { if(it.next().getId().equals(id)) { it.remove(); done = true; System.out.println("Please note Book with " + id + " is removed from Inventory."); } } if(!done) { System.out.println("Please note Book ID is not found within the Inventory. Please Try Again!"); } } scan.close(); } //find a book within inventory public void find() {

Scanner scan = new Scanner(System.in); boolean done = false; Book book = null; while(!done) { System.out.print("Please Enter Book ID(5-digits): "); String id = scan.next(); Iterator it = bookList.iterator(); while(it.hasNext()) { book = it.next(); if(book.getId().equals(id)) { done = true; System.out.println(book); } } if(!done) { System.out.println("Please note Book ID is not found within the Inventory. Please Try Again!"); } } scan.close(); } //display a book within inventory public void display() { Iterator it = bookList.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } public void displayMenu() { System.out.println("BOOK INVENTORY MENU" + " \t1. Add a Book." + " \t2. Remove a Book." + " \t3. Find a Book." + " \t4. Display ALL Books" + " \t9. Exit"); } }

public static void main(String[] args) { Scanner scan = new Scanner(System.in); //create instance for inventory Inventory inventory = new Inventory(); boolean quit = false; while(!quit) { inventory.displayMenu(); //prompt user to select desired option System.out.print("Please select option: "); int option = scan.nextInt(); switch(option) { case 1: inventory.add(); break;

case 2: inventory.remove(); break;

case 3: inventory.find(); break;

case 4: inventory.display(); break;

case 9: quit = true; break; default: System.out.println("Apologies for that is an invalid entry. Please select a valid option from menu."); } System.out.println(); scan.close(); } } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions

Question

1. Complete the table by filling in the missing data

Answered: 1 week ago