Question:
Java Programming logic for parking fee?
anjo z
2010-11-18 03:23:01 UTC
I Have a problem with this logic in the car parking the program is i need to use calendar from the api to get the hour in the time in and time out. I have a problem with the overnight parking because i use the hour in the system time if i time In in 20 o clock and i want to time out in like 5 oclock the computation is ruined can you give me any suggestions on how to do this

1st Class

public int price()
{
int T = Out - In;

if(Tl == 0)
return 0;
else if(T < 3)
return ITime;
else
T -= 3;
return (5 * T) + 15;
}

2nd Class
Calendar cal = Calendar.getInstance();
c.setcar(cal.getTime().getHours());
if((c.In() >=1 ) && c.Out() == 0))
JOptionPane.showMessageDialog(null, " OverNightParking: Your Fee is 200 Pesos");
else if(c.price() >= 60)
JOptionPane.showMessageDialog(null, " Your Parking Fee is 60 ");
else if(c.price() == 0)
JOptionPane.showMessageDialog(null, " Your Parking Fee is free ");
else
JOptionPane.showMessageDialog(null, "Your Parking Fee is " + car1.price() + "$");
Four answers:
Raul P
2010-11-18 04:23:08 UTC
Don't forget you can use the AM and PM fields to check if it's PM or AM. It will be 0 if Am and 1 if PM if I recall.



So 20 o clock would have an AM_PM value of 1 while 5 o clock would have an AM_PM value of 0.
deonejuan
2010-11-18 05:21:48 UTC
When we 'math' look for smallest unit. With time it is milliseconds.



import java.util.Calendar;





public class ParkTktDiff {



public static void main( String [] args ) {

Calendar today = Calendar.getInstance();

Calendar later = Calendar.getInstance();

long ticToc = 60*60*1000; // 1 hour

ticToc += 10*60*1000; // ten min. more

ticToc += later.getTimeInMillis(); // get the start of time

later.setTimeInMillis(ticToc); // set time forward



long diff = later.getTimeInMillis() - today.getTimeInMillis();



System.out.printf("DooWad Parking: %2d hours : %2d mins%n",

diff / (60*60*1000),

(diff / (60*1000)) - 60);



}

}

//.====================

long diffSeconds = diff / 1000;

long diffMinutes = diff / (60 * 1000);

long diffHours = diff / (60 * 60 * 1000);

long diffDays = diff / (24 * 60 * 60 * 1000);
tru_story
2010-11-18 03:31:08 UTC
If the calendar object can also give you the date you can use that to check if the date has changed and use it to calculate the fee.
anonymous
2010-11-18 03:23:51 UTC
f CV mve ,dzx


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...