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.

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

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

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

Originally Posted by JavaStudent23

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

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