首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

上次的周六日时段收费有关问题没解决,求大神

2013-12-02 
上次的周六日时段收费问题没解决,求大神本帖最后由 bulls5988 于 2013-11-26 19:10:36 编辑需实现的功能有

上次的周六日时段收费问题没解决,求大神
本帖最后由 bulls5988 于 2013-11-26 19:10:36 编辑 需实现的功能有:员工停车福利。周一到周五,每天7:00~23:00之间免费停车,其他时段每小时2元收费;周六日没有免费时间每小时2元;这个函数只实现了同一天内算免费时段和收费时段收取的费用。如果有中间跨了多天费用就不对了。


private static int staff_times(DateTime dateTime1, DateTime dateTime2,int fee_standard)
//分别为入场时间,离开时间,每小时收多少钱(收费标准)
        {
            int fee = 0;//总收费
            int hours = 0;//总停车时间(小时)
            DateTime dt1 = dateTime1.Date.AddHours(7);//早晨7点
            DateTime dt2 = dateTime2.Date.AddHours(23);//晚间23点
            staff_card weeks = new staff_card();
            string week_nums = weeks.Weeks_time().ToString();//函数返回当前日期是星期几

                if ((dateTime1 > dt1) && (dt2 > dateTime2))//7点之后来23点之前走的车
                {
                    return fee = 0;//在免费时间段内,免费
                }
                else
                {
                    if (dateTime1 < dt1)//7点之后来的车
                    {
                        if (dt2 < dateTime2)//23点之后走的车
                        {
                            TimeSpan ts = dateTime2 - dt2;//离开时间-23点,计算时间小时数
                            hours = (int)ts.TotalHours;
                            if (ts.Minutes < 15)//不满15分钟
                            {
                                hours = 0;//免费
                            }
                            else
                            {
                                hours = hours + 1;//超过15分钟按一小时收费
                            }
                        }
                    }
                    else if (dateTime1 > dt1)//7点之前来的车
                    {
                        if (dateTime2 < dt2)//23点前就走的车
                        {
                            TimeSpan ts = dateTime2 - dt1;//离开时间-7点
,计算时间小时数
                            hours = (int)ts.TotalHours;


                            if (ts.Minutes < 15)//不满15分钟
                            {
                                hours = 0;//免费
                            }
                            else
                            {
                                hours = hours + 1;//超过15分钟按一小时收费
                            }
                        }
                        if (dateTime2 > dt2)//如果23点后才走
                        {
                            TimeSpan ts = (dt1 - dateTime1) + (dateTime2 - dt2);
//离开时间-7点,计算时间小时数
                            hours = (int)ts.TotalHours;
                            if (ts.Minutes < 15)//不满15分钟
                            {
                                hours = 0;//免费
                            }
                            else
                            {
                                hours = hours + 1;//超过15分钟按一小时收费
                            }
                        }

                    }

                }
            
            fee = fee_standard * hours;
            return fee;
        }


[解决办法]
代码好像有点复杂,可以换一种思考来考虑:
假设每星期以周一0点开始(以周日开始也一样),按距离这个时间点的范围定义一个区间收费表(其实就是配置表),从第几小时起到第几小时结束,价格是多少,如

0-7             2元
7-23            0元
23-31(第二天的7点) 2元


31-47(第二天的7点) 0元
...............

上面这个表生成算法很简单,生成之后,就很好计算了,从开始停车dt1开始算,计算出离这周的0点的小时数(如星期二9点开始,计算得出24+9,即第33小时),每次增加1小时,遍历至dt2结束,从配置中取出每一个小时的价格,最后累加起来,最后多出的分钟数额外处理



[解决办法]

 Func<DateTime, DateTime, double> white = (tStart, tEnd) =>
            {
                double res = 0;
                res = ((tEnd.Date - tStart.Date).TotalHours/24) * 8 * 2; //都从零点算起每隔24小时加收16块
                int tStart_hours = tStart.Hour;
                int tEnd_hours = tEnd.Hour;
                if (tStart_hours < 7)
                {
                    //起点从零点起算,自然他多算了一点时间,把这点时间扣除
                    res = res - tStart_hours * 2; 
                }
                if (tStart_hours >= 23&&tStart.Minute>0)
                {
                    res = res - 16;
                }
                if (tEnd_hours < 7)
                {
                    //终点只算到零点,少算了时间,把这时间加上
                    res = res + (7-tStart_hours) * 2;
                }
                if (tEnd_hours >= 23&&tEnd.Minute>0)
                {
                    res = res + 16;
                }
                //计算这时间段内多少个星期六
               int Saturdays = (((int)tStart.DayOfWeek+(int)DayOfWeek.Saturday-5) + (tEnd.Date - tStart.Date).Days) / 7;
                //计算这时间段内多少个星期天
                int sundays = (((int)tStart.DayOfWeek+(int)DayOfWeek.Saturday-6) + (tEnd.Date - tStart.Date).Days) / 7;
                //冲消前面星期六,星期天多算的加收费用,重新计算星期六,星期天的加收费用并附加上
                res = res - (Saturdays + sundays) * 8 * 2 + (Saturdays + sundays) * 24 * 2;
                return res;
            };

            var res1 = white(DateTime.Parse("2013-11-1 5:10:51"), DateTime.Parse("2013-11-3 23:39:51"));

      
[解决办法]


            DateTime startTime = DateTime.Now;
            DateTime endTime = startTime.AddHours(50).AddMinutes(16);
            TimeSpan ts = endTime - startTime;
            int allHours = (int)ts.TotalHours + (ts.Minutes >= 15 ? 1 : 0);

            Console.WriteLine(allHours);
            int day = (int)ts.TotalDays+1;


            DateTime temp=startTime;
            for (int i = 1; i <= day; i++)
            {
                temp=temp.AddDays(1);
                int week = (int)temp.DayOfWeek;
                if (endTime >= temp)
                {
                    if (week >= 1 && week <= 5)//周一至周五
                    {
                        allHours = allHours - (23 - 7);//全天
                    }
                }
                else
                {
                    if (week >= 1 && week <= 5)//周一至周五
                    {
                        TimeSpan tempTs = endTime - startTime;
                        if (day > 1)
                        {
                           int tempHoure= endTime.Hour-7;//0-7点时间段
                           if (tempHoure > (23 - 7)) tempHoure = tempHoure - 1;//23-0点时间段
                           if (tempHoure > 0)
                           {
                               allHours = allHours - tempHoure;
                           }
                        }
                    }
                }
            }
            Console.WriteLine(allHours);
            Console.WriteLine(startTime);
            Console.WriteLine(endTime);



给你一段
[解决办法]
//test cases

using RateCalculator;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;


namespace RateCalculatorTest
{
    [TestClass()]
    public class CarParkPaymentTest
    {
        [TestMethod()]
        public void Shoud_Charge_0_dollar_for_zeor_parking_duration()
        {
            Payment target = new Payment();
            target.Begin = new DateTime(2013, 11, 25, 9, 0, 0);
            target.End = new DateTime(2013, 11, 25, 9, 0, 0);
            Decimal total = target.TotalCharge();


            Assert.AreEqual(total, 0);
        }


        [TestMethod()]
        public void Shoud_Charge_0_dollar_for_14minutes_parking_duration()
        {
            Payment target = new Payment();
            target.Begin = new DateTime(2013, 11, 25, 9, 0, 0);
            target.End = new DateTime(2013, 11, 25, 9, 14, 0);
            Decimal total = target.TotalCharge();
            Assert.AreEqual(total, 0);
        }


        [TestMethod()]
        public void Shoud_Charge_2_dollar_for_15minutes_parking_duration_on_Sunday()
        {
            Payment target = new Payment();
            target.Begin = new DateTime(2013, 11, 24, 9, 0, 0);
            target.End = new DateTime(2013, 11, 24, 9, 15, 0);
            Decimal total = target.TotalCharge();
            Assert.AreEqual(total, 2);
        }


        [TestMethod()]
        public void Shoud_Charge_2_dollar_for_15minutes_parking_duration_on_Weekdays_FreeParkingHours()
        {
            Payment target = new Payment();
            target.Begin = new DateTime(2013, 11, 27, 9, 0, 0);
            target.End = new DateTime(2013, 11, 27, 9, 15, 0);
            Decimal total = target.TotalCharge();
            Assert.AreEqual(total, 0);
        }



        [TestMethod()]
        public void MondayFreeParkingHour_Should_Charge_0_Dollar_Between_9am_and_11am()
        {
            Payment target = new Payment();
            target.Begin = new DateTime(2013, 11, 25, 9, 0, 0);
            target.End = new DateTime(2013, 11, 25, 11, 0, 0);
            Decimal total = target.TotalCharge();
            Assert.AreEqual(0, total);
        }

        [TestMethod()]
        public void MondayFreeParkingHour_Should_Charge_0_Dollar_Between_9am_and_1030am()
        {
            Payment target = new Payment();
            target.Begin = new DateTime(2013, 11, 25, 9, 0, 0);
            target.End = new DateTime(2013, 11, 25, 10, 30, 0);
            Decimal total = target.TotalCharge();
            Assert.AreEqual(0, total);
        }


        [TestMethod()]
        public void MondayNonFreeParkingHour_Should_Charge_4_Dollars_Between_23pm_and_1am_nextday()
        {
            Payment target = new Payment();
            target.Begin = new DateTime(2013, 11, 25, 23, 0, 0);
            target.End = new DateTime(2013, 11, 26, 1, 0, 0);


            Decimal total = target.TotalCharge();
            Assert.AreEqual(4, total);
        }


        [TestMethod()]
        public void Should_Charge_4_Dollars_on_Sunday()
        {
            Payment target = new Payment();
            target.Begin = new DateTime(2013, 11, 24, 5, 0, 0);
            target.End = new DateTime(2013, 11, 24, 7, 0, 0);
            Decimal total = target.TotalCharge();
            Assert.AreEqual(4, total);
        }

        [TestMethod()]
        public void Should_Charge_4_Dollars_on_Sunday_for_1_Hour_45_minutes()
        {
            Payment target = new Payment();
            target.Begin = new DateTime(2013, 11, 24, 8, 0, 0);
            target.End = new DateTime(2013, 11, 24, 9, 45, 0);
            Decimal total = target.TotalCharge();
            Assert.AreEqual(4, total);
        }

        [TestMethod()]
        public void Should_Charge_4_Dollars_on_Saturday_for_1_Hour_45_minutes()
        {
            Payment target = new Payment();
            target.Begin = new DateTime(2013, 11, 23, 8, 0, 0);
            target.End = new DateTime(2013, 11, 23, 9, 45, 0);
            Decimal total = target.TotalCharge();
            Assert.AreEqual(4, total);
        }

        
        [TestMethod()]
        public void Should_Charge_54_Dollars_Between_Nov24_0050_and_Nov25_1135()
        {
            Payment target = new Payment();
            target.Begin = new DateTime(2013, 11, 24, 5, 0, 0);
            target.End = new DateTime(2013, 11, 25, 23, 35, 0);
            Decimal total = target.TotalCharge();
            Assert.AreEqual(54, total);
        }


        [TestMethod()]
        public void Should_Charge_0_Dollar_Between_Nov25_0659_and_Nov25_2300()
        {
            Payment target = new Payment();
            target.Begin = new DateTime(2013, 11, 25, 6, 59, 59);
            target.End = new DateTime(2013, 11, 25, 23, 0, 0);
            Decimal total = target.TotalCharge();
            Assert.AreEqual(0, total);
        }

        [TestMethod()]
        public void Should_Charge_2_Dollars_Between_Nov24_2245_and_Nov24_2300()
        {
            Payment target = new Payment();
            target.Begin = new DateTime(2013, 11, 24, 22, 45, 0);
            target.End = new DateTime(2013, 11, 24, 23, 0, 0);


            decimal total = target.TotalCharge();
            Assert.AreEqual(2, total);
        }


        [TestMethod()]
        public void Should_Charge_2_Dollars_Between_Nov24_2345_and_Nov25_0000()
        {
            Payment target = new Payment();
            target.Begin = new DateTime(2013, 11, 24, 23, 45, 0);
            target.End = new DateTime(2013, 11, 25, 0, 0, 0);
            decimal total = target.TotalCharge();
            Assert.AreEqual(2, total);
        }
    }
}

热点排行