[英]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.