简体   繁体   English

如何检查已调用哪个 java 实例方法以对属性应用适当的折扣?

[英]How do I check which java instance method has been called to apply appropriate discount to the property?

So, basically I have a Funpark class which has 3 activities.所以,基本上我有一个 Funpark class 有 3 个活动。 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.如果用户注册了 2 个活动,那么我想对 totalPrice 应用 10% 的折扣,如果他们适用于所有 3 个活动,我想对 totalPrice 提供 20% 的折扣。

Here is my code for FunPark class:这是我的 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我得到的 output 是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但是,我的预期结果(在这种情况下)是37.6 (因为当然所有三个活动的总价格 - 20% 折扣是29.6

Here is my calling code in the main class btw:这是我在主要 class 中的调用代码:

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.谢谢。

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.trampoline之后,您将 12 添加到最初为 0 的总价格中,并且不应用任何折扣。 After climbWall , you add 12 (=22) and apply 10% discount, and get 19.8.climbWall之后,您添加 12 (=22) 并应用 10% 的折扣,得到 19.8。 Then after waterSlide , you add another 15 (=34.8) and apply a 20% discount, and the result is incorrectly 27.84.然后在waterSlide之后,再添加 15 (=34.8) 并应用 20% 的折扣,结果错误地为 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查是否已调用某种方法? - How to check if a certain method has been called? 如何添加 priceAfterDiscount 的公共实例方法,该方法返回折扣后的价格 - How do I add a public instance method of priceAfterDiscount which returns the price after discount 创建实例时如何在两个子类之间检查哪个已被调用? - How to check between two subclasses which one has been called when creating an instance? 是否有警卫来检查Java是否调用了一个方法? - Are guards to check if a method has been called optimized by Java? Java:如何检查数组中的元素是否已初始化? - Java: How do I check if an element in an array has been initialized? 在 Java 中,如何检查 AutoCloseable.close() 是否已被调用? - In Java, how to check that AutoCloseable.close() has been called? 如何检查是否已在Java中调用数组索引 - How to check to see if an array index has already been called in java 如何检查是否已在Java中创建/调用过对象? (Eclipse IDE) - How can I check if an object has been created/called upon in Java? (Eclipse IDE) 如何测试是否已调用java.lang.Math方法? - How can I test if a java.lang.Math method has been called? 如何检查已选择运行哪个JNLP文件? - How do I check which JNLP file that has been selected to run?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM