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:
import javax.swing.JOptionPane;
public class test
{
int pepToppings = 0;
int mushToppings = 0;
int totalToppings = 0;
int smallPizza = 0;
int pizzaCount = 0;
int smallPizzaTotal = 0;
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[i] + " toppings on hand:", "0" ));
System.out.println(toppingsArray[i] + " on hand:\t" + numToppingsArray[i]);
}
}
public void orderedToppings()
{
//brings up pizza input dialog
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." );
int smallCount = 0;
int smallToppingsTotal = 0;
String[] arr = smallToppings.split(" ");
smallCount = 0;
for(int i = 0; i <arr.length;i++)
{//start for
if(arr[i].equals(""))
{//start if
smallCount = 0;
}//end if
else
{//start else
smallCount++;
}//end else
}//end for
//computes total number of small toppings ordered
smallToppingsTotal = (smallCount * smallPizza) + smallToppingsTotal;
String nameToFind = smallToppings;
boolean found = false;
int location = -1;
int i = 0;
String output = "";
do
{
if( nameToFind.equals(toppingsArray[i]) )
{
found = true;
location = i;
if (numToppingsArray[i] > smallToppingsTotal)
{
numToppingsArray[i] = numToppingsArray[i] - smallToppingsTotal;
System.out.println("We can make your pizza.");
}
else
System.out.println("We can't make your pizza.");
}
i++;
}
while( found == false && i < toppingsArray.length );
if( location != -1 )
{
if (numToppingsArray[location] <= 0)
{
JOptionPane.showMessageDialog( null, "We are out of those toppings, sorry." );
}
else
{
output += "\n" + nameToFind + " found at nameArray[" + location + "]" + "there are " + numToppingsArray[location] + " left!";
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[i] + " on hand:\t" + (numToppingsArray[i] - numToppingsOrderedArray[i]));
}
}
public static void main( String[] args )
{
test pizza = new test();
pizza.onHand();
pizza.orderedToppings();
int checkInv = 0;
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!");
}
}
}
It runs when you enter only 1 topping (ie. pepperoni, but not for pepperoni, mushroom)
Any help would be appreciated!
Thanks!