c# - C#根据折扣表计算房间租金
问题描述
我正在尝试根据折扣表计算房间租金。问题陈述:一家公司为其员工在自己的宾馆之一提供补贴住宿。折扣方案是
- 前 2 天住宿 50% 折扣
- 接下来 2 天住宿可享受 25% 的折扣
- 接下来 2 天住宿可享受 15% 的折扣
- 入住超过 6 天可享受 0% 折扣
这里的问题是我的计算总是错误的。例如,如果员工已经在宾馆住了 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;
}
}
解决方案
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;
}
}
推荐阅读
- javascript - 使用组件方法中的 vuejs 触发`image-uploader`
- laravel - Laravel 8 的动态路由
- android - 如何在不更改版本的情况下排除重复类
- c#-9.0 - 使用记录而不是类有什么缺点?
- python - 使用 Flasks app.logger 有什么好处?
- javascript - 您可以使用异步函数通过 useState 设置初始状态吗
- c# - 如何将 TestServer 输出记录到测试控制台
- firebase - 未处理的异常:[firebase_auth/invalid-verification-id] 用于创建电话身份验证凭据的验证 ID 无效
- html - 使用 Bulma 在英雄主体顶部阻止内容
- authlib - 如何通过代理发送 Authlib 请求?