0

I'm developing a video rental application using C# winforms, and came across a problem I can't seem to write up or find the solution to.

The program needs to check the current date and number of days passed and also the range between them.

If the current Date is less than or equal to the date specified, it will not calculate the penalty cost.

Otherwise if the Date today has already passed the date specified, it will calculate the penalty cost multiplied by the number of days that has passed between them.

Here's the sample code I have playing with the idea:

        DateTime db = DateTime.Parse(dateBeforeString);
        DateTime dt = DateTime.Now;

        var dateDiff = (dt - db);
        double totalDays = dateDiff.TotalDays;

        int totalPenalty = initialPenaltyInt*(int)Convert.ToInt64(totalDays);
        int totalCost = totalPenalty + rentalCostInt;

        if(DateTime.Now != db)
        {
            //do stuff here to:
            //check if current day is less than the one on the database
            //set total penalty to zero
        }
        else if(DateTime.Now > db)
        {
            //otherwise calculate the total penalty cost multipled by the number of days passed since a specific date
        }
10
  • 1
    Why are you casting an Int64 to an Int32? ((int)Convert.ToInt64(totalDays); Commented Oct 12, 2016 at 15:29
  • What is your question? Commented Oct 12, 2016 at 15:29
  • 3
    @BerndFischer I'm pretty sure it's "How to check if DateTime is within a specific range" Commented Oct 12, 2016 at 15:30
  • "past a certain date: isnt exactly range checking Commented Oct 12, 2016 at 15:31
  • 2
    I'm pretty sure that this is never true: if(DateTime.Now != db), maybe you want: if(DateTime.Today != db) Commented Oct 12, 2016 at 15:31

1 Answer 1

2

Simplistic, but might help you progress, hopefully:

public class Penalties
{
    // What about this choice of "int" (vs. decimal)?
    public virtual int ComputeOverdueDaysPenalty(int penaltyPerOverdueDay, DateTime dueDate)
    {
        // Work only with year, month, day, to drop time info and ignore time zone
        dueDate = new DateTime(dueDate.Year, dueDate.Month, dueDate.Day);
        var now = DateTime.Now;
        now = new DateTime(now.Year, now.Month, now.Day);
        return now > dueDate ? (int)now.Subtract(dueDate).TotalDays * penaltyPerOverdueDay : 0;
    }
}

class Program
{
    static void Main(string[] args)
    {
        var penalties = new Penalties();

        var now = DateTime.Now;

        // due = today
        // should print 0
        Console.WriteLine(penalties.ComputeOverdueDaysPenalty(1234, new DateTime(now.Year, now.Month, now.Day)));

        // due = today plus 1
        var dueDate = now.AddDays(1);
        // should print 0 again
        Console.WriteLine(penalties.ComputeOverdueDaysPenalty(1234, dueDate));

        // due = today minus 1
        dueDate = dueDate.Subtract(new TimeSpan(48, 0, 0));
        // should print 1234
        Console.WriteLine(penalties.ComputeOverdueDaysPenalty(1234, dueDate));

        // due = today minus 2
        dueDate = dueDate.Subtract(new TimeSpan(24, 0, 0));
        // should print 2468
        Console.WriteLine(penalties.ComputeOverdueDaysPenalty(1234, dueDate));

        dueDate = DateTime.Parse("2016-10-02");
        // should print 12340, as of 10/12/2016
        Console.WriteLine(penalties.ComputeOverdueDaysPenalty(1234, dueDate));

        Console.ReadKey();
    }
}

Just a remark:

I find it a bit odd you've settled for using the int type in that context, btw.

If your "penalty" units are in fact some currency, the recommended data type for that is decimal, in most use cases.

'Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

This solved my problem. I guess I still have a lot to learn and improve, especially with my logical thought process when coding. Thank you for the well-constructed input and code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.