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.
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);
}
}
Comments
Leave a comment