944,110 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1442
  • Java RSS
Apr 24th, 2007
0

Array problem

Expand Post »
I have to create a program for class and I am stuck. Basically, the problem is that we need the manager to enter in the number of pizza toppings on hand at the beginning of the day. Then, when a customer orders a pizza with toppings (ie. small pizza with pepperoni) I have to check it against the list to see if we have enough toppings to make it. If not, give them an error message.

Anyways, I'm stuck on how to split an array so if the customer orders a small pizza with pepperoni, mushroom it checks for pepperoni (if it can make it, subtract it from the number of toppings on hand; then do the same for mushroom etc...

here is my code:

Java Syntax (Toggle Plain Text)
  1. import javax.swing.JOptionPane;
  2.  
  3. public class test
  4. {
  5.  
  6. int pepToppings = 0;
  7. int mushToppings = 0;
  8. int totalToppings = 0;
  9. int smallPizza = 0;
  10. int pizzaCount = 0;
  11. int smallPizzaTotal = 0;
  12.  
  13.  
  14. String[] toppingsArray = { "pepperoni", "mushroom"};
  15. int[] numToppingsArray = { pepToppings, mushToppings };
  16. int[] numToppingsOrderedArray = { pepToppings, mushToppings };
  17.  
  18.  
  19. public void onHand()
  20. {
  21. for( int i = 0; i < toppingsArray.length; i++ )
  22. {
  23. numToppingsArray[i] = Integer.parseInt(JOptionPane.showInputDialog("Please enter number of " + toppingsArray[i] + " toppings on hand:", "0" ));
  24. System.out.println(toppingsArray[i] + " on hand:\t" + numToppingsArray[i]);
  25.  
  26. }
  27. }
  28.  
  29.  
  30.  
  31. public void orderedToppings()
  32. {
  33. //brings up pizza input dialog
  34. smallPizza = Integer.parseInt(JOptionPane.showInputDialog
  35. ( "How many small pizzas would you like to order?" , "0" ));
  36. pizzaCount += smallPizza;
  37.  
  38. //computes total number of small pizzas ordered
  39. smallPizzaTotal = smallPizza + smallPizzaTotal;
  40.  
  41. String smallToppings = JOptionPane.showInputDialog
  42. ( "What kind of toppings do you want on your small pizza?\nWe currently have pepperoni and mushroom.\n" +
  43. "Please enter your toppings with spaces in between (ex: pepperoni mushroom)\nFor cheese only, please click OK." );
  44.  
  45. int smallCount = 0;
  46. int smallToppingsTotal = 0;
  47. String[] arr = smallToppings.split(" ");
  48. smallCount = 0;
  49. for(int i = 0; i <arr.length;i++)
  50. {//start for
  51.  
  52. if(arr[i].equals(""))
  53. {//start if
  54. smallCount = 0;
  55. }//end if
  56.  
  57. else
  58. {//start else
  59. smallCount++;
  60. }//end else
  61.  
  62. }//end for
  63.  
  64. //computes total number of small toppings ordered
  65. smallToppingsTotal = (smallCount * smallPizza) + smallToppingsTotal;
  66.  
  67. String nameToFind = smallToppings;
  68. boolean found = false;
  69. int location = -1;
  70.  
  71. int i = 0;
  72.  
  73. String output = "";
  74.  
  75. do
  76. {
  77. if( nameToFind.equals(toppingsArray[i]) )
  78. {
  79. found = true;
  80. location = i;
  81. if (numToppingsArray[i] > smallToppingsTotal)
  82. {
  83. numToppingsArray[i] = numToppingsArray[i] - smallToppingsTotal;
  84. System.out.println("We can make your pizza.");
  85. }
  86. else
  87. System.out.println("We can't make your pizza.");
  88. }
  89. i++;
  90. }
  91. while( found == false && i < toppingsArray.length );
  92.  
  93. if( location != -1 )
  94. {
  95. if (numToppingsArray[location] <= 0)
  96. {
  97. JOptionPane.showMessageDialog( null, "We are out of those toppings, sorry." );
  98. }
  99. else
  100. {
  101. output += "\n" + nameToFind + " found at nameArray[" + location + "]" + "there are " + numToppingsArray[location] + " left!";
  102. JOptionPane.showMessageDialog( null, output, "Original and Sorted Array",
  103. JOptionPane.INFORMATION_MESSAGE );
  104. }
  105. }
  106. else
  107. {
  108. output += "\n" + nameToFind + " not found in nameArray[]";
  109. JOptionPane.showMessageDialog( null, output, "Original and Sorted Array",
  110. JOptionPane.INFORMATION_MESSAGE );
  111. }
  112.  
  113. }
  114.  
  115. public void check()
  116. {
  117. for( int i = 0; i < toppingsArray.length; i++ )
  118. {
  119.  
  120. System.out.println(toppingsArray[i] + " on hand:\t" + (numToppingsArray[i] - numToppingsOrderedArray[i]));
  121.  
  122. }
  123. }
  124.  
  125.  
  126.  
  127.  
  128.  
  129. public static void main( String[] args )
  130. {
  131.  
  132. test pizza = new test();
  133. pizza.onHand();
  134. pizza.orderedToppings();
  135.  
  136.  
  137. int checkInv = 0;
  138. checkInv = JOptionPane.showConfirmDialog(null , "Would you like to check the inventory?" , "Pizza Palace" , JOptionPane.YES_NO_OPTION);
  139. if (checkInv == JOptionPane.YES_OPTION)
  140. {
  141. pizza.check();
  142. }
  143. else
  144. {
  145. System.out.print("Thanks!");
  146. }
  147.  
  148. }
  149.  
  150. }

It runs when you enter only 1 topping (ie. pepperoni, but not for pepperoni, mushroom)

Any help would be appreciated!

Thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bmxer84 is offline Offline
6 posts
since Apr 2007
Apr 25th, 2007
0

Re: Array problem

I'm not able to understand some parts of your code.... If user enters pepperoni and mushroom together then what does it mean ...?
Will you put both mushroom and pepperoni together in all the ordered pizza or what ...?

I'm assuming that you want to do this. I modified your code according to that.

package com.gaurav;
 
import javax.swing.JOptionPane;
 
publicclass test {
 
int pepToppings = 0;
 
int mushToppings = 0;
 
int totalToppings = 0;
 
int smallPizza = 0;
 
int pizzaCount = 0;
 
int smallPizzaTotal = 0;
 
int totalAvailableToppings = 0;
 
boolean NotEnoughQuantity = false;
 
String[] toppingsArray = { "pepperoni", "mushroom" };
 
int[] numToppingsArray = { pepToppings, mushToppings };
 
int[] numToppingsOrderedArray = { pepToppings, mushToppings };
 
 
 

public void onHand() {
for (int i = 0; i < toppingsArray.length; i++) {
numToppingsArray[i] = Integer.parseInt(JOptionPane.showInputDialog(
"Please enter number of " + toppingsArray
+ " toppings on hand:", "0"));
totalAvailableToppings = totalAvailableToppings
[i]+ numToppingsArray;
[i]System.out.println(toppingsArray + " on hand:\t"
[i]+ numToppingsArray);
}
} public void orderedToppings() {
// brings up pizza input dialog
[i]smallPizza = Integer.parseInt(JOptionPane.showInputDialog(
"How many small pizzas would you like to order?", "0"));
pizzaCount += smallPizza;
// computes total number of small pizzas ordered
smallPizzaTotal = smallPizza + smallPizzaTotal;
String smallToppings = JOptionPane
.showInputDialog("What kind of toppings do you want on your small pizza?\nWe currently have pepperoni and mushroom.\n"
+ "Please enter your toppings with spaces in between (ex: pepperoni mushroom)\nFor cheese only, please click OK.");
String[] arr = smallToppings.split(" ");
for(int j =0;j<arr.length;j++) {
String nameToFind = arr[j];
int location = -1;
for(int i=0;i<toppingsArray.length;i++)
{
if (nameToFind.equals(toppingsArray)) {
location = i;
[i]System.out.println(nameToFind + " found at location " +location);
break;
}
} String output = " "; if ((location !=-1) && arr.length>1 && totalAvailableToppings>=arr.length*smallPizza) {
JOptionPane.showMessageDialog(null, " Checking " + nameToFind + "topping .....",null
,JOptionPane.INFORMATION_MESSAGE);
if(numToppingsArray[location]>=smallPizza)
{
numToppingsArray[location] = numToppingsArray[location]- smallPizza;
System.out.println("Enough " + nameToFind + " is avilable for this order");
} else {
NotEnoughQuantity = true;
}
} else if((location !=-1) && arr.length==1 && totalAvailableToppings>=smallPizza) {
JOptionPane.showMessageDialog(null, " Checking " + nameToFind + " topping .....",null,
JOptionPane.INFORMATION_MESSAGE);
if(numToppingsArray[location]>=smallPizza)
{
numToppingsArray[location] = numToppingsArray[location]- smallPizza;
} else {
NotEnoughQuantity = true;
}
} else if(!(totalAvailableToppings >=arr.length*smallPizza)) {
NotEnoughQuantity = true;
System.out.println(" System crash ");
System.out.println("We can't make your pizza.");
} if (location != -1) {
if (NotEnoughQuantity)
{
JOptionPane.showMessageDialog(null,
"We are out of "+ nameToFind +" topping, sorry.");
} else {
output += "\n Enough " + nameToFind + " topping available for this order";
JOptionPane.showMessageDialog(null, output,
"Original and Sorted Array",
JOptionPane.INFORMATION_MESSAGE);
}
} else {
output += "\n" + nameToFind + " not found in nameArray[]";
JOptionPane.showMessageDialog(null, output,
"Original and Sorted Array",
JOptionPane.INFORMATION_MESSAGE);
}
} } public void check() {
for (int i = 0; i < toppingsArray.length; i++) {
System.out.println(toppingsArray + " on hand:\t"
[i]+ (numToppingsArray[i] - numToppingsOrderedArray));
}
} public static void main(String[] args) {
test pizza = new test();
pizza.onHand();
pizza.orderedToppings();
int checkInv = 0;
[i]checkInv = JOptionPane.showConfirmDialog(null,
"Would you like to check the inventory?", "Pizza Palace",
JOptionPane.YES_NO_OPTION);
if (checkInv == JOptionPane.YES_OPTION) {
pizza.check();
} else {
System.out.print("Thanks!");
}
} }
Last edited by lucky1981_iway; Apr 25th, 2007 at 8:21 am.
Reputation Points: 10
Solved Threads: 3
Light Poster
lucky1981_iway is offline Offline
46 posts
since Apr 2007
Apr 25th, 2007
0

Re: Array problem

Yea, you are right. If a customer orders pepperoni and mushroom, then they get a pepperoni and mushroom pizza. If they order 3 pepperoni and mushroom pizzas then its 6 toppings total (3 pepperoni and 3 mushroom).

I'll try your code out later today and let you know.

Thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bmxer84 is offline Offline
6 posts
since Apr 2007
Apr 25th, 2007
0

Re: Array problem

Well, so far I have gotten the program to check to see if the topping is there. If the topping is there, subtract it from the on hand amount. The problem I'm running into is that it is going through the array too many times.

When a customer orders two or more toppings (ie. pepperoni and mushroom) it goes through the array four times instead of two times (one for each topping).

Here is the code. It runs, but I just can't figure that bug out. Hope for some help .

Java Syntax (Toggle Plain Text)
  1. import javax.swing.JOptionPane;
  2.  
  3. public class testingCode
  4. {
  5.  
  6. int pepToppings = 0;
  7. int mushToppings = 0;
  8. int meatballToppings = 0;
  9. int totalAvailableToppings = 0;
  10.  
  11. int smallPizza = 0;
  12. int pizzaCount = 0;
  13. int smallPizzaTotal = 0;
  14.  
  15. String[] toppingsArray = { "pepperoni", "mushroom", "meatball" };
  16. int[] numToppingsArray = { pepToppings, mushToppings, meatballToppings };
  17. int[] numToppingsOrderedArray = { pepToppings, mushToppings, meatballToppings };
  18.  
  19.  
  20.  
  21. //sets number of toppings on hand
  22. public void onHand()
  23. {//start method onHand
  24.  
  25. for (int i = 0; i < toppingsArray.length; i++)
  26. {//start for
  27.  
  28. numToppingsArray[i] = Integer.parseInt
  29. (JOptionPane.showInputDialog("Please enter number of " + toppingsArray[i] + " toppings on hand:", "10"));
  30.  
  31. totalAvailableToppings = totalAvailableToppings + numToppingsArray[i];
  32.  
  33. System.out.println(toppingsArray[i] + " on hand:\t" + numToppingsArray[i]);
  34.  
  35. }//end for
  36. }//end method onHand
  37.  
  38.  
  39.  
  40. public void order()
  41. {//start method order
  42.  
  43. //asks customer how many small pizzas they would like to order
  44. smallPizza = Integer.parseInt
  45. (JOptionPane.showInputDialog("How many small pizzas would you like to order?", "5"));
  46.  
  47. pizzaCount += smallPizza;
  48.  
  49. smallPizzaTotal = smallPizza + smallPizzaTotal;
  50.  
  51. //asks the customer what kind of toppings they want
  52. String smallToppings =
  53. JOptionPane.showInputDialog("What kind of toppings do you want on your small pizza?", "pepperoni mushroom");
  54.  
  55.  
  56. //splits each toppings into an array
  57. String[] arr = smallToppings.split(" ");
  58.  
  59. int smallCount = 0;
  60.  
  61. //counts how many toppings there are (just by words)
  62. for(int k = 0; k <arr.length;k++)
  63. {//start for
  64. smallCount = 0;
  65. if(arr[k].equals(""))
  66. {//start if
  67. smallCount = 0;
  68. }//end if
  69.  
  70. else
  71. {//start else
  72. smallCount++;
  73. }//end else
  74.  
  75. //searches for topping in toppingsArray
  76. for(int j = 0; j <arr.length;j++)
  77. {//start for
  78. //sets arr[j] to nameToFind (String to search for)
  79. String nameToFind = arr[j];
  80.  
  81. //sets number of toppings ordered to an array
  82. numToppingsOrderedArray[k] = (smallCount * smallPizza);
  83.  
  84.  
  85. //if found, check to see if there are enough, otherwise we can't make it
  86. for(int i = 0; i <toppingsArray.length;i++)
  87. {//start for
  88. if (nameToFind.equals(toppingsArray[i]))
  89. {//start if
  90.  
  91. //total toppings ordered (ie. 5 small pepperoni pizzas, charge them for 5 pepperoni toppings)
  92. System.out.println("\nTotal " + toppingsArray[i] + " ordered: " + numToppingsOrderedArray[k]);
  93.  
  94. //doesn't do anything, just text
  95. System.out.println("Checking to see if there are enough " + toppingsArray[i] + " left...");
  96.  
  97. //checks to see if there are enough toppings available
  98. if (numToppingsArray[i] > numToppingsOrderedArray[k])
  99. {//start if
  100. System.out.println("There are enough " + toppingsArray[i] + " to make your pizza.");
  101.  
  102. //removes number of toppings ordered from those on hand
  103. numToppingsArray[i] = numToppingsArray[i] - numToppingsOrderedArray[k];
  104. System.out.println(toppingsArray[i] + " on hand:\t" + (numToppingsArray[i]) + "\n");
  105. }//end if
  106. else
  107. {//start else
  108. System.out.println("We can't make your pizza.\n");
  109. }//end else
  110.  
  111. }//end if
  112. }//end for
  113.  
  114. }//end for
  115.  
  116. }//end for
  117.  
  118. }//end method order
  119.  
  120.  
  121. public void check()
  122. {//start method check
  123.  
  124. for( int i = 0; i < toppingsArray.length; i++ )
  125. {//start for
  126. System.out.println(toppingsArray[i] + " on hand:\t" + numToppingsArray[i]);
  127. }//end for
  128. }//end method check
  129.  
  130.  
  131. public static void main( String[] args )
  132. {//start main
  133. testingCode pizza = new testingCode();
  134. pizza.onHand();
  135. pizza.order();
  136.  
  137.  
  138. int checkInv = 0;
  139. checkInv = JOptionPane.showConfirmDialog(null , "Would you like to check the inventory?" , "Pizza Palace" , JOptionPane.YES_NO_OPTION);
  140. if (checkInv == JOptionPane.YES_OPTION)
  141. {//start if
  142. pizza.check();
  143. }//end if
  144. else
  145. {//start else
  146. System.out.print("Thanks!");
  147. }//end else
  148.  
  149. }//end main
  150.  
  151. }//end class testingcode
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bmxer84 is offline Offline
6 posts
since Apr 2007

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: Blackjack setting Ace value
Next Thread in Java Forum Timeline: sorting members





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


Follow us on Twitter


© 2011 DaniWeb® LLC