Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package PizzaCalories;
- import java.util.HashMap;
- import java.util.Map;
- public class Topping {
- private String toppingType;
- private double weight;
- private final Map toppingsModifiers = new HashMap<String, Double>(){{
- put("Meat", 1.2);
- put("Veggies", 0.8);
- put("Cheese", 1.1);
- put("Sauce", 0.9);
- }};
- public Topping(String toppingType, double weight) {
- setToppingType(toppingType);
- setWeight(weight);
- }
- private void setToppingType(String toppingType) {
- if (!toppingsModifiers.containsKey(toppingType)) {
- throw new IllegalArgumentException("Cannot place " + toppingType +" on top of your pizza.");
- }
- this.toppingType = toppingType;
- }
- private void setWeight(double weight) {
- if (weight < 1 && weight > 50) {
- throw new IllegalArgumentException(toppingType + " weight should be in the range [1..50].");
- }
- this.weight = weight;
- }
- public double calculateCalories() {
- return (double)toppingsModifiers.get(this.toppingType) * (2 * this.weight);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement