Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

you will implement a generic class InventoryItem and test it by creating an ArrayList that stores such items. The Car class has been given to

you will implement a generic class InventoryItem and test it by creating an ArrayList that stores such items. The Car class has been given to you, as well as some implementation in InventoryTester. Please complete the following three tasks:

1) Complete the implementation of the class InventoryItem. It should:

  • Store an item of type T
  • Store a price of type double
  • Have a constructor that initializes the item and price
  • Have a getItem() method
  • Have a getPrice() method
  • Have a toString() method (this one has already been implemented for you).

2) In the main method of InventoryTester, code has been provided to read user input for creating Car objects. We will create inventory items that store items of type Car together with a price for that car. Please complete the implementation as follows (see \\TODO comments in the code):

  • Create an ArrayList called carsInventory that stores inventory items of cars.
  • In the for-loop create a Car object with the information that was read (make, model, color). Then create an InventoryItem with that Car as item and with the price that was read. And then add that InventoryItem to the carsInventory list.
  • Finally, comment out the line that prints carsInventory after the for-loop.

3) In the main method of InventoryTester, add a try-catch block around the for-loop to catch an `InputMismatchException'. When the exception is caught, the following should be printed: Wrong input format. Car not read.

Please implement this according to the specifications above and using best object-oriented programming principles . Any missing details should be filled in using best object-oriented programming principles.

GIVEN CODE

---------------------

import java.util.Scanner; import java.util.ArrayList; import java.util.InputMismatchException;

public class InventoryTester{

public static void main(String[] args){

System.out.print("Number of cars to enter: "); Scanner scnr = new Scanner(System.in); int num = scnr.nextInt();

// TODO: Task 2: Declare Arraylist carsInventory here

System.out.println("Please enter "+num+" cars: make model color price (the price is a double)"); // TODO: Task 3: Catch InputMismatchException for(int i=0; i

// TODO: Task 2: Add InventoryItem with Car and price into the carsInventory list }

// TODO: Task 2: Uncomment the line below once you've declared carsInventory above //System.out.println(carsInventory);

}

}

--------------------------------------------------

// TODO: Task 1: Finish the implementation of the InventoryItem class according to specifications public class ..... { // TODO: Add variables

// TODO: Constructor

// TODO: getItem() method

// TODO getPrice() method // This toString method is given to you. @Override public String toString(){ return getItem()+" with price "+getPrice(); }

} -----------------------------------------------------

// This class is given to you. Do not edit it. public class Car{ private String make; private String model; private String color;

public Car(String make, String model, String color){ this.make = make; this.model = model; this.color = color; }

@Override public String toString(){ return color+" "+make+" "+model; }

}

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

Step: 3

blur-text-image

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

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions

Question

How many multiples of 4 are there between 10 and 250?

Answered: 1 week ago

Question

Question Can a Roth IRA invest in stock of the IRA owners business?

Answered: 1 week ago