| | |
Array problem
![]() |
•
•
Join Date: Apr 2007
Posts: 6
Reputation:
Solved Threads: 0
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:
It runs when you enter only 1 topping (ie. pepperoni, but not for pepperoni, mushroom)
Any help would be appreciated!
Thanks!
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)
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!
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.
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++) {} public void orderedToppings() {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);}// 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 orderedsmallPizzaTotal = 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++){} } public void check() {if (nameToFind.equals(toppingsArray)) {} String output = " "; if ((location !=-1) && arr.length>1 && totalAvailableToppings>=arr.length*smallPizza) {location = i;[i]System.out.println(nameToFind + " found at location " +location);break;}JOptionPane.showMessageDialog(null, " Checking " + nameToFind + "topping .....",null,JOptionPane.INFORMATION_MESSAGE);if(numToppingsArray[location]>=smallPizza){} else if((location !=-1) && arr.length==1 && totalAvailableToppings>=smallPizza) {numToppingsArray[location] = numToppingsArray[location]- smallPizza;System.out.println("Enough " + nameToFind + " is avilable for this order");} else {NotEnoughQuantity = true;}JOptionPane.showMessageDialog(null, " Checking " + nameToFind + " topping .....",null,JOptionPane.INFORMATION_MESSAGE);if(numToppingsArray[location]>=smallPizza){} else if(!(totalAvailableToppings >=arr.length*smallPizza)) {numToppingsArray[location] = numToppingsArray[location]- smallPizza;} else {NotEnoughQuantity = true;}NotEnoughQuantity = true;System.out.println(" System crash ");System.out.println("We can't make your pizza.");} if (location != -1) {if (NotEnoughQuantity){} else {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);}output += "\n" + nameToFind + " not found in nameArray[]";JOptionPane.showMessageDialog(null, output,"Original and Sorted Array",JOptionPane.INFORMATION_MESSAGE);}for (int i = 0; i < toppingsArray.length; i++) {} public static void main(String[] args) {System.out.println(toppingsArray + " on hand:\t"[i]+ (numToppingsArray[i] - numToppingsOrderedArray));}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.
•
•
Join Date: Apr 2007
Posts: 6
Reputation:
Solved Threads: 0
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
.
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)
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
![]() |
Similar Threads
- Array problem (C++)
- Array problem (C#)
- Is there a simplest way to work this array problem? (C++)
- class array problem! (C++)
- C++ help with student array problem (C++)
- i have problem with array plz help (Java)
- Large Array Problem (PHP)
Other Threads in the Java Forum
- Previous Thread: Blackjack setting Ace value
- Next Thread: sorting members
| Thread Tools | Search this Thread |
account android api applet application array arrays automation bidirectional binary birt bluetooth class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program project property recursion ria scanner search server set sharepoint smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree unlimited webservices windows





