When I printout the array contents, it prints null rather than the expected entered data. Will someone please tell me what I am doing wrong? Thanks. My code follows;
import java.util.Scanner;
public class Inventory {
public static void main(String[] args)
{
Scanner input = new Scanner( System.in );
boolean isValid = true;//**
String productName;// = "DVD"//++
int productNumber;//++
int onHand;//++
double price;//++
//double totalInventory;//++
//int count;
double totInventory;//[] = new double[3];//++
//totalInventory = 0;
int count = 0;
while (isValid)
{
System.out.println ("Enter Product Name. Enter exit to quit program");
productName = input.next();
if (!productName.equalsIgnoreCase ("exit"))//++
{
System.out.println ("Enter Item Number: ");
productNumber = input.nextInt();
System.out.println ("Enter number of items in stock: ");
onHand = input.nextInt();
System.out.println ("Enter item price: ");
price = input.nextDouble();
//compute student grade
totInventory = (onHand * price);
//create a new class and pass the values to the method
Product.getData(productName, productNumber, onHand, price, totInventory, count);//+++
count++; //increment the counter to use as the array position in second class//+++
}
else
{
isValid = false;//**
System.out.print ("Item Name" + "\t");
System.out.print ("Item Number" + "\t");
System.out.print ("Items on Hand" + "\t");
System.out.print ("Item Price" + "\t");
System.out.print ("Items Value" + "\t");
System.out.print ("Total Value" + "\n");
//Product.printAll(count);
Product.printArray(count);
//}
//Product.modifyArray(count);
System.out.println ("Thank you, come again.");
break;
}
}
}
}
public class Product {
static int maxlength = 100;
//protected static String pName[] = new String[maxlength];
//protected static int[] iNumber = new int [maxlength];
//protected static int[] iStock = new int [maxlength];
//protected static double[] uPrice = new double [maxlength];
//protected static double[] tInventory = new double [maxlength];
//protected static double totalInventory = 0;
static String pName[] = new String[maxlength];
static int[] iNumber = new int [maxlength];
static int[] iStock = new int [maxlength];
static double[] uPrice = new double [maxlength];
static double[] tInventory = new double [maxlength];
static double totalInventory = 0;
double reStockFee = .05;
double reStockCost = 0;
double totreStock = 0;
public Product(int productNumber, int onHand, double price,
double totInventory, int count) {
// TODO Auto-generated constructor stub
}
//static String pName[] = new String[maxlength];
//static int[] iNumber = new int [maxlength];
//static int[] iStock = new int [maxlength];
//static double[] uPrice = new double [maxlength];
//static double[] tInventory = new double [maxlength];
//static double totalInventory = 0;
//static double reStockFee = .05;
//static double reStockCost = 0;
//static double totreStock = 0;
//load array
public static void getData(String productName, int productNumber, int onHand, double price, double totInventory, int count)
{
//check that count doesn't exceed maximum array length
if (count > maxlength)
{
System.out.println("DATA OVERLOAD!!!!. YOU CRASHED THE SYSTEM!!! Program Exiting!");
System.exit(0);
}
else
{
//store values in the array
pName[count] = productName;
iNumber[count] = productNumber;
iStock[count] = onHand;
uPrice[count] = price;
tInventory[count] = totInventory;
totalInventory += totInventory;
reStockCost += (totInventory * reStockFee);
totreStock = (totalInventory * reStockFee);
// rf.getRestockFee(totInventory);
//Public class productFee
{
// public double getRestockFee(double total)
// {
// double fee = (total * .05);
}
}
}
//}
//print the values in the array
//public static void printAll(int count)
public static void printArray(int count)
{
for (int i=0; i<count; i++ )
{
System.out.print(pName[count]);
System.out.print(iNumber[count]);
System.out.print(iStock[count]);
System.out.print(uPrice[count]);
System.out.print(tInventory[count]);
//System.out.println(pName + "\t");;
//System.out.println(iNumber + "\t");
//System.out.println(iStock + "\t");
//System.out.println(uPrice + "\t");
//System.out.println(tInventory + "\n");
// System.out.printf("%s\n","Restock Fee", reStockCost[count]);
}
System.out.printf("%s$%.2f\n", "Entire Inventory Value: ", totalInventory);
System.out.printf("%s$%.2f\n", "Entire Inventory Restock Cost: ", totreStock);
}
// public static void modifyArray(int count)
{
// Arrays.sort(pName);//, iNumber, iStock, uPrice, tInventory);
//{
//for (int i=0; i<count; i++ )
// {
// System.out.println(pName + "\t");
// System.out.println(iNumber + "\t");
// System.out.println(iStock + "\t");
// System.out.println(uPrice + "\t");
System.out.println(tInventory + "\n" );
}
}