Answer to Question #262516 in Java | JSP | JSF for TayyTayyy

Question #262516

Consider a class representing a ClubSandwich. A sandwich is made of some number of bread slices, cheese slices, meat patties and tomato slices. The club sandwich might also contain (or not) mustard, ketchup, iceburg. The sandwich may or may not be grilled. 

Different composition of the sandwich will cost different prices according to what it contains.

     A parameterized constructor that takes argument for each of these variables and initializes them accordingly

    Provide a method calculatePrice() that calculates and returns the price of this sandwich according to the rates given in the above table. E.g

3 * 20 + 2 * 30 + 2 * 5 + 3 * 70 + 5+ 0 + 0 + 10

     Provide a method applyDiscount(double disc) that calculates the price, applies the discount % given in the argument and returns the discounted price of the sandwich.



1
Expert's answer
2021-11-07T17:45:30-0500
public class ClubSandwich {
    private int breadSlices;
    private int cheeseSlices;
    private int meatPatties;
    private int tomatoSlices;
    private boolean mustard;
    private boolean ketchup;
    private boolean iceburg;
    private boolean grilled;

    public ClubSandwich(int breadSlices, int cheeseSlices, int meatPatties, int tomatoSlices,
                        boolean mustard, boolean ketchup, boolean iceburg, boolean grilled) {
        this.breadSlices = breadSlices;
        this.cheeseSlices = cheeseSlices;
        this.meatPatties = meatPatties;
        this.tomatoSlices = tomatoSlices;
        this.mustard = mustard;
        this.ketchup = ketchup;
        this.iceburg = iceburg;
        this.grilled = grilled;
    }

    public double calculatePrice() {
        return breadSlices * 20 + cheeseSlices * 30 + tomatoSlices * 5 + meatPatties * 70
                + (mustard ? 5 : 0) + (ketchup ? 5 : 0) + (iceburg ? 5 : 0) + (grilled ? 10 : 0);
    }

    public double applyDiscount(double disc) {
        return calculatePrice() * (1 - disc / 100);
    }

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS