Question
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
1) Complete the implementation of the class InventoryItem
- 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started