943,839 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1384
  • Java RSS
Feb 5th, 2009
0

GroceryStore Simulation Java help

Expand Post »
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
Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4.  
  5. public class GrocerySim {
  6.  
  7. public static final int MAXNUMBEROFITEMS = 100; // the maximum number of items that can be bought.
  8. static GroceryItem[] dataList=new GroceryItem[MAXNUMBEROFITEMS];
  9. static int inventory=0;
  10. static int quantity=0;
  11. static int productNumber=0;
  12.  
  13.  
  14. /* readIventory will read in the file "inventory.dat" */
  15.  
  16.  
  17. private static void readInventory(String filename)
  18. {
  19. Scanner fileInput;
  20. File inFile = new File("inventory.dat");
  21. try
  22. {
  23. fileInput = new Scanner(inFile);
  24. while(fileInput.hasNext())
  25. {
  26. GroceryItem.readItem(fileInput);
  27. ++inventory; //stick update for inventory
  28. //System.out.print(inventory);
  29. System.out.println("File was read successfully."); // Debug to make sure file was read in correctly.
  30. if(inventory>100)
  31. {
  32. {
  33. System.out.println("Item Number "+dataList[inventory].getProductNumber()+" has already been added.");
  34. }
  35. }
  36.  
  37. }
  38.  
  39. }
  40. catch(FileNotFoundException e)
  41. {
  42. System.out.println(e);
  43. System.exit(1);
  44. }
  45. }
  46. /*readCustomerInput(Scanner keyboard) will get the user input for the product number and
  47.   * quantity of that item they want.
  48.   */
  49. public static void readCustomerInput(Scanner keyboard)
  50. {
  51.  
  52. System.out.print("Enter Product Number and Quantity:"+'\n');
  53. while(keyboard.hasNext())
  54. {
  55. if(productNumber == 0 && quantity == 0)
  56. {
  57. productNumber=keyboard.nextInt();
  58. quantity=keyboard.nextInt();
  59. }
  60. else break;
  61.  
  62. }
  63. }
  64. // System.out.println(productNumber+ " "+quantity); // debug to make sure productNumber and quantity are being read in.
  65.  
  66.  
  67.  
  68.  
  69.  
  70. /*calcCustomerReciept() will calculate the customers order displaying the subtotal, tax,
  71.  * and final price.
  72.  */
  73. public static void calcCustomerReciept()
  74. {
  75. int item=0;
  76. int numberOfCustomers=1;
  77. int nonFoodItem=0; // will determine if the item is a nonFoodItem.
  78. int foodItem=0; // will determine if the item is a foodItem.
  79. double foodTax = 0.2; // food tax is 2%
  80. double nonFoodTax = 0.7; // nonfood tax is 7%
  81.  
  82. System.out.println('\n'+"Customer"+" "+numberOfCustomers+'\n');
  83. // System.out.println(productNumber+ " "+quantity);
  84.  
  85. while(productNumber == 0 && quantity== 0);
  86.  
  87. {
  88. //System.out.println("stop");
  89. item=GroceryItem.itemSearch(dataList,inventory,productNumber);
  90. if(item!=-1)
  91. {
  92. double priceTimesQuantity = dataList[item].getPrice()*quantity*100.0f/100.0f;
  93. if(dataList[item].getTax() == 'N')
  94. {
  95. nonFoodItem += priceTimesQuantity;
  96. }
  97. else
  98. {
  99. foodItem += priceTimesQuantity;
  100. }
  101.  
  102. System.out.println(dataList[item].getDescription()+"\t"+
  103. quantity+" @ "+ "$ "+ (dataList[item].getPrice())*100.0f/100.0f+
  104. " "+"\t"+"$ "+priceTimesQuantity+" "+dataList[item].getTax()); // displays the quantity followed by the price
  105. } // then quantity times price and finally the food tax.)
  106. //{
  107. // System.out.println("Item number "+productNumber+" is not in located in the inventory.");
  108.  
  109. //}
  110.  
  111. float subtotal = nonFoodItem+foodItem*100.0f/100.0f; // subtotal = the addition of both food and nonfood items
  112. double tax = (foodItem*foodTax)+(nonFoodItem*nonFoodTax)*100.0f/100.0f; // tax is food items * 2% and non food * 7%
  113.  
  114. System.out.println('\n'+"\t"+"Subtotal:\t"+"$ "+subtotal); //display the subtotal
  115. System.out.println("\t"+"Tax:\t\t"+"$ "+tax+'\n'); // display the tax
  116. System.out.println("\t"+"Total:\t\t"+"$ "+(subtotal+tax)*100.0f/100.0f+'\n'); // display the final total
  117. numberOfCustomers++; //begin new customer
  118.  
  119. }
  120. }
  121.  
  122.  
  123.  
  124. public static void main( String [] args)
  125. {
  126. Scanner keyboard = new Scanner(System.in);
  127. readInventory("inventory.dat");
  128. readCustomerInput(keyboard);
  129. calcCustomerReciept();
  130.  
  131.  
  132.  
  133. }
  134.  
  135. }

Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2.  
  3. public class GroceryItem
  4. {
  5.  
  6.  
  7.  
  8. private static int productNumber; // the product number for an item
  9. private static String description; // the description of the item
  10. private static double price; // the price for the item
  11. private static char tax; // the tax for the item
  12.  
  13. // accessor methods
  14.  
  15. public int getProductNumber() { return productNumber;}
  16. public String getDescription() { return description;}
  17. public double getPrice() { return price;}
  18. public char getTax() { return tax;}
  19.  
  20.  
  21. //*******************************************************************************************
  22.  
  23. /* readItem(Scanner file) will read in the productNumber, description, price and tax from
  24.   * the inventory data, it will then leave the position of the object on the next input line.
  25.   */
  26.  
  27. public static void readItem(Scanner file)
  28. {
  29. while(file.hasNext())
  30. {
  31. productNumber= file.nextInt();
  32. description=file.next();
  33. price=file.nextDouble();
  34. tax=(file.next()).charAt(0);
  35. //System.out.print(productNumber+" "+ description+" "+ price+" "+ tax+" "); // debug test
  36. file.hasNextLine(); //leaves at the next input line
  37. }
  38.  
  39. }
  40.  
  41. /* itemSearch(GroceryItem [] data, int elementsUsed, int productItem) searches the array of items
  42.   * looking for the item that matches productItem. It if cannot find it wil return -1.
  43.   */
  44.  
  45. public static int itemSearch(GroceryItem [] data, int elementsUsed , int productItem)
  46. {
  47.  
  48. int index = -1;
  49. for(int i = 0; i < elementsUsed; i++)
  50. {
  51. System.out.print(productItem);
  52. if(data[i].getProductNumber() == productItem)
  53. {
  54. index = i;
  55. break;
  56. }
  57. }
  58. return index;
  59. }
  60.  
  61. /* printIventory(GroceryItem[] data, int elementsUsed) prints all the inventory that is
  62.   * from i to i<elementsUsed.
  63.   */
  64. public static void printInventory(GroceryItem[] data, int elementsUsed)
  65. {
  66. for(int i=0; i<elementsUsed; i++)
  67. {
  68. System.out.printf("\t"+data[i].getProductNumber()+"\t"+data[i].getDescription()+"\t"
  69. +"\t"+data[i].getPrice()+"\t"+data[i].getTax()+'\n');
  70. }
  71. }
  72.  
  73. }
Reputation Points: 46
Solved Threads: 0
Newbie Poster
yingfo is offline Offline
13 posts
since Sep 2008
Feb 5th, 2009
0

Re: GroceryStore Simulation Java help

it's a bit hard to debug is just looking at the code and I can't really be bothered compiling it myself. But since it is a nullpointer I think that one of your objects in itemSearch is not initiliased properly.
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Feb 6th, 2009
0

Re: GroceryStore Simulation Java help

this program needs a lot of improvements
you dont have any customers array
or even shop list for a single customer included in the program
i fixed some major bugs so now it works fine at least for one item entered

you obviously need to read more about oo programming

iv included some comments in the code below
java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4.  
  5. public class GrocerySim {
  6.  
  7. // consider to use array list instead of table
  8. public static final int MAXNUMBEROFITEMS = 100; // the maximum number of items that can be bought.
  9. static GroceryItem[] dataList=new GroceryItem[MAXNUMBEROFITEMS];
  10. static int inventory=0;
  11. static int quantity=0;
  12. static int productNumber=0;
  13.  
  14.  
  15. /* readIventory will read in the file "inventory.dat" */
  16.  
  17.  
  18. private static void readInventory(String filename)
  19. {
  20. Scanner fileInput;
  21. File inFile = new File("inventory.dat");
  22. try
  23. {
  24. fileInput = new Scanner(inFile);
  25. while(fileInput.hasNext())
  26. {
  27. //You havent got items stored!!!
  28. GroceryItem tempItem = new GroceryItem(); // creating new item
  29. tempItem.readItem(fileInput); // filling it with data
  30. dataList[inventory] = tempItem; // putting it in table
  31.  
  32.  
  33. ++inventory; //stick update for inventory
  34. //System.out.print(inventory);
  35.  
  36. if(inventory>100)
  37. {
  38. {
  39. System.out.println("Item Number "+dataList[inventory].getProductNumber()+" has already been added.");
  40. }
  41. }
  42.  
  43. }
  44.  
  45. }
  46. catch(FileNotFoundException e)
  47. {
  48. System.out.println(e);
  49. System.exit(1);
  50. }
  51. }
  52. /*readCustomerInput(Scanner keyboard) will get the user input for the product number and
  53.   * quantity of that item they want.
  54.   */
  55. public static void readCustomerInput(Scanner keyboard)
  56. {
  57.  
  58. System.out.print("Enter Product Number and Quantity:"+'\n');
  59. while(keyboard.hasNext())
  60. {
  61. if(productNumber == 0 && quantity == 0)
  62. {
  63. productNumber=keyboard.nextInt();
  64. quantity=keyboard.nextInt();
  65. // the values are not stored anywhere,
  66. // so you only have one item for 1 customer
  67.  
  68. }
  69. else break;
  70.  
  71. }
  72. }
  73. // System.out.println(productNumber+ " "+quantity); // debug to make sure productNumber and quantity are being read in.
  74.  
  75.  
  76.  
  77.  
  78.  
  79. /*calcCustomerReciept() will calculate the customers order displaying the subtotal, tax,
  80.  * and final price.
  81.  */
  82. public static void calcCustomerReciept()
  83. {
  84. int item=0;
  85. int numberOfCustomers=1;
  86. int nonFoodItem=0; // will determine if the item is a nonFoodItem.
  87. int foodItem=0; // will determine if the item is a foodItem.
  88. double foodTax = 0.2; // food tax is 2%
  89. double nonFoodTax = 0.7; // nonfood tax is 7%
  90.  
  91. System.out.println('\n'+"Customer"+" "+numberOfCustomers+'\n');
  92. System.out.println("debug: "+productNumber+ " "+quantity);
  93.  
  94. // this loop makes mo sense at all
  95. // while(productNumber == 0 && quantity== 0)
  96.  
  97. //System.out.println("stop");
  98. item=GroceryItem.itemSearch(dataList,inventory,productNumber);
  99.  
  100. if(item!=-1)
  101. {
  102. double priceTimesQuantity = dataList[item].getPrice()*quantity*100.0f/100.0f;
  103. if(dataList[item].getTax() == 'N')
  104. {
  105. nonFoodItem += priceTimesQuantity;
  106. }
  107. else
  108. {
  109. foodItem += priceTimesQuantity;
  110. }
  111.  
  112. System.out.println(dataList[item].getDescription()+"\t"+
  113. quantity+" @ "+ "$ "+ (dataList[item].getPrice())*100.0f/100.0f+
  114. " "+"\t"+"$ "+priceTimesQuantity+" "+dataList[item].getTax()); // displays the quantity followed by the price
  115. } // then quantity times price and finally the food tax.)
  116. //{
  117. // System.out.println("Item number "+productNumber+" is not in located in the inventory.");
  118.  
  119. //}
  120.  
  121. float subtotal = nonFoodItem+foodItem*100.0f/100.0f; // subtotal = the addition of both food and nonfood items
  122. double tax = (foodItem*foodTax)+(nonFoodItem*nonFoodTax)*100.0f/100.0f; // tax is food items * 2% and non food * 7%
  123.  
  124. System.out.println('\n'+"\t"+"Subtotal:\t"+"$ "+subtotal); //display the subtotal
  125. System.out.println("\t"+"Tax:\t\t"+"$ "+tax+'\n'); // display the tax
  126. System.out.println("\t"+"Total:\t\t"+"$ "+(subtotal+tax)*100.0f/100.0f+'\n'); // display the final total
  127. numberOfCustomers++; //begin new customer
  128.  
  129.  
  130. }
  131.  
  132.  
  133.  
  134. public static void main( String [] args)
  135. {
  136. Scanner keyboard = new Scanner(System.in);
  137. readInventory("inventory.dat");
  138. readCustomerInput(keyboard);
  139. calcCustomerReciept();
  140.  
  141.  
  142.  
  143. }
  144.  
  145. }
java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2.  
  3. public class GroceryItem
  4. {
  5. // static means that you have only one variable per class!!!
  6.  
  7.  
  8. private int productNumber; // the product number for an item
  9. private String description; // the description of the item
  10. private double price; // the price for the item
  11. private char tax; // the tax for the item
  12.  
  13. // accessor methods
  14.  
  15. public int getProductNumber() { return productNumber;}
  16. public String getDescription() { return description;}
  17. public double getPrice() { return price;}
  18. public char getTax() { return tax;}
  19.  
  20.  
  21. //*******************************************************************************************
  22.  
  23. /* readItem(Scanner file) will read in the productNumber, description, price and tax from
  24.   * the inventory data, it will then leave the position of the object on the next input line.
  25.   */
  26.  
  27. public GroceryItem readItem(Scanner file) // it should return new object containing data
  28. {
  29. // while(file.hasNext())
  30. // you don't need that loop here it reads trough all file
  31. // only most recent line in the object and leves
  32. //{
  33.  
  34. productNumber= file.nextInt();
  35. description=file.next();
  36. price=file.nextDouble();
  37. tax=(file.next()).charAt(0);
  38. //System.out.print(productNumber+" "+ description+" "+ price+" "+ tax+" "); // debug test
  39. file.hasNextLine(); //leaves at the next input line
  40. //}
  41. return this;
  42.  
  43. }
  44.  
  45. /* itemSearch(GroceryItem [] data, int elementsUsed, int productItem) searches the array of items
  46.   * looking for the item that matches productItem. It if cannot find it wil return -1.
  47.   */
  48.  
  49. public static int itemSearch(GroceryItem [] data, int elementsUsed , int productItem)
  50. {
  51.  
  52. int index = -1;
  53. for(int i = 0; i < elementsUsed; i++)
  54. {
  55. System.out.print("debug: "+data[i].getProductNumber());
  56. if(data[i].getProductNumber() == productItem)
  57. {
  58. index = i;
  59. break;
  60. }
  61. }
  62. return index;
  63. }
  64.  
  65. /* printIventory(GroceryItem[] data, int elementsUsed) prints all the inventory that is
  66.   * from i to i<elementsUsed.
  67.   */
  68. public static void printInventory(GroceryItem[] data, int elementsUsed)
  69. {
  70. for(int i=0; i<elementsUsed; i++)
  71. {
  72. System.out.printf("\t"+data[i].getProductNumber()+"\t"+data[i].getDescription()+"\t"
  73. +"\t"+data[i].getPrice()+"\t"+data[i].getTax()+'\n');
  74. }
  75. }
  76.  
  77. }
Reputation Points: 14
Solved Threads: 3
Newbie Poster
arseniew is offline Offline
14 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: brightest red spot detection
Next Thread in Java Forum Timeline: java loop problem help?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC