Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have mentioned my problem below
C#
st = "UPDATE tbl_time set time_out='" + dt1.ToString("hh:mm:ss") + "' WHERE ID=(SELECT MAX(ID) FROM tbl_time)";
db.ExeQuery(st);
st = "Select * from tbl_time where ID=(Select MAX(ID) from tbl_time)";
dr = db.readall(st);
if (dr.Read() == true)
{
   txt1.Text = dr["time_in"].ToString();
   txt2.Text = dr["time_out"].ToString();
}
TimeSpan t1 = TimeSpan.Parse(txt1.Text);
TimeSpan t2 = TimeSpan.Parse(txt2.Text);
TimeSpan ts = t2 - t1;
dr.Close();
st = "UPDATE tbl_time set tot='" + ts + "' WHERE ID=(SELECT MAX(ID) FROM tbl_time)";
db.ExeQuery(st);
st = "Select * from tbl_time where ID=(Select MAX(ID) from tbl_time)";
dr = db.readall(st);
if (dr.Read() == true)
{
   txt3.Text = dr["tot"].ToString();
}
dr.Close();
TimeSpan t3 = TimeSpan.Parse(txt3.Text);
double fee = 0;
  
// In ts variable i'm storing the start and end time i.e total time
if (ts.TotalMinutes < 60)   // Here is my doubt If the user spend time less than 60 min i want make charge of  10Rs i.e i have done 
{                                        //but if the user spend 120 min i want make charge of 20Rs, but in the database it saving like 60rs.
   // I'm not getting where my calculation is wrong   
   st = "UPDATE tbl_time set amt='" + fee + "' WHERE ID=(SELECT MAX(ID) FROM tbl_time)";
   db.ExeQuery(st);
}
if (ts.TotalMilliseconds > 60)
{
   fee = (ts.TotalMinutes / 10) * 10;
   st = "UPDATE tbl_time set amt='" + fee + "' WHERE ID=(SELECT MAX(ID) FROM tbl_time)";
   db.ExeQuery(st);
}
Posted
Updated 18-Feb-15 3:05am
v2
Comments
phil.o 18-Feb-15 9:03am    
Not a question. This is called a requirement.
Where are you stuck.
Member 10637123 18-Feb-15 9:09am    
bro if the user spend less than 60min make charge of 10rs or if the user spend more than 60 min increase the charge of Rs according to time
CHill60 18-Feb-15 9:19am    
phil.o will not be notified of your response if you don't use the Reply link next to his post.
Don't put your question in comments - put it in plain text... it is easier for us to read and therefore easier for us to help you
CHill60 18-Feb-15 9:21am    
By the way, in some cultures "bro" can imply "obnoxious" or "idiot" - best to avoid the use of that slang where you don't know the cultural impact

I've read your question slightly differently to the other poster, in that I think you need to pr-rata[^] the charge subject to a minimum charge of 10rs.

If it was just based on the number of hours then you could use
double feePerHours = Math.Max(minFee, hourlyFee * (timeInMinutes / 60));
where I've initialised both minFee and hourlyFee to 10rs. Note the use of Math.Max to ensure I never charge less than 10rs.

To get the pro-rata fee (i.e. the bit to add on for every minute over the hour) you can use
double proRataFee = Math.Max(0, ((timeInMinutes - 60) % 60) * (hourlyFee / 60.0));
Note that I've used the modulo operator[^] to get the number-of-minutes-over-an-hour i.e. the remainder when divided by 60. I've also used Max again to ensure that the first hour doesn't start giving me negative numbers.

The total fee is just the two added together. I tested this with this code
C#
int minFee = 10;
double hourlyFee = 10.0;

for (var timeInMinutes = 40; timeInMinutes <= 160; timeInMinutes += 20)
{
    double feePerHours = Math.Max(minFee, hourlyFee * (timeInMinutes / 60));

    double proRataFee = Math.Max(0, ((timeInMinutes - 60) % 60) * (hourlyFee / 60.0));

    double totalFee = feePerHours + proRataFee;

    Console.WriteLine("Time : {0} minutes; Fee for hours {1}, Pro-rata bit {2:0.00} Total Fee R{3:0.00}", timeInMinutes, feePerHours, proRataFee, totalFee);

}
which gave me these results:
Time : 40 minutes; Fee for hours 10, Pro-rata bit 0.00 Total Fee R10.00
Time : 60 minutes; Fee for hours 10, Pro-rata bit 0.00 Total Fee R10.00
Time : 80 minutes; Fee for hours 10, Pro-rata bit 3.33 Total Fee R13.33
Time : 100 minutes; Fee for hours 10, Pro-rata bit 6.67 Total Fee R16.67
Time : 120 minutes; Fee for hours 20, Pro-rata bit 0.00 Total Fee R20.00
Time : 140 minutes; Fee for hours 20, Pro-rata bit 3.33 Total Fee R23.33
Time : 160 minutes; Fee for hours 20, Pro-rata bit 6.67 Total Fee R26.67
 
Share this answer
 
Comments
TheRealSteveJudge 18-Feb-15 10:17am    
We both can only guess what the inquirer wants.
The idea of calculating the exact fee after one hour
is not bad.
5* for thinking ahead.
CHill60 18-Feb-15 10:20am    
:laugh: I've seen that homework before ;)
TheRealSteveJudge 18-Feb-15 10:26am    
ROFL
You already knew the right solution?
CHill60 18-Feb-15 10:32am    
Couldn't find it when I looked - thought I'd answered it before, but it might have been someone else. Typical that I can't find it when I want it of course!
Had to start from scratch :(
Embarrassing bit was not being able to remember the name of the operator - modulo(modulus) ... :-O
TheRealSteveJudge 18-Feb-15 10:48am    
It was a good exercise anyway?
Maybe Code Project should introduce a new category besides "Quick Answers",
e.g. "Homework" :-)
Here is a simple method to achieve what you want.
C#
int CalculateParkingFee(DateTime entryTime, DateTime leaveTime)
{
    DateTime effectiveLeaveTime;

    TimeSpan ellapsedTime = leaveTime.Subtract(entryTime);

    if (ellapsedTime.TotalMinutes % 60 == 0)
    {
        effectiveLeaveTime = leaveTime;
    }
    else
    {
        effectiveLeaveTime = new DateTime(leaveTime.Year, leaveTime.Month, leaveTime.Day, leaveTime.Hour, 0, 0).AddHours(1);
    }

    double accountableHours = effectiveLeaveTime.Subtract(entryTime).TotalHours;

    return (int)accountableHours;
}


You can test it like this:
C#
MessageBox.Show(CalculateParkingFee(new DateTime(2015,2,18,15,0,0), new DateTime(2015,2,18,15,20,0)).ToString());
MessageBox.Show(CalculateParkingFee(new DateTime(2015, 2, 18, 15, 0, 0), new DateTime(2015, 2, 18, 16, 0, 0)).ToString());
MessageBox.Show(CalculateParkingFee(new DateTime(2015, 2, 18, 15, 0, 0), new DateTime(2015, 2, 18, 16, 20, 0)).ToString());


The results are:
1
1
2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900