Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Check difference between no. of stops, calculate cost

  1. #1
    Junior Member JavaStudent23's Avatar
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Check difference between no. of stops, calculate cost

    Hi everyone,

    I’m working on a public transport card system which records the no. of stops of the journey and then calculates the costs. I’m a bit lost with this program and I hope you can help me...

    Features; the fare for up to 3 stops is charged £4, after than each stop is calculated as £2. How can I calculate the difference in stops between firstStop and lastStop? Also, how do I calculate the price? Let’s say they get on the bus on ‘stop 3’ and go to ‘stop 9’, how would I code this?

    From what I know, it seems that I have to do two different things;

    a) Firstly, code a calculation which checks the difference between the stops, i.e. 6 stops – but then again, what if they go from ‘stop 7’ to ‘stop 2’? I will end up with negative numbers!

    b) Secondly, I have to put the no. of stops in a calculation that will determine the cost...

    So in short I need help with the following;

    1)
    How can I calculate the difference between the stops without going into negative figure?
    2) How can I code a calculation which says; if stops = ‘1, 2, 3’ then charge £4, anything higher add £2 for each stop.

    I urgently need some guidance regarding this,
    Thanks.
    "Do not condemn the judgment of another because it differs from your own. You may both be wrong."

    — Dandemis aka Lao Kiun


  2. #2
    Junior Member
    Join Date
    Oct 2009
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: Check difference between no. of stops, calculate cost

    Quote Originally Posted by JavaStudent23 View Post

    1)
    How can I calculate the difference between the stops without going into negative figure?
    2) How can I code a calculation which says; if stops = ‘1, 2, 3’ then charge £4, anything higher add £2 for each stop.
    1) There are two different options. One, is to have the stops loop in a circle so they go in the order of 1,2,3,4,5,6,1,2... etc. To achieve this, you can have:
    if(stopfrom > stopto)
    {
         stops = stopto+numberofstops-stopfrom;
    }
    else
    {
         stops=stopto-stopfrom;
    }
    The other, simpler solution, is to use:
    stops=Math.abs(stopto-stopfrom);
    so a negitive number will become positive.

    2) This is just a simple if else solution:
    if(stops<=3)
         charge=firstthreestopscost;
    else
         charge=(stops-3)*stopscost+firstthreestopscost;

  3. The Following 2 Users Say Thank You to bguy For This Useful Post:

    JavaPF (December 17th, 2009), JavaStudent23 (November 16th, 2009)

  4. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Check difference between no. of stops, calculate cost

    Hello there,

    What I'd do in this case is probably just calculate the number of stops passed.

        int stops = endStop - startStop;
     
        // If number of stops is a negative number just invert it to a positive number
        if(stops < 0) {
            stops *= -1;
        }
     
        // Do your maths here to calculate the price.

    I believe this would take into account for the fact that a person might travel from stop 9 to stop 4 which would mean they've gone (4 - 9 = -5) -5 stops which inverted would be 5 stops and then you do your calculation from there.

    Simples!

    // Json

  5. The Following User Says Thank You to Json For This Useful Post:

    JavaStudent23 (November 16th, 2009)

  6. #4
    Junior Member JavaStudent23's Avatar
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Check difference between no. of stops, calculate cost

    bguy,
    Thanks for the reply, I appreciated the 2nd method you showed, it had slipped my mind but you brought it all back, i’ve used it now : )

    Json,
    That’s excellent, thank you very much for your feedback, I have fixed that now.


    As for the different stops then I've used the following code;
     
    // code omitted
     
    if((stopNumber < 20) && (stopNumber > 1));
     
    //code omitted

    This automatically sets the bounderies for the stops, It's done the trick so far...
    "Do not condemn the judgment of another because it differs from your own. You may both be wrong."

    — Dandemis aka Lao Kiun

  7. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Check difference between no. of stops, calculate cost

    If that solved the problem for you please mark this thread as SOLVED from the Thread Tools drop down menu at the top.

    Cheers!

    // Json

Similar Threads

  1. Difference between Arraylist and Vector in abstractTableModel ?
    By riddhik84 in forum Collections and Generics
    Replies: 2
    Last Post: November 7th, 2009, 04:22 PM
  2. Calculate primes help
    By TommyFiz in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 27th, 2009, 11:41 PM
  3. Difference between Speech API and Sound API
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: October 22nd, 2009, 12:22 AM
  4. Simple java program to calculate wall covering of a room
    By parvez07 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 22nd, 2009, 03:31 PM
  5. [SOLVED] Difference between public and private variable and their uses
    By napenthia in forum Java Theory & Questions
    Replies: 1
    Last Post: April 22nd, 2009, 11:36 AM