Hi I'm having trouble with this program I am working on. It is to simulate a grocery store. The program will read in a file which is called inventory.dat
the file will have items in it like:
11012 gallon-milk 1.99 F
11014 butter 2.59 F
Then a customer will input the product number then the quantity they want
then they will type 0 0 to end their order. When that happen it suppose to ouput a reicpet.
Customer Input
11012 1
11014 1
0 0
Output Recipet
Customer 1
gallon-milk 1@1.99
butter 1@2.59
Subtotal:
Tax:
Total:
I keep getting the errors
Exception in thread "main" java.lang.NullPointerException
at GroceryItem.itemSearch(GroceryItem.java:64)
at GrocerySim.calcCustomerReciept(GrocerySim.java:98)
at GrocerySim.main(GrocerySim.java:138)
I've gotten lost in my code and I'm having a hard time trying to figure out what to do next.
Here is my code for it in two parts GroceryItem and GrocerySim
import java.util.*;
import java.io.*;
public class GrocerySim {
public static final int MAXNUMBEROFITEMS = 100; // the maximum number of items that can be bought.
static GroceryItem[] dataList=new GroceryItem[MAXNUMBEROFITEMS];
static int inventory=0;
static int quantity=0;
static int productNumber=0;
/* readIventory will read in the file "inventory.dat" */
private static void readInventory(String filename)
{
Scanner fileInput;
File inFile = new File("inventory.dat");
try
{
fileInput = new Scanner(inFile);
while(fileInput.hasNext())
{
GroceryItem.readItem(fileInput);
++inventory; //stick update for inventory
//System.out.print(inventory);
System.out.println("File was read successfully."); // Debug to make sure file was read in correctly.
if(inventory>100)
{
{
System.out.println("Item Number "+dataList[inventory].getProductNumber()+" has already been added.");
}
}
}
}
catch(FileNotFoundException e)
{
System.out.println(e);
System.exit(1);
}
}
/*readCustomerInput(Scanner keyboard) will get the user input for the product number and
* quantity of that item they want.
*/
public static void readCustomerInput(Scanner keyboard)
{
System.out.print("Enter Product Number and Quantity:"+'\n');
while(keyboard.hasNext())
{
if(productNumber == 0 && quantity == 0)
{
productNumber=keyboard.nextInt();
quantity=keyboard.nextInt();
}
else break;
}
}
// System.out.println(productNumber+ " "+quantity); // debug to make sure productNumber and quantity are being read in.
/*calcCustomerReciept() will calculate the customers order displaying the subtotal, tax,
* and final price.
*/
public static void calcCustomerReciept()
{
int item=0;
int numberOfCustomers=1;
int nonFoodItem=0; // will determine if the item is a nonFoodItem.
int foodItem=0; // will determine if the item is a foodItem.
double foodTax = 0.2; // food tax is 2%
double nonFoodTax = 0.7; // nonfood tax is 7%
System.out.println('\n'+"Customer"+" "+numberOfCustomers+'\n');
// System.out.println(productNumber+ " "+quantity);
while(productNumber == 0 && quantity== 0);
{
//System.out.println("stop");
item=GroceryItem.itemSearch(dataList,inventory,productNumber);
if(item!=-1)
{
double priceTimesQuantity = dataList[item].getPrice()*quantity*100.0f/100.0f;
if(dataList[item].getTax() == 'N')
{
nonFoodItem += priceTimesQuantity;
}
else
{
foodItem += priceTimesQuantity;
}
System.out.println(dataList[item].getDescription()+"\t"+
quantity+" @ "+ "$ "+ (dataList[item].getPrice())*100.0f/100.0f+
" "+"\t"+"$ "+priceTimesQuantity+" "+dataList[item].getTax()); // displays the quantity followed by the price
} // then quantity times price and finally the food tax.)
//{
// System.out.println("Item number "+productNumber+" is not in located in the inventory.");
//}
float subtotal = nonFoodItem+foodItem*100.0f/100.0f; // subtotal = the addition of both food and nonfood items
double tax = (foodItem*foodTax)+(nonFoodItem*nonFoodTax)*100.0f/100.0f; // tax is food items * 2% and non food * 7%
System.out.println('\n'+"\t"+"Subtotal:\t"+"$ "+subtotal); //display the subtotal
System.out.println("\t"+"Tax:\t\t"+"$ "+tax+'\n'); // display the tax
System.out.println("\t"+"Total:\t\t"+"$ "+(subtotal+tax)*100.0f/100.0f+'\n'); // display the final total
numberOfCustomers++; //begin new customer
}
}
public static void main( String [] args)
{
Scanner keyboard = new Scanner(System.in);
readInventory("inventory.dat");
readCustomerInput(keyboard);
calcCustomerReciept();
}
}
import java.util.*;
public class GroceryItem
{
private static int productNumber; // the product number for an item
private static String description; // the description of the item
private static double price; // the price for the item
private static char tax; // the tax for the item
// accessor methods
public int getProductNumber() { return productNumber;}
public String getDescription() { return description;}
public double getPrice() { return price;}
public char getTax() { return tax;}
//*******************************************************************************************
/* readItem(Scanner file) will read in the productNumber, description, price and tax from
* the inventory data, it will then leave the position of the object on the next input line.
*/
public static void readItem(Scanner file)
{
while(file.hasNext())
{
productNumber= file.nextInt();
description=file.next();
price=file.nextDouble();
tax=(file.next()).charAt(0);
//System.out.print(productNumber+" "+ description+" "+ price+" "+ tax+" "); // debug test
file.hasNextLine(); //leaves at the next input line
}
}
/* itemSearch(GroceryItem [] data, int elementsUsed, int productItem) searches the array of items
* looking for the item that matches productItem. It if cannot find it wil return -1.
*/
public static int itemSearch(GroceryItem [] data, int elementsUsed , int productItem)
{
int index = -1;
for(int i = 0; i < elementsUsed; i++)
{
System.out.print(productItem);
if(data[i].getProductNumber() == productItem)
{
index = i;
break;
}
}
return index;
}
/* printIventory(GroceryItem[] data, int elementsUsed) prints all the inventory that is
* from i to i<elementsUsed.
*/
public static void printInventory(GroceryItem[] data, int elementsUsed)
{
for(int i=0; i<elementsUsed; i++)
{
System.out.printf("\t"+data[i].getProductNumber()+"\t"+data[i].getDescription()+"\t"
+"\t"+data[i].getPrice()+"\t"+data[i].getTax()+'\n');
}
}
}