Question
JAVA ...little help please :) I'm working on an project and a little stuck... THANK YOU IN ADVANCE for any thoughts and guidance you may
JAVA ...little help please :) I'm working on an project and a little stuck... THANK YOU IN ADVANCE for any thoughts and guidance you may provide
There will be 2 classes and a driver class. One class describes a single item in inventory. One class describe the inventory of a store, multiple items stored in an array.
Create a class named Inventory, which has instance variables for the name, price and quantity of each inventory item. The constructor needs 3 parameters, one for each instance variable. Include separate methods to get the name, the price, and the quantity of an item. Create a method to display an item, with its name, price, and quantity on the same line return the string with this data, do not print to the screen in this method. Create a method to sell, which needs as a parameter the quantity of this item being purchased, and returns the price of the item, the instance variable for price of a single item (not the cost of this item times the quantity purchased in this sale). In this method, test if there is enough of the item in inventory for the purchase, and if there is, subtract the quantity from the inventory and return the items price. If there is not enough quantity, return a value of 0 this price of zero means the purchase was not completed.
Create a class named Store, which uses an array to hold the inventory for its items. This class needs instance variables for the size of the inventory for this exercise, initialize that value to 2, but be sure to use the variable in the code, do not hard-code the value of 2 anywhere else, only as the initialization value. The class also needs an instance variable for an array of Inventory items declare its name and type in the method variables only. In the constructor, allocate the space for the array using the size variable. This allows for future modification of the program, where the user can specify how many items are in inventory. If the size is not known until after the user provides the number, then the array cant be allocated until after that interaction occurs. That is, you cant write the combined declaration and allocation of the array in one line of code you have to declare it with the method variables, and allocate it (using the new keyword) in the constructor. The constructor needs to ask the user for the name of each inventory item, price, and quantity, and store the data in the array.
The Store class also needs methods to find an item in the array, display an item, and sell an item. When trying to find an item in the inventory, return either the index into the array where it exists, or -1 if it is not found. In the method to display, use a For-Each loop to call the method in the Inventory class for displaying the data for an item note this MUST be a For-Each loop, not a regular For loop. In the method to sell an item, first find the item, and if it is found, call the method in the Inventory class that sells a quantity of an item and returns its price. If the item is not found, print a message that the item cant be found, and return a value of 0 (a price of 0 means it wasnt found or the item doesnt have enough quantity for the sale).
In the driver class, instantiate a Store variable. That will call the constructor in Store that will ask the user about the items and fill the array of inventory items. Then call the method to display the inventory. In a loop, ask the user which item they want to purchase and the quantity. Call the sell method in the Store class, which will need the name of the item and the quantity to purchase, and which returns the price of the item, so call this method in an assignment statement, assigning the results to a cost variable. If the cost is 0, print a message that there is not sufficient quantity for that sale, or print the cost of the purchase (price * quantity). Then ask if the user wants to continue. Be sure to format the cost as a money transaction (dollar sign, 2 decimal places).
Note that when the inventory is displayed after a sale, it will show a smaller quantity of that item in inventory.
Here is a UML diagram for this project.
Note that there is no method in the Inventory class that uses the keyboard or screen. The Store class asks the user for inventory information and also displays the inventory on the screen.
The driver class works in a loop to ask the user which item and the quantity of the purchase. If the item exists and there is sufficient inventory, the price for a single item is returned by the sell() method. A price of 0 means there was not enough inventory for that quantity of purchase. If the price is not 0, the driver class calculates the purchase cost of (quantity * price) and displays that. The inventory is displayed, and should show a different quantity if the purchase was successful. The user is asked if they want to continue; if yes, repeat the loop; if no, close the program.
HERE IS MY INVENTORY CLASS
public class Inventory {
//Instance variables private String name; //Product Inventory Name private double price; //Product Price int quantity; //Product Quantity //**************************************************************************
//Constructor method with 3 parameters for each variable public Inventory(String name, double price, int quantity) { this.name = name; this.price = price; this.quantity = quantity; }//end method Inventory
public String getName() { return name; }//end method getName
public double getPrice() { return price; }//end method getPrice
public int getQuantity() { return quantity; }//end method getQuantity //****************************************************************************** //method to display an item with name, price, quantity of item. //return this String with this data do not print to the screen with this data public String Display(){ }// end method Display //************************************************************************** //method to sell, quantity of item to purchase, return price of the item //test to see if there is enough inventory, and if so subtract quantity from inventory //return item price public double Sell() { }//end method Sell }//end class Inventory
HERE IS MY STORE CLASS
public class Store { //instance variables private int size; private Inventory[] items = new Inventory[2]; //************************************************************************** //constructor to allocate space for the array using the size variable //this allows for future modification of the program where user can specify //how many items are in inventory public Store() { }//end constructor Store //************************************************************************** public void display() { //this will display the printing to the screen }//end method Display //************************************************************************** //three methods to find an item, display an item, and sell an item private int findItem (String name){ }//end method findItem //display method //using a "For-each Loop" to call the method in the inventory class to display public String display(){ ??????? }//end display method ??????? //sell method public double sell(){ ???????? }//end sell method //************************************************************************** }//end class Store
HERE IS MY MAIN DRIVER CLASS
public class StoreInventoryMain {
/** * @param args the command line arguments */ public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in); Inventory Store = new Inventory(name, 0, 0); String name; double price; int quantity;
System.out.print("Stock this inventory: ");
System.out.print("Name of inventory: "); name = stdIn.next(); System.out.print("Price of that item: "); price = stdIn.nextDouble(); System.out.println("Quantity of that item: "); quantity = stdIn.nextInt(); System.out.print("Inventory Listing: ");
}//end main
}//end class StoreInventoryMain
HERE IS THE SAMPLE OUTPUT
Store class size: int -items : Inventory[] Inventory class name: String -price : double -quantity : int +Inventory(name String, price : double, +Store() (asks user for inventory data) +display) void (does the printing to the screen) -findltem (name String): int +sell(name : String, qty : int): double quantity : int) +getName(): String +getPrice() double +getQty(): int +display(): String (does not print to the screen) +sell (qty) : price Store class size: int -items : Inventory[] Inventory class name: String -price : double -quantity : int +Inventory(name String, price : double, +Store() (asks user for inventory data) +display) void (does the printing to the screen) -findltem (name String): int +sell(name : String, qty : int): double quantity : int) +getName(): String +getPrice() double +getQty(): int +display(): String (does not print to the screen) +sell (qty) : priceStep 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