Skip to content

Commit

Permalink
Revert "Add1 (#140)"
Browse files Browse the repository at this point in the history
This reverts commit c1d573f.
  • Loading branch information
hcsp-bot committed Mar 23, 2020
1 parent c1d573f commit db0e2ef
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
package com.github.hcsp.polymorphism;

public class Discount95Strategy extends DiscountStrategy{
@Override
public int discount(int price, User user) {
return (int)(price * 0.95);
}
}
public class Discount95Strategy {}
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
package com.github.hcsp.polymorphism;

public class NoDiscountStrategy extends DiscountStrategy{
@Override
public int discount(int price, User user) {
return price;
}
}
public class NoDiscountStrategy {}
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
package com.github.hcsp.polymorphism;

public class OnlyVipDiscountStrategy extends DiscountStrategy{
@Override
public int discount(int price, User user) {
return user.isVip() ? (int) (price * 0.95) : price;
}
}
public class OnlyVipDiscountStrategy {}
22 changes: 18 additions & 4 deletions src/main/java/com/github/hcsp/polymorphism/PriceCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,22 @@ public class PriceCalculator {
// OnlyVipDiscountStrategy 只有VIP打95折,其他人保持原价
// 重构后的方法签名:
// public static int calculatePrice(DiscountStrategy strategy, int price, User user)
public static int calculatePrice(DiscountStrategy strategy, int price, User user) {
return strategy.discount(price, user);
public static int calculatePrice(String discountStrategy, int price, User user) {
switch (discountStrategy) {
case "NoDiscount":
return price;
case "Discount95":
return (int) (price * 0.95);
case "OnlyVip":
{
if (user.isVip()) {
return (int) (price * 0.95);
} else {
return price;
}
}
default:
throw new IllegalStateException("Should not be here!");
}
}

}
}

0 comments on commit db0e2ef

Please sign in to comment.