So here is my questions. I am working on building an array using a defined class. The class looks like below:
class InventoryItem {
String ItemName;
int ItemNumber;
int InStock;
double UnitPrice;
double InventoryValue;
public InventoryItem(String ItemName, int ItemNumber, int InStock, double UnitPrice) {
this.ItemName = ItemName;
this.ItemNumber = ItemNumber;
this.InStock = InStock;
this.UnitPrice = UnitPrice;
this.InventoryValue = UnitPrice * InStock;
}
public void output() {
System.out.println("Item Name = " + ItemName);
System.out.println("Item Number = " + ItemNumber);
System.out.println("In Stock = " + InStock);
System.out.println("Item Price = $" + UnitPrice);
System.out.println("Item value of stocked items = $" + InventoryValue);
}
}
This works just fine when I load it with a test example.
However when I try to build the array using this class something breaks.
import java.util.*;
public class Inventory {
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
System.out.println("Welcome to the inventory program\n")
System.out.println("This will help keep track of your inventory of office supplies");
InventoryItem[] Stock;
InventoryItem[10] = Stock;
Stock[1] = new InventoryItem("Test", 123456, 500, .99);
System.out.println("Please enter the Item Nmae");
ItemName = user_input.nextline();
the problem I am having is that it doesn't like the InventoryItem[] Stock; It keeps telling me that it cannot find InventoryItem. I thought that I could use this class as the array basically. I think that I am asking the right questions, but I am new to this so please bear with me. BTW this test example does not work. However this is the initial test example.
public static void main(String[] args) {
InventoryItem Stock = new InventoryItem("TEST", 123456789, 999, 1.25);
Stock.output();
Which works fine to load that one line, but I am working towards having the user input each section of the array in order to load it up. Does that make sense or do I just sound like an idiot? I am not great with the terminology, but I am working on it. Any help would be appreciated.
InventoryItem, it may be thatInventoryItemis in the wrong directory, or needs to be imported. So where is theInventoryItemfile? Also, it's good practice to make your classespublic, unless you have a good reason not to..nextline();is not aScannermethod, but.nextLine()is (inItemName = user_input.nextline();). Also,ItemNamehas not been declared yet, so you might want to change that toString ItemName = ...unless you've declared it somewhere else. Otherwise, the program doesn't know whatItemNameis, and it's not known that it is a variable and is going to store aString. You also seem to be missing a semicolon at the end of the "Welcome" message on line 3 ofmain().