首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建动态优惠券检查器

问创建动态优惠券检查器
EN

Stack Overflow用户
提问于 2018-07-05 18:42:14
回答 2查看 303关注 0票数 4

我正在尝试创建一个优惠券计数器,以检查可组合的优惠券的可用性,该优惠券可用于减少发票。

如果可用的优惠券等于所需的优惠券,则不需要发票。

示例:

公司XYZ需要支付100美元(金额总是可以除以25和0休息),但他们可以用优惠券支付。

一张优惠券价值25美元,所以我需要检查一下公司是否在一个动态选择的月份内节省了4张。

我的优惠券表看起来是:

Total_Coupon_Count

  • ID
  • CompanyID
  • 月份
  • Total_Coupon_Count
  • 年

例续:

XYZ公司3月份有0张优惠券,4月份有1张,5月有0张,6月和7月1日有2张。

XYZ公司可以在3个月前省下优惠券(所以优惠券是五月、六月、七月)。

这个例子的结果需要是:

已经使用了3张优惠券(6月份2张,7月份1张),25美元仍需由客户支付。

我的计划需要做的是:

  • 获取公司可以节省优惠券的月数(在本例中为3)
  • 减去优惠券,直到0为止
  • 获取发票的其余部分并将其显示给用户

这是我现在的代码:

代码语言:javascript
复制
        //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;
                    }
                }
            }

我还没有测试我的代码,但我知道我没有走上正确的轨道,因为我没有考虑到以下几点:

  • 如果使用了所有优惠券,并且仍然需要支付款项,则Rest值。
  • 我的代码明年一月会起作用吗?
  • 我的循环,我是否应该继续循环,直到我有足够的凭证(当然不是)

不过,我想我快到了。我只是想知道我如何实现一个rest变量,以检查在旅程结束时,所有的优惠券是否已经用完,而rest是否仍然需要支付。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-05 19:19:17

为了帮助生成和打印发票,我创建了以下几个模型:

代码语言:javascript
复制
public class Invoice
{
    public int TotalCouponsNeeded { get; set; }
    public int UsedCouponCount { get; set; }
    public List<UsedCouponsForMonth> UsedCoupons { get; set; }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        if (UsedCouponCount == 0)
        {
            sb.Append("0 coupons have been used");
        }
        else
        {
            sb.AppendFormat("{0} have been used ({1})",
                    UsedCouponCount,
                    String.Join(", ", UsedCoupons.Select(x => String.Format("{0} in {1:MMM yyyy}", x.Count, new DateTime(x.Year, x.Month, 1))))
                );
        }
        sb.AppendFormat(", {0} dollars still needs to be paid by client.", 25 * (TotalCouponsNeeded - UsedCouponCount));
        return sb.ToString();
    }       
}

public class UsedCouponsForMonth
{
    public int Month { get; set; }
    public int Year { get; set; }
    public int Count { get; set; }
}

Invoice包含需要多少优惠券和使用了多少优惠券。它还知道如何以所需的格式打印自己。

生成发票的算法如下:

代码语言:javascript
复制
public Invoice GenerateInvoice(int companyId, int maxMonths, int couponsNeeded)
{
    Invoice invoice = new Invoice
    {
        TotalCouponsNeeded = couponsNeeded,
        UsedCouponCount = 0,
        UsedCoupons = new List<UsedCouponsForMonth>(),
    };

    for (int i = 0; i < maxMonths && invoice.UsedCouponCount < invoice.TotalCouponsNeeded; i++)
    {
        DateTime month = DateTime.Now.AddMonths(i - maxMonths + 1);
        int availableForMonth = GetCouponAmountByMonth(companyId, month.Year, month.Month);
        if (availableForMonth <= 0)
        {
            continue;
        }
        int usedThisMonth = (invoice.UsedCouponCount + availableForMonth < invoice.TotalCouponsNeeded)
            ? availableForMonth
            : (couponsNeeded - invoice.UsedCouponCount);
        invoice.UsedCouponCount += usedThisMonth;
        SubtractCoupons(companyId, month.Year, month.Month, usedThisMonth);
    }

    return invoice;
}

这个想法是从最早的一个月开始,使用GetCouponAmountByMonth检查可用优惠券的数量,并根据需要使用SubtractCoupons减去它们。注意,我修改了这些函数,将其作为输入year和month。同时,在invoice.UsedCouponCount变量中跟踪优惠券的总数。当使用的计数等于所需的数时,循环会自动中断。

票数 3
EN

Stack Overflow用户

发布于 2018-07-05 20:11:31

我创建了一些与问题相对应的类,然后是一个可以获得有效优惠券(最早的)的经理,然后从未使用的有效优惠券中计算发票金额。您可以更改优惠券有效的月数,代码将根据需要作出反应。

代码语言:javascript
复制
public class Company
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public List<Coupon> Coupons { get; set; }
        public Company()
        {
            Coupons = new List<Coupon>();
        }
    }
    public class Coupon
    {
        public bool IsUsed { get; set; }
        public double Amount { get; set; }
        public DateTime Date { get; set; }
    }



public static class CouponManager
    {
        public const int MONTHS_BACK = 3;

        /// <summary>
        /// Returns all coupons within X months not used by the company
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static List<Coupon> GetValidCoupons(Company c)
        {
            return c.Coupons.Where(
                t => !t.IsUsed &&
            (Math.Abs((DateTime.Now.Month - t.Date.Month) + 12 * (DateTime.Now.Year - t.Date.Year))<   MONTHS_BACK))
            .OrderBy(t=>t.Date).ToList();
        }
        /// <summary>
        /// Find valid coupons, subtract from invoice each coupon amount until invoice is 0
        /// </summary>
        /// <param name="company"></param>
        /// <param name="invoice"></param>
        /// <returns></returns>
        public static double CalculateInvoice(Company company, double invoice)
        {
            double amountOwed = invoice;
            List<Coupon> coupons = GetValidCoupons(company);
            coupons.ForEach(c => {
                if(invoice <= 0) //we are done with coupons
                    { return; }
                if (c.Amount > invoice) //no credits to give
                { return; }
                invoice = invoice - c.Amount; //subtract amount from invoice
                c.IsUsed = true; //mark coupon as used then update in db later
            });
            return invoice;
        }
        public static Company GetMockedCompany()
        {
            //mock data
            Company myCompany = new Company();
            bool used = false;
            for (int i = 0; i < 215; i++)
            {
                Coupon c = new Coupon();
                c.Date = DateTime.Now.AddDays(-i);
                c.Amount = 5.00;
                c.IsUsed = used;
                used = !used;
                myCompany.Coupons.Add(c);
                Coupon d = new Coupon();
                d.Date = DateTime.Now.AddDays(-i * 4);
                d.Amount = 5.00;
                d.IsUsed = used;
                used = !used;
                myCompany.Coupons.Add(d);
            }
            return myCompany;
        }
    }

然后,您可以在一个被嘲弄的公司中测试这个例程,如下所示:

代码语言:javascript
复制
 double invoice = CouponManager.CalculateInvoice(CouponManager.GetMockedCompany(), 1400); //returns 1070
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51197972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档