Java help

Discussion in 'OT Technology' started by letsgoridebmx, Feb 23, 2007.

  1. letsgoridebmx

    letsgoridebmx New Member

    Joined:
    Jan 1, 2007
    Messages:
    14
    Likes Received:
    0
    I can't for the life of me figure out how to use variables in another class. I'm getting input from the user in one class and need to put it into another class. My code is below:

    Code:
    import javax.swing.JOptionPane;
    public class CustomerOrder {
    
        public static void main(String[] args) {
            JOptionPane.showMessageDialog(null, "Welcome to Pizza Palace!", "Pizza Palace", JOptionPane.INFORMATION_MESSAGE);//welcome dialog
            String cheese;
            String pep;
            String veg;
            String drink;
            String order;
            
            int numCheese;//number of cheese pizzas ordered
            int numPep;//number of pepperoni pizzas ordered
            int numVeg;//number of veggie pizzas ordered
            int numDrink;//number of drinks ordered
            int orderMore;//asks the user if they want to order more
            
            
            do {//start of do loop
                
                //brings up order dialog
                Object[] possibilities = {cheese = "Cheese Pizza:  $12.99", pep = "Pepperoni Pizza:  $13.99", veg = "Veggie Pizza:  $13.49",
                                          drink = "Drink:  $0.99"};//all the possibilities
                order = (String)JOptionPane.showInputDialog(null, "What would you like to order today?","Pizza Palace",
                                       JOptionPane.PLAIN_MESSAGE,null, possibilities,cheese);
                
                
                //if the user orders cheese then we ask them how many
                if(order == cheese)
                {//start if
                    numCheese = Integer.parseInt(JOptionPane.showInputDialog("How many cheese pizzas would you like?", "0"));
                }//end if
                
                
                //if the user orders pepperoni then we ask them how many
                if(order == pep)
                {//start if
                    numPep = Integer.parseInt(JOptionPane.showInputDialog("How many pepporoni pizzas would you like?", "0"));
                }//end if
                
                
                //if the user orders veggie then we ask them how many
                if(order == veg)
                {//start if
                    numVeg = Integer.parseInt(JOptionPane.showInputDialog("How many veggie pizzas would you like?", "0"));
                }//end if
                
                
                //if the user orders a drink then we ask them how many
                if(order == drink)
                {//start if
                    numDrink = Integer.parseInt(JOptionPane.showInputDialog("How many drinks would you like?", "0"));
                }//end if
                
                
                //asks if the user wants to order more, if so, it repeats
                orderMore = JOptionPane.showConfirmDialog
                            (null, "Would you like to order more?" , "Pizza Palace",JOptionPane.YES_NO_OPTION);
    
                }//end do loop
            
            //while loop, repeats do loop until the user clicks no
            while(orderMore != JOptionPane.NO_OPTION);
            //end while loop
            
            ProductItem calculateorder = new ProductItem();
          calculateorder.order();
        }
        
            }
    
    
    class ProductItem
    {
            double cheesePrice = 12.99; //constant cheese pizza price
            double pepperoniPrice = 13.99; //constant pepperoni pizza price
            double veggiePrice = 13.49; //constant veggie pizza price
            double drinkPrice = 0.99; //constant drink price
            double taxRate = 0.055; //constant tax rate
            
            double subtotal;
            double tax;
            double total;
            
        public void order()
           {
            if(numCheese <=2 & numPep==0 & numVeg==0)
                {
                    subtotal = (numCheese * cheesePrice) + (numDrink * drinkPrice);
                    tax = (subtotal * taxRate);
                    total = subtotal + tax;                       
                          JOptionPane.showMessageDialog(null, "Your total (without the discount) is:  $" + Math.rint(total*100)/100,
                       "Pizza Cost",
                       JOptionPane.INFORMATION_MESSAGE);
                          
                }    
    
            
                   }
           }      
    
    I can't figure out how to get numCheese, numPep, numVeg, and numDrink to be recognized. I'm sure there's a pretty simple way to do this, any help would be appreciated. Just trying to learn :wavey:

    Thanks!
     
  2. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    add accessor methods to it. for numCheese you would add
    Code:
    public int getNumCheese()
    {
             return numCheese;
    }
    

    do this for each variable and when you want to find them call the method for that variable
     
  3. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Right, you shouldn't access the variables directly. Instead you define an interface that lets you get and set them, as the previous poster has so graciously explained.
     
  4. letsgoridebmx

    letsgoridebmx New Member

    Joined:
    Jan 1, 2007
    Messages:
    14
    Likes Received:
    0
    So I should just replace in CustomerOrder
    Code:
    int numCheese;//number of cheese pizzas ordered
    with
    Code:
    public int getNumCheese()
    {
             return numCheese;
    }
    Is that right? It's giving me this error:
    Code:
    Multiple markers at this line
        - Syntax error on token "int", @ expected
        - getNumCheese cannot be resolved to a type
        - The member enum $missing$ cannot be local
        - Syntax error, insert "enum Identifier" to complete EnumHeaderName
        - Syntax error, insert "EnumBody" to complete BlockStatement
    Or do I have to set it as well? I don't know why I'm having so much trouble :hs:

    Thanks again for the help
     
  5. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    no you still need the actual member variable in that class but you also need a way to access it from other classes. so for the other class youll need a reference variable like CustomerOrder order1 = new CustomerOrder(); Then when you want to get how many cheese pizzas are in that order youd do order1.getNumCheese() and itll return that value.
     
  6. Corp

    Corp OT Supporter

    Joined:
    Sep 11, 2003
    Messages:
    28,201
    Likes Received:
    0
    Location:
    Providence, RI
    are you taking a class right now? if so i think it would be best for you to get some help directly from your professor
     

Share This Page