Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java help pleaseIm almost there with the code. I just need a little help pushing me over the finish line please. I created a Java

Java help pleaseIm almost there with the code. I just need a little help pushing me over the finish line please.

I created a Java project that used an array of size 2 that allowed a user to fill inventory (e.g. pens, paper.) in a store. I need to now modify my existing program that now will use an ArrayList and now asks the user to stock inventory in a store with unlimited inventory (no longer limiting the array to just 2).

In my original program I had initialized an array to 2 (e.g. private int size = 2; //initialized value to 2) just for test case purposes. Now I am modifying the program to allow unlimited items to be placed into inventory using the ArrayList.

I have an Inventory class that does NOT need to be modified, and the StoreInventoryMain class which does NOT need to be modified.

The only class that needs to be modified is the Store class, which will use an ArrayList of unknown size (Ive created this new ArrayList already in the Store class (e.g. private ArrayList size = new ArrayList(); ).

The original program used a For loop to go through the array to add the two inventory items. I believe the For loop now needs to change to a while loop? In this new version I need to ask the user if they want to continue adding more inventory items and repeat that work if the answer is Y or y.

For this modified version, I will need to use the get method, (e.g.items.get(i)), and will need to chain the sell method, and the findItem method that is already within the Store class.

So, when the program runs, it should ask the user for inventory information to stock the store (e.g. stapler, hole punch, pen, envelop, etc.), and ask if they want to continue to add more inventory, instead of asking for 2 items and no more. Thus, the user can enter any number of items.

Here is my Store class which I know will need to be modified;

import java.util.*;

public class Store {

//instance variables private ArrayList size = new ArrayList(); //allows for user to enter unlimited inventory (revised program) //private int size = 2; //initialized value to 2 for test case purposes (original program) private Inventory[] items; //array for inventory items //************************************************************************** //constructor space for the array using the "size" variable public Store() { //Initialize Scanner Scanner stdIn = new Scanner(System.in); //fill the array based upon the size items = new Inventory[size]; System.out.println(); System.out.println("Stock this Inventory:"); stockThisInventory(stdIn); }//end constructor Store //**************************************************************************

//method for stocked inventory private void stockThisInventory(Scanner stdIn) { //local variables String name; String choice = "Y"; double price; int qty;

for (int i = 0; i

//method to display and item using a For-Each loop public void display() { for (int i = 0; i

//method to find an item private int findItem(String name) { for (int i = 0; i

//method to sell an item, first find item then call the method //in the Inventory class to sell an item, return price public double sell(String name, int qty) { int index = findItem(name); double price; if (index != -1) { price = items[index].sell(qty); } else { price = -1; }//end for return price; }//end method sell

}//end class Store

Here is my Inventory class which will NOT need to be modified;

public class Inventory {

//instance variables private String name; private double price; private int quantity; //**************************************************************************

//Constructor with 3 parameters for each instance variable public Inventory(String name, double price, int quantity) { this.name = name; this.price = price; this.quantity = quantity; }//end constructor Inventory //**************************************************************************

//methods for name, price, quantity; return each public String getName() { return name; }//end method getName

public double getPrice() { return price; }//end method getPrice

public int getQty() { return quantity; }//end method getQty //**************************************************************************

//method to display item,with name, price, and quantity public String display() { return "Item: " + name + "\t" + "Price: $" + price + "\t" + "Quantity: " + quantity; }//end method display //**************************************************************************

//method to sell with parameter for quantity of item purchased public double sell(int qty) { double pr = 0; if (qty

}//end class Inventory

Here is my StoreInventoryMain class which will NOT need to be modified;

public static void main(String[] args) {

//instance variables String name; int qty; double totPrice; Scanner stdIn = new Scanner(System.in); String choice = "Y";

//instantiate Store variable to call constructor in Store class Store store = new Store(); //call method to display inventory store.display(); //**********************************************************************

//while loop ask user which item to purchase and quantity while (choice.equalsIgnoreCase("y")) { System.out.print("Which item? "); name = stdIn.nextLine(); System.out.print("How many? "); qty = Integer.parseInt(stdIn.nextLine()); //call the sell method in the Store class //which will need the name of the item and quantity totPrice = store.sell(name, qty); if (totPrice == -1) { System.out.println("Can't find this item, sorry "); } else if (totPrice == 0) { System.out.println("We don't have that quantity to sell. "); } else { System.out.printf("Cost: $%.2f ", totPrice); } System.out.println("Inventory Listing"); store.display(); System.out.print("Continue? Y/N "); choice = stdIn.nextLine(); System.out.println(); }//end while }//end class main

}//end class StoreInventoryMain

Here is a sample of the final output;

image text in transcribed

image text in transcribed

Stock thi inventory: Please add an item to inventory Name of inventory item: stapler Price of that item: 10.79 Quantity of that item: 10 Contine? Y/N Y Name of inventory item: hole punch Price of that item: 8.29 Quantity of that item: 20 Continue? Y/N y Name of inventory item: pen Price of that item: 2.50 Quantity of that item: 100 Continue? Y/N y Name of inventory item: envelope Price of that item:.75 Quantity of that item: 200 Continue? Y/N n Inventory Listing Item: stapler Price: $10.79 Quantity: 10 Item: hole punch Price: $8.29 Quantity: 20 Item: pen Price: $2.5 Quantity: 100 Item: envelope Price: $0.75 Quantity: 200 Item 1 Item 2 Item 3 Item 4

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

Students also viewed these Databases questions

Question

4. Identify cultural variations in communication style.

Answered: 1 week ago