【发布时间】:2018-12-14 07:54:25
【问题描述】:
我正在尝试创建一个优惠券计数器来检查可用于减少发票的可组合优惠券的可用性。
如果可用优惠券等于所需优惠券,则不需要发票。
例子:
XYZ 公司需要为他们的发票支付 100 美元(金额始终可除以 25,余数为 0),但他们可以使用优惠券支付。
一张优惠券价值 25 美元,所以我需要检查公司是否在动态选择的月份数内节省了 4 美元。
我的优惠券表如下所示:
Total_Coupon_Count
- 身份证
- 公司ID
- 月
- Total_Coupon_Count
- 年份
示例续:
XYZ 公司在 3 月有 0 张优惠券,在 4 月有 1 张,在 5 月有 0 张,在 6 月有 2 张,7 月 1 日。
XYZ公司最多可以保存3个月前的优惠券(所以5月、6月、7月的优惠券)
这个例子的结果需要是:
已使用3张优惠券(6月2张,7月1张),客户还需支付25美元。
我的程序需要做的是:
获取公司可以保存优惠券的月数(在本例中为 3)
减去优惠券,直到它们为 0
- 获取发票的其余部分并将其显示给用户
这是我现在拥有的代码:
//While the needed amount of coupons for the invoice is smaller than the total available coupons
while(neededCoupons < cv.combinableAmount)
{
//We start at the earliest month we can go back to (this month - the combinable months)
for (int i = int.Parse(DateTime.Now.AddMonths(DateTime.Now.Month - cv.monthsCombinable).ToString()); i >= int.Parse(DateTime.Now.Month.ToString()) ; i++ )
{
int amountForMonth = GetCouponAmountByMonth(companyID, i);
if (amountForMonth >= neededCoupons)
{
//The amount is enough, we can stop
Result += "Coupons used from " + new DateTime(DateTime.Now.Year, i, 1).ToString("MMMM") + " amount: " + amountForMonth;
//To save the data, we subtract the neededCoupons from that month
SubtractCoupons(companyID, i, amountForMonth);
}
else
{
//The amount for that month is not enough (or zero), we need to subtract it, get the remaining and continue to the following month)
if (amountForMonth != 0)
{
SubtractCoupons(companyID, i, amountForMonth);
Result += "Coupons used from " + new DateTime(DateTime.Now.Year, i, 1).ToString("MMMM") + " amount: " + amountForMonth;
neededCoupons = neededCoupons - amountForMonth;
}
}
}
我没有测试我的代码,但我知道我没有走上正确的道路,因为我没有考虑以下因素:
- 如果所有的优惠券都用完了,还需要付钱的话,剩下的价值
- 我的代码可以在明年 1 月运行吗?
- 我的 while 循环,我应该继续循环直到我有足够的凭证(当然不是)
我想我快到了。我只是想知道如何实现一个休息变量来检查在骑行结束时是否所有优惠券都已用完并且仍然需要支付休息。
【问题讨论】:
-
减去所有优惠券后剩下的不就是你的“剩余”价值吗?
-
你可以说,但我怎么能指定我的
while循环呢?我如何在我的while-loop 中指定我需要继续,直到我没有更多的优惠券并且剩下的要支付? -
从您的
SubtractCoupons方法中返回减法结果并将该结果用于您的while条件? -
while (result of subtraction > 0)? -
@RobertHarvey 不能用作减去任何东西之前的时间......
标签: c# algorithm while-loop