# Thread: need help figuring out algorithm

1. Member
Join Date
Dec 2011
Posts
15
Rep Power
0

## 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.

2. Member
Join Date
Nov 2011
Posts
65
Rep Power
0

## 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?

3. Member
Join Date
Dec 2011
Posts
15
Rep Power
0

## 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
Last edited by mnorman; 12-15-2011 at 01:21 AM.

4. Member
Join Date
Nov 2011
Posts
65
Rep Power
0

## 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"}
Last edited by kevinn205; 12-15-2011 at 01:40 AM.

5. Member
Join Date
Dec 2011
Posts
15
Rep Power
0

## 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

6. Member
Join Date
Nov 2011
Posts
65
Rep Power
0

## 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);

7. Member
Join Date
Dec 2011
Posts
15
Rep Power
0

## Re: need help figuring out algorithm

oh haha ok thanks :p
and thanks that helped alot, it works now :D

8. Member
Join Date
Nov 2011
Posts
65
Rep Power
0

## Re: need help figuring out algorithm

Np, best of luck with the project!

9. ## 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•