Results 1 to 9 of 9
  1. #1
    mnorman is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

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

    Any help you could provide would be much appreciated.

  2. #2
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default 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. #3
    mnorman is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default 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 12:21 AM.

  4. #4
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default 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 12:40 AM.

  5. #5
    mnorman is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default 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. #6
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default 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. #7
    mnorman is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: need help figuring out algorithm

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

  8. #8
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: need help figuring out algorithm

    Np, best of luck with the project!

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

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

Similar Threads

  1. Help figuring out a regular expression!
    By srs in forum New To Java
    Replies: 1
    Last Post: 12-10-2011, 02:51 AM
  2. Figuring out for????
    By seanfmglobal in forum New To Java
    Replies: 4
    Last Post: 07-21-2011, 09:47 PM
  3. Help with figuring out the output
    By Yakg in forum New To Java
    Replies: 3
    Last Post: 12-13-2010, 09:32 AM
  4. Replies: 3
    Last Post: 06-10-2010, 03:42 PM
  5. figuring out the current colour of my appelet
    By vortexprogramming in forum New To Java
    Replies: 2
    Last Post: 07-15-2008, 04:16 AM

Posting Permissions

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