Home > OS >  How do I check which java instance method has been called to apply appropriate discount to the prope
How do I check which java instance method has been called to apply appropriate discount to the prope

Time:07-30

So, basically I have a Funpark class which has 3 activities. If user enrolls in 2 activities, then I want to apply 10% discount on totalPrice and if they apply to all 3 activities, I want to give 20% discount on totalPrice.

Here is my code for FunPark class:

public class FunPark {
    private final int climbWallPrice = 10;
    private final int trampoliningPrice = 12;
    private final int waterSlidesPrice = 15;

    public float getTotalPrice() {
        return totalPrice;
    }

    private float totalPrice = 0;

    private float applyDiscountTo(float applyTo, int discountInPercent) {

        float discount = (float) discountInPercent / 100;
        float discountValue = applyTo * discount;

        float discountedPrice = applyTo - discountValue;

        return discountedPrice;
    }

    public void climbWall() {
        if(totalPrice == trampoliningPrice || totalPrice == waterSlidesPrice) {
            totalPrice  = climbWallPrice;
            totalPrice = applyDiscountTo(totalPrice, 10);
        }

        else if(totalPrice == trampoliningPrice   waterSlidesPrice) {
            totalPrice  = climbWallPrice;
            totalPrice = applyDiscountTo(totalPrice, 20);
        }

        else {
            totalPrice = climbWallPrice;
        }
    }

    public void trampoline() {
        if(totalPrice == climbWallPrice || totalPrice == waterSlidesPrice) {
            totalPrice  = trampoliningPrice;
            totalPrice = applyDiscountTo(totalPrice, 10);
        }

        else if(totalPrice == climbWallPrice   waterSlidesPrice) {
            totalPrice  = trampoliningPrice;
            totalPrice = applyDiscountTo(totalPrice, 20);
        }

        else {
            totalPrice = trampoliningPrice;
        }
    }



    public void waterSlide() {
        if(totalPrice == climbWallPrice || totalPrice == trampoliningPrice) {
            totalPrice  = waterSlidesPrice;
            totalPrice = applyDiscountTo(totalPrice, 10);
        }

        else if(totalPrice == climbWallPrice   trampoliningPrice) {
            totalPrice  = waterSlidesPrice;
            totalPrice = applyDiscountTo(totalPrice, 20);
        }

        else {
            totalPrice = waterSlidesPrice;
        }
    }

}

The output I get is 15.0

But, my expected result (in this case) is 37.6 (because of course the sum price for all three activities - 20% discount is 29.6

Here is my calling code in the main class btw:

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");

        FunPark park = new FunPark();

        park.trampoline();
        park.climbWall();
        park.waterSlide();

        System.out.println(park.getTotalPrice());
    }
}

So, could anyone please guide me in a direction on how should I go on about achieving the result? Thanks.

CodePudding user response:

This will not work if you apply the discounts in each of the activity's methods. For example, consider the sequence:

park.trampoline();
park.climbWall();
park.waterSlide();

After trampoline, you add 12 to the total price which is initially 0 and apply no discounts. After climbWall, you add 12 (=22) and apply 10% discount, and get 19.8. Then after waterSlide, you add another 15 (=34.8) and apply a 20% discount, and the result is incorrectly 27.84.

You could potentially do it this way by "undoing" the discounts with more math, but I find that more counter-intuitive.

Better to just keep track of the activities. You can do this easily with an enum.

enum Activity {

    CLIMB_WALL(10),
    TRAMPOLINE(12), 
    WATER_SLIDE(15);
    private final int price;
    
    Activity(int price) {
        this.price = price;
    }

    public int getPrice() {
        return price;
    }
}

Then you can use a list to record all the activities:

// in FunPark
private List<Activity> activities = new ArrayList<>();

public void climbWall() {
    activities.add(Activity.CLIMB_WALL);
}

public void trampoline() {
    activities.add(Activity.TRAMPOLINE);
}

public void waterSlide() {
    activities.add(Activity.WATER_SLIDE);
}

The price can be calculated by iterating through the list:

public float getTotalPrice() {
    float total = 0;
    var uniqueActivities = new HashSet<Activity>();
    for (var activity : activities) {
        uniqueActivities.add(activity);
        total  = activity.getPrice();
    }
    if (uniqueActivities.size() == 2) {
        total = applyDiscountTo(total, 10);
    } else if (uniqueActivities.size() == 3) {
        total = applyDiscountTo(total, 20);
    }
    return total;
}
  • Related