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!

Recommended Answers

All 3 Replies

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.

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

[B]public[/B] [B]void[/B] onHand() {[INDENT][B]for[/B] ([B]int[/B] i = 0; i < toppingsArray.length; i++) {[INDENT]numToppingsArray[i] = Integer.[I]parseInt[/I](JOptionPane.[I]showInputDialog[/I]([/INDENT][INDENT]"Please enter number of " + toppingsArray[/INDENT][INDENT][I]+ " toppings on hand:", "0"));[/I][/INDENT][INDENT][I]totalAvailableToppings = totalAvailableToppings[/I][/INDENT][INDENT][i]+ numToppingsArray[I];[/I][/INDENT][INDENT][i]System.[I]out[/I].println(toppingsArray[I] + " on hand:\t"[/I][/INDENT][INDENT][i]+ numToppingsArray[I]);[/I][/INDENT][I]}[/I]
 
 

[/INDENT][I]}[/I]
 

[I][B]public[/B] [B]void[/B] orderedToppings() {[/I][INDENT][I]// brings up pizza input dialog[/I][/INDENT]

[INDENT][i]smallPizza = Integer.[I]parseInt[/I](JOptionPane.[I]showInputDialog[/I]([/INDENT]

[INDENT]"How many small pizzas would you like to order?", "0"));[/INDENT]

[INDENT]pizzaCount += smallPizza;[/INDENT]
 

[INDENT]// computes total number of small pizzas ordered[/INDENT]

[INDENT]smallPizzaTotal = smallPizza + smallPizzaTotal;[/INDENT]
 

[INDENT]String smallToppings = JOptionPane[/INDENT]

[INDENT].[I]showInputDialog[/I]("What kind of toppings do you want on your small pizza?\nWe currently have pepperoni and mushroom.\n"[/INDENT]

[INDENT]+ "Please enter your toppings with spaces in between (ex: pepperoni mushroom)\nFor cheese only, please click OK.");[/INDENT]
 

[INDENT]String[] arr = smallToppings.split(" ");[/INDENT]
[B]for[/B]([B]int[/B] j =0;j<arr.length;j++)
 
 

{[INDENT]String nameToFind = arr[j];[/INDENT][INDENT][B]int[/B] location = -1;[/INDENT][INDENT][B]for[/B]([B]int[/B] i=0;i<toppingsArray.length;i++)[/INDENT][INDENT]{[INDENT][B]if[/B] (nameToFind.equals(toppingsArray[I])) {[/I][INDENT][I]location = i;[/I][/INDENT][INDENT][i]System.[I]out[/I].println(nameToFind + " found at location " +location);[/INDENT][INDENT][B]break[/B];[/INDENT]}
 
 

[/INDENT]}
String output = " ";
 
[B]if[/B] ((location !=-1) && arr.length>1 && totalAvailableToppings>=arr.length*smallPizza)
 
 

{[INDENT]JOptionPane.[I]showMessageDialog[/I]([B]null[/B], " Checking " + nameToFind + "topping .....",[B]null[/B][/INDENT][INDENT],JOptionPane.[I]INFORMATION_MESSAGE[/I]);[/INDENT][INDENT][B]if[/B](numToppingsArray[location]>=smallPizza)[/INDENT]

[INDENT]{[INDENT]numToppingsArray[location] = numToppingsArray[location]- smallPizza;[/INDENT][INDENT]System.[I]out[/I].println("Enough " + nameToFind + " is avilable for this order");[/INDENT]}
[B]else[/B]
 

{[INDENT]NotEnoughQuantity = [B]true[/B];[/INDENT]}
 

[/INDENT]}
[B]else[/B] [B]if[/B]((location !=-1) && arr.length==1 && totalAvailableToppings>=smallPizza)
 

{[INDENT]JOptionPane.[I]showMessageDialog[/I]([B]null[/B], " Checking " + nameToFind + " topping .....",[B]null[/B],[/INDENT][INDENT]JOptionPane.[I]INFORMATION_MESSAGE[/I]);[/INDENT][INDENT][B]if[/B](numToppingsArray[location]>=smallPizza)[/INDENT][INDENT]{[INDENT]numToppingsArray[location] = numToppingsArray[location]- smallPizza;[/INDENT]}
[B]else[/B]
 

{[INDENT]NotEnoughQuantity = [B]true[/B];[/INDENT]}
 

[/INDENT]}
[B]else[/B] [B]if[/B](!(totalAvailableToppings >=arr.length*smallPizza))
 

{[INDENT]NotEnoughQuantity = [B]true[/B];[/INDENT][INDENT]System.[I]out[/I].println(" System crash ");[/INDENT][INDENT]System.[I]out[/I].println("We can't make your pizza.");[/INDENT]}
 

[B]if[/B] (location != -1) {[INDENT][B]if[/B] (NotEnoughQuantity) [/INDENT][INDENT]{[INDENT]JOptionPane.[I]showMessageDialog[/I]([B]null[/B],[/INDENT][INDENT]"We are out of "+ nameToFind +" topping, sorry.");[/INDENT]} 
[B]else[/B] 
 

{[INDENT]output += "\n Enough " + nameToFind + " topping available for this order";[/INDENT][INDENT]JOptionPane.[I]showMessageDialog[/I]([B]null[/B], output,[/INDENT][INDENT]"Original and Sorted Array",[/INDENT][INDENT]JOptionPane.[I]INFORMATION_MESSAGE[/I]);[/INDENT]}
 
 

[/INDENT]} [B]else[/B] {[INDENT]output += "\n" + nameToFind + " not found in nameArray[]";[/INDENT][INDENT]JOptionPane.[I]showMessageDialog[/I]([B]null[/B], output,[/INDENT][INDENT]"Original and Sorted Array",[/INDENT][INDENT]JOptionPane.[I]INFORMATION_MESSAGE[/I]);[/INDENT]}
 
 

[/INDENT]}
}
 

[B]public[/B] [B]void[/B] check() {[INDENT][B]for[/B] ([B]int[/B] i = 0; i < toppingsArray.length; i++) {[/INDENT]

[INDENT][INDENT]System.[I]out[/I].println(toppingsArray[I] + " on hand:\t"[/I][/INDENT]

[INDENT][i]+ (numToppingsArray[i] - numToppingsOrderedArray[I]));[/I][/INDENT]
[I]}[/I]

[/INDENT][I]}[/I]
 
[I][B]public[/B] [B]static[/B] [B]void[/B] main(String[] args) {[/I]

[INDENT][I]test pizza = [B]new[/B] test();[/I][/INDENT][INDENT][I]pizza.onHand();[/I][/INDENT][INDENT][I]pizza.orderedToppings();[/I][/INDENT]

[INDENT][I][B]int[/B] checkInv = 0;[/I][/INDENT][INDENT][i]checkInv = JOptionPane.[I]showConfirmDialog[/I]([B]null[/B],[/INDENT][INDENT]"Would you like to check the inventory?", "Pizza Palace",[/INDENT][INDENT]JOptionPane.[I]YES_NO_OPTION[/I]);[/INDENT][INDENT][B]if[/B] (checkInv == JOptionPane.[I]YES_OPTION[/I]) {[INDENT]pizza.check();[/INDENT]

} [B]else[/B] {[INDENT]System.[I]out[/I].print("Thanks!");[/INDENT]}
 

[/INDENT]}
 
}

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!

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 :).

import javax.swing.JOptionPane;

   public class testingCode
   {
       
      int pepToppings                 = 0;
      int mushToppings                 = 0;
      int meatballToppings            = 0;
      int totalAvailableToppings     = 0;
       
      int smallPizza                 = 0;
      int pizzaCount                 = 0;
      int smallPizzaTotal             = 0;
            
      String[] toppingsArray         = { "pepperoni", "mushroom", "meatball" };         
      int[] numToppingsArray         = { pepToppings, mushToppings, meatballToppings };         
      int[] numToppingsOrderedArray = { pepToppings, mushToppings, meatballToppings };
      
      
      
      //sets number of toppings on hand
      public void onHand()
       {//start method onHand
          
          for (int i = 0; i < toppingsArray.length; i++) 
              {//start for

                numToppingsArray[i] = Integer.parseInt
                (JOptionPane.showInputDialog("Please enter number of " + toppingsArray[i] + " toppings on hand:", "10"));

                totalAvailableToppings = totalAvailableToppings + numToppingsArray[i];

                System.out.println(toppingsArray[i] + " on hand:\t" + numToppingsArray[i]);

            }//end for
       }//end method onHand
       
       
       
       public void order()
       {//start method order
           
           //asks customer how many small pizzas they would like to order
            smallPizza = Integer.parseInt
            (JOptionPane.showInputDialog("How many small pizzas would you like to order?", "5"));

            pizzaCount += smallPizza;

            smallPizzaTotal = smallPizza + smallPizzaTotal;
                        
            //asks the customer what kind of toppings they want
            String smallToppings = 
            JOptionPane.showInputDialog("What kind of toppings do you want on your small pizza?", "pepperoni mushroom");
            
            
            //splits each toppings into an array
            String[] arr = smallToppings.split(" ");
              
            int smallCount = 0;
            
            //counts how many toppings there are (just by words)
              for(int k = 0; k <arr.length;k++)
            {//start for
                  smallCount = 0;
                if(arr[k].equals("")) 
                {//start if
                    smallCount = 0;
                }//end if
                
                else
                {//start else
                    smallCount++;
                }//end else            
                
                //searches for topping in toppingsArray
                for(int j = 0; j <arr.length;j++)
                {//start for
                    //sets arr[j] to nameToFind (String to search for)
                    String nameToFind = arr[j];
                    
                    //sets number of toppings ordered to an array
                    numToppingsOrderedArray[k] = (smallCount * smallPizza);
                    
                    
                    //if found, check to see if there are enough, otherwise we can't make it
                    for(int i = 0; i <toppingsArray.length;i++)
                    {//start for
                        if (nameToFind.equals(toppingsArray[i]))
                        {//start if    
                            
                            //total toppings ordered (ie.  5 small pepperoni pizzas, charge them for 5 pepperoni toppings)
                            System.out.println("\nTotal " + toppingsArray[i] + " ordered:  " + numToppingsOrderedArray[k]);
                            
                            //doesn't do anything, just text
                            System.out.println("Checking to see if there are enough " + toppingsArray[i] + " left...");

                            //checks to see if there are enough toppings available
                            if (numToppingsArray[i] > numToppingsOrderedArray[k])
                            {//start if
                                System.out.println("There are enough " + toppingsArray[i] + " to make your pizza.");
                                
                                //removes number of toppings ordered from those on hand
                                numToppingsArray[i] = numToppingsArray[i] - numToppingsOrderedArray[k];
                                System.out.println(toppingsArray[i] + " on hand:\t" + (numToppingsArray[i]) + "\n");
                            }//end if
                            else
                            {//start else
                                System.out.println("We can't make your pizza.\n");
                            }//end else
                                
                        }//end if
                    }//end for
            
                }//end for
                
            }//end for
          
       }//end method order
       
       
       public void check()
           {//start method check
           
               for( int i = 0; i < toppingsArray.length; i++ )
               {//start for
                   System.out.println(toppingsArray[i] + " on hand:\t" + numToppingsArray[i]);
               }//end for
           }//end method check
       
    
       public static void main( String[] args )
       {//start main
           testingCode pizza = new testingCode();
           pizza.onHand();
           pizza.order();
          
          
           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)
           {//start if
               pizza.check();
           }//end if
           else
           {//start else
               System.out.print("Thanks!");
           }//end else
          
      }//end main

   }//end class testingcode
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.