Hi guys I'm trying to do this java exercise and I'm getting nowhere with this So I was wondering if anyone can help me out with this.

1. Create a PizzaOrder.java class with the following attributes and methods:
PizzaOrder

-size: String
-toBeDelivered: boolean
-count: int

+PizzaOrder()
+PizzaOrder(size: String, toBeDelivered: boolean)
+getSize(): String
+isToBeDelivered(): boolean
+getCount: int
+setSize(size: String): void
+setToBeDelivered(toBeDelivered: boolean): void
+calculatePrice(): double
+toString(): String

For size:
“small” – small
“medium” – medium
“large” – large
“extra” – extra large

The price should be calculated as the following:
Base price:
Small pizza: 6.99
Medium pizza: 8.99
Large pizza: 10.99
Extra large pizza: 12.99
If user wants the pizza to be delivered, add additional $3.00 as the tip.


2. Under the same project, create a PizzaWithToppings Class, which is a subclass of PizzaOrder class.
PizzaOrder
Δ

PizzaWithToppings
-sausage: boolean
-pepperoni: boolean
-peppers: boolean

+PizzaWithToppings()
+PizzaWithToppings(sausage: boolean, pepperoni: boolean, peppers: boolean, size: String, toBeDelivered: boolean)
+isSausage(): boolean
+isPepperoni (): boolean
+isPeppers (): boolean
+setSausage(sausage: boolean): void
+setPepperoni (pepperoni: boolean): void
+setPeppers (pepprers: boolean): void
+calculatePrice(): double
+toString(): String

So far this is what I have and I'm stuck on the calculating price of the pizza and how to include the delivery fee.

public class PizzaOrder {
    private String size;
    private boolean toBeDelivered;
    private static int count = 0;
    
    
    /** Creates a new instance of PizzaOrder */
    public PizzaOrder() {
        toBeDelivered = false;
    }
    public PizzaOrder (String size, boolean toBeDelivered)
    {
        this.size = size;
        count = count + 1;
    }
    //get methods
    public String getSize()
    {
        return size;
    }
    public void setSize (String size)
    {
        this.size = size;
    }
    
    public boolean toBeDelivered ()
    {
        return toBeDelivered;
    }
    public void setYes (boolean yesNo)
    {
        toBeDelivered = yesNo;
    }

I changed the code a little bit following the diagram.

public class PizzaOrder {
    private String size;
    private boolean toBeDelivered;
    private static int count = 0;
    
    
    /** Creates a new instance of PizzaOrder */
    public PizzaOrder() {
     
    }
    public PizzaOrder (String size, boolean toBeDelivered)
    {
        this.toBeDelivered = toBeDelivered;
        this.size = size;
        count = count + 1;
    }
    //get methods
    public String getSize()
    {
        return size;
    }
    public void setSize (String size)
    {
        this.size = size;
    }
    
    public boolean toBeDelivered ()
    {
        return toBeDelivered;
    }
    public void setToBeDelivered()
    {
        this.toBeDelivered = toBeDelivered;
    }
    public static int getCount()
    {
        return count;
    }

Here is my main method codes

public static void main(String[] args) {
        // TODO code application logic here
        
        System.out.println("myOrder info:");
        PizzaOrder myOrder = new PizzaOrder("large", true);
        System.out.println(myOrder);
        System.out.println("yourOrder info");
        PizzaOrder yourOrder = new PizzaOrder("medium", false);
        System.out.println(yourOrder);
        System.out.println("Total order is now: " + PizzaOrder.getCount());
}

and the pizzaorder class codes

public class PizzaOrder {
    private String size;
    private boolean toBeDelivered;
    private static int count = 0;
    
    
    /** Creates a new instance of PizzaOrder */
    public PizzaOrder() {
 
     
    }
    public PizzaOrder (String size, boolean toBeDelivered)
    {
        this.toBeDelivered=toBeDelivered;
        this.size = size;
        count = count + 1;
    }
    //get methods
    public String getSize()
    {
        return size;
    }
       public boolean getToBeDelivered ()
    {
        return toBeDelivered;
    }
        public void setToBeDelivered(boolean toBeDelivered)
        {
            this.toBeDelivered = toBeDelivered;
    }
    public void setSize (String size)
    {
        this.size = size;
    }
    public static int getCount()
    {
        return count;
    }
    
    public double getPrice()
    {
        double price = 0;
        if ((getSize() == "small") && (getToBeDelivered() == false))
        {
            price = 6.99;
        }
        else if ((getSize() == "small") && (getToBeDelivered()==true))
        {
            price = 9.99;
        }
        else if ((getSize() == "medium") && (getToBeDelivered() == false))
        {
            price = 8.99;
        }
        else if ((getSize() == "medium") && (getToBeDelivered()==true))
        {
            price = 11.99;
        }
         else if ((getSize() == "large") && (getToBeDelivered() == false))
        {
            price = 10.99;
        }
        else if ((getSize() == "large") && (getToBeDelivered()==true))
        {
            price = 13.99;
        }
         else if ((getSize() == "extra") && (getToBeDelivered() == false))
        {
            price = 12.99;
        }
        else if ((getSize() == "extra") && (getToBeDelivered()==true))
        {
            price = 15.99;
        }
        return price;
       
    }

    public String toString()
    {
        String report = "#####################" + "\n" 
                +"Size: "  +size + "\n" 
                + "To Be Deliver: "  + toBeDelivered + "\n" 
                + "Price: $" + getPrice() + "\n"
                + "#####################" + "\n";
        
        return report;
    }

}

Now I just need the last one. the super class I think that's what you call it.

Thats a lot better mate, similar to what I've done for you, heres the first class. Tis pretty self explanatory:

public class PizzaOrder
{
    protected String size; //Size of pizza
    protected boolean toBeDelivered; //Is the pizza being delivered
    protected int count; //Dunno what this is for tbh
    protected double cost; //Cost of pizza
    
    /**
     * A default no-args constructor with all values set to null or false.
     */
    
    PizzaOrder( ) //Default no-args constructor
    {
        size = ""; //No Size
        toBeDelivered = false; //No Delivery
        count = 0; //Again, no idea
        cost = 0;
    }
    
    /**
     * An argumented constructor.
     * @param _size The size of the pizza.
     * @param _toBeDelivered Whether or not the pizza is to be delivered.
     * @throws IllegalArgumentException If the size of the pizza is invalid.
     */
    
    PizzaOrder( String _size, boolean _toBeDelivered ) throws IllegalArgumentException //Argumented Constructor
    {
        if ( _size.equals( "small" ) //If _size is valid
             || _size.equals( "medium") 
             || _size.equals( "large" ) 
             || _size.equals( "extra" ) )
        {
            size = _size; //Set size
            toBeDelivered = _toBeDelivered; //Set delivery
            count = 1; //Again, no idea
            cost = 0;
        }
        else
            throw new IllegalArgumentException( "Size not valid."); //If size isn't valid throw an exception
    }
    
    /**
     * A method to set whether or not the pizza is to be delivered separate to the constructor.
     * @param _toBeDelivered Whether or not the pizza is to be delivered.
     */
    
    public void setToBeDelivered( boolean _toBeDelivered )
    {
        toBeDelivered = _toBeDelivered;
    }
    
    /**
     * A method to check whether or not the pizza is to be delivered.
     * @return True if the pizza is being delivered.
     */
    
    public boolean isToBeDelivered( )
    {
        return toBeDelivered;
    }
    
    /**
     * A method to set the size of the pizza separate to the constructor.
     * @param _size The size of the pizza.
     * @throws IllegalArgumentException If the size of the pizza is invalid.
     */
    
    public void setSize( String _size ) throws IllegalArgumentException
    {
        if ( _size.equals( "small" ) //If size is valid
             || _size.equals( "medium") 
             || _size.equals( "large" ) 
             || _size.equals( "extra" ) )
            size = _size; //Set size
        else
            throw new IllegalArgumentException( "Size not valid" ); //If not throw exception
    }
    
    /**
     * A method to view the size of the pizza.
     * @return The size of the pizza.
     */
    
    public String getSize( )
    {
        return size;
    }
    
    /**
     * A method to view the count.
     * @return The count.
     */
    
    public int getCount( )
    {
        return count;
    }
    
    /**
     * A method to calculate the cost of the pizza, depending on the size of the pizza and if it is being delivered.
     * @return The cost of the pizza.
     */
    
    public double calculateCost( )
    {
        if ( toBeDelivered ) //If it is being delivered
            cost = 3.99; //Apply delivery charge
        if ( size.equals( "small" ) ) //If size is valid
            cost += 6.99; //Apply Cost for size of pizza
        else if ( size.equals( "medium" ) )
            cost += 8.99;
        else if ( size.equals( "large" ) )
            cost += 10.99;
        else
            cost += 12.99;
        return cost;
    }
    
    /**
     * A method to view a String representation of the pizza.
     * @return The String representation of the pizza.
     */
    
    public String toString( )
    {
        return ( "Size: " + size
               + "\nDelivery: " + toBeDelivered 
               + "Cost: $" + calculateCost( ) );
    }
}

And here's yer PizzaWithToppings class. Pretty similar!

class PizzaWithToppings extends PizzaOrder
{
    protected boolean sausage, pepperoni, peppers; //The three toppings
    
    /**
     * Default no-args constructor. All values set to null or false.
     */
    
    public PizzaWithToppings( ) //Default no-args constructor
    {
        super( ); //Default PizzaOrder constructor
        sausage = false; //All toppings set to false
        pepperoni = false;
        peppers = false;
    }
    
    /**
     * Argumented constructor. Initializes variables to supplied values.
     * @param _size The size of the pizza.
     * @param _toBeDelivered The delivery status of the pizza.
     * @param withSausage If it comes with sausage.
     * @param withPepperoni If it comes with pepperoni.
     * @param withPeppers If it comes with peppers.
     * @throws IllegalArgumentException If the size of the pizza is invalid.
     */
    
    public PizzaWithToppings( String _size, boolean _toBeDelivered, boolean withSausage, boolean withPepperoni, boolean withPeppers ) throws IllegalArgumentException //Argumented constructor
    {
        super( _size, _toBeDelivered ); //Argumented PizzaOrder constructor
        sausage = withSausage; //Toppings set to supplied values
        pepperoni = withPepperoni;
        peppers = withPeppers;
    }
    
    /**
     * A method to calculate the cost of the pizza based on the supplied values.
     * @return The cost of the pizza.
     */
    
    public double calculateCost( )
    {
        return super.calculateCost(); //PizzaOrder calculate cost as no variables from this call influence cost.
    }
    
    /**
     * A method to set the sausage status outside of the constructor.
     * @param _sausage Whether or not there is sausage on the pizza/
     */
    
    public void setSausage( boolean _sausage )
    {
        sausage = _sausage;
    }
    
    /**
     * A method to check whether or not there is sausage on the pizza.
     * @return True If there is sausage on the pizza.
     */
    
    public boolean isSausage( )
    {
        return sausage;
    }
    
    /** 
     * A method to set the status of pepperoni on the pizza.
     * @param _pepperoni Whether or not pepperoni is wanted on the pizza.
     */
    
    public void setPepperoni( boolean _pepperoni )
    {
        pepperoni = _pepperoni;
    }
    
    /**
     * A method to check whether or not there is pepperoni on the pizza.
     * @return True If there is pepperoni on the pizza.
     */
    
    public boolean isPepperoni( )
    {
        return pepperoni;
    }
    
    /**
     * A method to set the status of peppers on the pizza.
     * @param _peppers Whether or not there are peppers on the pizza
     */
    
    public void setPeppers( boolean _peppers )
    {
        peppers = _peppers;
    }
    
    /**
     * A method to check whether or not there are peppers on the pizza.
     * @return True if there are peppers on the pizza.
     */
    
    public boolean isPeppers( )
    {
        return peppers;
    }
    
    /**
     * A method to view a String representation of this pizza.
     * @return The string representation of this pizza.
     */
    
    public String toString( )
    {
        return ( super.toString( ) 
               + "\nSausage : " + sausage
               + "\nPepperoni: " + pepperoni
               + "\nPeppers: " + peppers );
    }
}
This article has been dead for over six months. Start a new discussion instead.