# need help figuring out algorithm

• 12-15-2011, 12:55 AM
mnorman
need help figuring out algorithm
For my comp sci class I'm making a program that a library would use.
Part of it has to be able to calculate the late fee for a book.
I've figured how to get the current day/month and all that stuff, but
for the life of me I can figure out an equation to calculate how late the book is.
To simplify everything I'm just assuming that all months have 31 days.

• 12-15-2011, 01:02 AM
kevinn205
Re: need help figuring out algorithm
Well I'm assuming you need to have the date it was rented kept track of then subtract that from the current date?
I'm not actually sure if java supports that or has a days360 feature like excel perhaps someone know more try googling it a bit

one option would be to store the month and date seperatly or to read them seperatly and multiply the month *30 + the days?
• 12-15-2011, 01:18 AM
mnorman
Re: need help figuring out algorithm
i have the day the book was taken out and the month stored in seperate text files and am able to read from the
text files no problem.
Oh and i forgot to mention that no late fee is owed until 30 days after the book is taken out.
I have a function for if the book was rented on the same month as it was returned, that works fine.
My problem is, for example if the book was taken out on october 12 and returned on december 14,
i cant make an equation to figure out how many days the book is late by
• 12-15-2011, 01:21 AM
kevinn205
Re: need help figuring out algorithm
excellent well id sugget getting the text into a String or array by using by using a reader and a loop for each value
then I would use a if command if days>30
days/30*2
else {"0"}
• 12-15-2011, 01:36 AM
mnorman
Re: need help figuring out algorithm
is the \$ sign nessary or is that a typo, and if it is what does it do?
also the way i have it set up days is never greater than 30
im using
Calendar calendar = new GregorianCalendar();
int newMonth = calendar.get(Calendar.MONTH);
int newDay = calendar.get(Calendar.DAY_OF_MONTH);

to get the date if thats any help
• 12-15-2011, 01:43 AM
kevinn205
Re: need help figuring out algorithm
Wouldnt compile with the \$ sign it was just to label so you understood what i meant, the if statement also isnt formatted correctly it was just saying you should use one :D
int Both = newMonth*30 + newDay;
system.out.println(Both);
• 12-15-2011, 01:47 AM
mnorman
Re: need help figuring out algorithm
oh haha ok thanks :p
and thanks that helped alot, it works now :D
• 12-15-2011, 01:49 AM
kevinn205
Re: need help figuring out algorithm
Np, best of luck with the project!
• 12-15-2011, 02:41 AM
Junky
Re: need help figuring out algorithm
Use Calendar class to represent your 2 dates.
Convert them to milliseconds.
Subtract one from the other.
Divide by 86400000 (number of milliseconds in one day).

The above should work but beware of "off-by-one" errors.