Results 1 to 7 of 7
  1. #1
    jgonzalez14 is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default setting and getting array

    Hi, I am programming a program to ask the user what kind of toppings they would like on there pizza.
    This is what I would like it to do:
    1. ask the user for how many toppings
    2. print out the list of toppings
    3. from that list choose a number for the corresponding topping
    4. access the the toppingsList array with that number
    5. send that to the Pizza class addToppings method and set it to an array
    6. then be able to print out that array with in my testdrive

    Im really stuck can any one help?

    - this is my super class
    Java Code:
    public abstract class Pizza {
    	
    	protected String[] toppings;
    	protected String[] newToppings;
    	
    	public String[] getToppings(){
    		
    		return toppings;
    	}
    	
    	public void addToppings(String top){
    		
    		newToppings = top;
    		
    		
    		
    		
    		
    	}
    	
    	public int numOfToppings(){
    		
    		int length = toppings.length;
    		
    		return length;
    	}
    	
    	abstract public double getTotalPrice();
    	abstract public double getToppingPrice();
    	
    	
    }
    smallPizza subclass:
    Java Code:
    public class smallPizza extends Pizza {
    
    	private double toppingPrice = .25;
    	private double pizzaPrice = 4.00;
    	
    	
    	public double getToppingPrice() {
    		return toppingPrice;
    	}
    
    	public double getTotalPrice() {
    		return pizzaPrice;
    	}
    
    }
    and the testdrive class
    Java Code:
    import java.io.*;
    public class PizzaTestDrive {
    	
    	private static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
    	public static void main(String[] args) throws IOException {
    		//ARRAY OBJECT OF PIZZA
    		String typePizza     = null;
    		String generalChoose = "\nPlease choose your toppings. All pizzas come with sauce and cheese.\nEach additional topping is  ";
    		String optionElse	 = "There is nothing bigger than an xlarge dumbass!!!! Go to MoMo's if you want a larger one duh!!!";
    		String[] toppingsList={"(1) pepporoni","(2) sauguage","(3) ham ","(4) bacon","(5) mushrooms","(6) onions ","(7) green peppers","(8) hot peppers","(9) green olives","(10) black olives","(11) pineapple","(12) anchovies"};
    		Pizza[] toppings;
    		
    		String chooseAnother = "Please make another selection. Or press 0 to exit";
    		int topSelection 	 = 0;
    		
    		
    		System.out.println("\nWELCOME TO Josh's Pizza Shack!!!!!!\n");
    		System.out.println("What size pizza do you want to have?\n ");
    		System.out.println("(1) Small Pizza");
    		System.out.println("(2) Medium Pizza");
    		System.out.println("(3) Large Pizza");
    		System.out.println("(4) Extra Large Pizza");
    		
    		int selection = Integer.parseInt(stdin.readLine());
    
    		if(selection == 1){
    			smallPizza s  = new smallPizza();
    			System.out.println(generalChoose + s.getToppingPrice());
    			typePizza = "small Pizza";
    		}
    		else if(selection == 2){
    			mediumPizza m = new mediumPizza();
    			System.out.println(generalChoose + m.getToppingPrice());			
    			typePizza = "Medium Pizza";
    		}
    		else if(selection == 3){
    			largePizza l = new largePizza();
    			System.out.println(generalChoose + l.getToppingPrice());
    			typePizza = "Large Pizza";
    		}
    		else if(selection == 4){
    			xlargePizza x = new xlargePizza();
    			System.out.println(generalChoose + x.getToppingPrice());
    			typePizza = "XLarge Pizza";
    		}
    		else{
    			System.out.println(optionElse);
    		}
    		
    		//ASK THE USER FOR THE HOW MANY TOPPINGS THEY WANT
    		System.out.println("How many toppings would you like?");
    		
    		//SET NUMTOPPINGS TO THE TOP COUNT		
    		int numToppings = Integer.parseInt(stdin.readLine());
    		
    		//START CREATING YOUR ARRAY/S
    		
    						
    		//START THE MAIN LOOP
    		for(int i=0;i < numToppings; i++){
    			
    			if(i== 0){
    				for(int j=0;j<toppingsList.length;j++){
    			    	
    					System.out.println(toppingsList[j]);
    			 	}
    			
    			}
    			if(i > 0){System.out.println(chooseAnother);}
    			
    			topSelection = Integer.parseInt(stdin.readLine());
    			
    			//START SMALL PIZZA ARRAY
    			if(selection == 1){
    				
    				toppings[i] = new smallPizza();
    				
    				smallPizza s = (smallPizza) toppings[i];
    				
    				System.out.println(s.toppings[topSelection]);
    				
    				s.addToppings(toppingsList[topSelection]);
    				
    				
    			}
    		}		
    		
    		System.out.print("Thank you your order is \n");
    		System.out.print("One 12 slice " + typePizza + " with");
    		System.out.print("Your total is " );
    	
    	}

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    jgonzalez14 is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    when i pass in the "pizza toppings" to s.addToppings(toppingsList[topSelection]);

    I want addToppings to add each "topping" to the Toppings array in Pizza but this doesn't work for some reason

  4. #4
    jgonzalez14 is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    I have made some changes to the code this might help. I am now getting this error:

    Exception in thread "main" java.lang.NullPointerException
    at Pizza.<init>(Pizza.java:5)
    at smallPizza.<init>(smallPizza.java:2)
    at PizzaTestDrive.main(PizzaTestDrive.java:27)

    I'm not sure what this means? This is my question.

    *testdrive*
    {code} //START THE MAIN LOOP
    for(int i=0;i < numToppings; i++){

    if(i== 0){
    for(int j=0;j<toppingsList.length;j++){

    System.out.println(toppingsList[j]);
    }
    }

    if(i > 0){System.out.println(chooseAnother);}

    topSelection = Integer.parseInt(stdin.readLine());

    //START SMALL PIZZA ARRAY
    if(selection == 1){

    toppings[i] = new smallPizza();

    smallPizza s = (smallPizza) toppings[i];

    String yourToppings = toppingsList[topSelection];

    s.addToppings(yourToppings);




    }
    }

    int i = 0;
    if(toppings[i] instanceof smallPizza){ //ASK IF THE ARRAY POSITION IS OF GIRLRANGER
    smallPizza R = (smallPizza) toppings[i];
    double Total = R.getTotalPrice() + R.getToppingPrice() + (R.getToppingPrice() * R.numOfToppings());

    System.out.print("Thank you your order is \n");
    System.out.print("One 12 slice " + typePizza + " with" + R.getToppings() );
    System.out.print("Your total is " + Total );
    }
    }
    }{/code}

    Pizza Class

    {code}public abstract class Pizza {

    protected String[] toppings;
    protected String[] newToppings = new String[toppings.length + 1];


    public String[] getToppings(){

    toppings = newToppings;

    return toppings;
    }

    public void addToppings(String top){


    newToppings[toppings.length] = top;

    }

    public int numOfToppings(){

    int length = toppings.length;

    return length;
    }

    abstract public double getTotalPrice();
    abstract public double getToppingPrice();


    {/code}

  5. #5
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    In method
    Java Code:
    	public void addToppings(String top){
    		
    		newToppings = top;
    		
    		
    		
    		
    		
    	}
    you cannot assign all the array into string.That's why it doesn't work.you should make some index to insert the top pizza in the empty cell in the array.Think about it .

  6. #6
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    Java Code:
    public void addToppings(String top){
    
    
    newToppings[toppings.length] = top;
    
    }
    Is not good ,cuz you always put the pizza on top ,but at the same you delete the pizza which has been on top before.As i told you make some index to advance into array.

  7. #7
    jgonzalez14 is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    That gives me a type mismatch. Is there a way around that?

Similar Threads

  1. Setting up CVS using Eclipse???
    By JavaInLove in forum Eclipse
    Replies: 1
    Last Post: 07-18-2008, 02:03 PM
  2. Setting the DSN
    By tim in forum JDBC
    Replies: 1
    Last Post: 02-14-2008, 09:55 PM
  3. Help with setting up please
    By BlitzA in forum New To Java
    Replies: 6
    Last Post: 12-29-2007, 12:54 PM
  4. Setting currency
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-16-2007, 02:08 PM
  5. Help with setting number as even or odd
    By fegiflu in forum New To Java
    Replies: 8
    Last Post: 07-24-2007, 06:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •