首页 > 解决方案 > C#根据折扣表计算房间租金

问题描述

我正在尝试根据折扣表计算房间租金。问题陈述:一家公司为其员工在自己的宾馆之一提供补贴住宿。折扣方案是

这里的问题是我的计算总是错误的。例如,如果员工已经在宾馆住了 4 天,现在要求再住 3 天,则租金应按 1 天计算,折扣为 0%。同样,如果员工已经在那里呆了 2 天,现在要求再呆 5 天,他应该被收取 2 天的 25% 折扣、2 天的 15% 折扣和 1 天的 0% 折扣。我希望这能说明问题。

这是程序结构:

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        List<ExemptionDetails> exemptionDetailsList = new List<ExemptionDetails>();

        exemptionDetailsList.Add(new ExemptionDetails{ExemptionTo = 2, ExemptionPercentage = 50});//First 2 Days, 50% off
        exemptionDetailsList.Add(new ExemptionDetails{ExemptionTo = 2, ExemptionPercentage = 25});//Next 2 Days, 25% off
        exemptionDetailsList.Add(new ExemptionDetails{ExemptionTo = 2, ExemptionPercentage = 15});//Next 2 Days, 15% off
        //No reimbursement for more than 6 days

        var perDayRent = 2000.00M;//per day rent of the room
        var daysAvailedInPast = 2;//has already availed 2 days discount
        var daysAppyingNow = 5;//need to calculate the rent of 5 days based on discount scheme
        var calculatedRent = 0.00M
        foreach (var item in exemptionDetailsList)
        {
            //I have tried a lot of options here but all of them failed. Need to calculate the rent for 5 days according to the list above.
        }

        Console.WriteLine(calculatedRent);//should give result: 8400
    }
}

public class ExemptionDetails
{
    public int ExemptionTo
    {
        get;
        set;
    }

    public int ExemptionPercentage
    {
        get;
        set;
    }
}

标签: c#

解决方案


 public class Program
{
    public static void Main()
    {
        List<ExemptionDetails> exemptionDetailsList = new List<ExemptionDetails>();

        exemptionDetailsList.Add(new ExemptionDetails { ExemptionTo = 2, ExemptionPercentage = 50 });//First 2 Days, 50% off
        exemptionDetailsList.Add(new ExemptionDetails { ExemptionTo = 2, ExemptionPercentage = 25 });//Next 2 Days, 25% off
        exemptionDetailsList.Add(new ExemptionDetails { ExemptionTo = 2, ExemptionPercentage = 15 });//Next 2 Days, 15% off
                                                                                                     //No reimbursement for more than 6 days

        var perDayRent = 2000.00M;//per day rent of the room
        var daysAvailedInPast = 3;//has already availed 2 days discount
        var daysAppyingNow = 5;//need to calculate the rent of 5 days based on discount scheme
        var calculatedRent = 0.00M;
        var daysRemain =  daysAppyingNow;
        foreach (var item in exemptionDetailsList)
        {  if (daysAvailedInPast - item.ExemptionTo >= 0)
            {
                daysAvailedInPast = daysAvailedInPast - item.ExemptionTo;
                continue;
            }

                if (daysRemain > 0)
            {
                if (item.ExemptionTo <= daysRemain) {
                    calculatedRent += ((perDayRent * (100-item.ExemptionPercentage)) / 100) * (item.ExemptionTo - daysAvailedInPast);
                    daysRemain = daysRemain - (item.ExemptionTo - daysAvailedInPast);
                }
                else {
                    calculatedRent += ((perDayRent * (100-item.ExemptionPercentage)) / 100) * daysRemain;
                    daysRemain = 0;
                }
            }
            else
            {
                break;
            }
            daysAvailedInPast = 0;
        }
        if(daysRemain > 0)
        {
            calculatedRent += perDayRent * daysRemain;
        }

        Console.WriteLine(calculatedRent);//should give result: 8400
    }
}

public class ExemptionDetails
{
    public int ExemptionTo
    {
        get;
        set;
    }

    public int ExemptionPercentage
    {
        get;
        set;
    }
}

推荐阅读