所以我试图设计一张持有美分和积分的现金卡。因此,当您花2000美分时,您将获得2000积分(1:1比率),这些积分将自动兑换为10美分现金返还。您需要在卡中至少有2000积分才能兑换现金。只有在添加任何现金返还之前有足够的资金支付整个购买时,您才能使用该卡。我遇到的问题是,积分不会从卡的积分余额中扣除,也不会提供现金返还。我想知道我的代码中是否有任何地方让我搞砸了。感谢
public static int POINTS_NEEDED_FOR_CASH_BACK = 2000; // points needed to get cash-back reward
public static int REWARD_POINTS_PER_CENT_CHARGED = 1; // points earned for each cent charged to card
public static int CASH_BACK_REWARD = 10; // reward in cents
private int NUMBER_OF_CASH_BACKS;
private int POINTS_ON_CARD;
private int initialBalance; //must be integer larger or equal to 0
public boolean makePurchase(int amount) {
if (amount < initialBalance) {
initialBalance = (initialBalance - amount);
POINTS_ON_CARD = (POINTS_ON_CARD + (amount * REWARD_POINTS_PER_CENT_CHARGED));
if (POINTS_ON_CARD > POINTS_NEEDED_FOR_CASH_BACK) {
NUMBER_OF_CASH_BACKS = POINTS_ON_CARD / POINTS_NEEDED_FOR_CASH_BACK;
POINTS_ON_CARD -= (NUMBER_OF_CASH_BACKS * POINTS_NEEDED_FOR_CASH_BACK);
initialBalance += (NUMBER_OF_CASH_BACKS * CASH_BACK_REWARD);
}
return true;
}
return false;
}
这是我的测试,我跑了,因为我想找到一种方法来返回新的点值,所以它没有完整。
public void testPurchase3()
{ //当卡上的现金余额足够且有足够的现金返还点时
int oldBalance = testCard.getBalance();
int oldPOINTS_ON_CARD = testCard.getRewardPoints();
testCard.makePurchase(2000);
assertEquals(oldBalance - 2000 , testCard.getBalance());
assertEquals(testCard.getRewardPoints() ,
oldPOINTS_ON_CARD + (2000 * FoodServicesCard.REWARD_POINTS_PER_CENT_CHARGED));
assertTrue(testCard.getRewardPoints() >= FoodServicesCard.POINTS_NEEDED_FOR_CASH_BACK);