# Check difference between no. of stops, calculate cost

• November 14th, 2009, 08:46 AM
JavaStudent23
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.
• November 14th, 2009, 09:17 AM
bguy
Re: Check difference between no. of stops, calculate cost
Quote:

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:
Code :

```if(stopfrom > stopto) { stops = stopto+numberofstops-stopfrom; } else { stops=stopto-stopfrom; }```
The other, simpler solution, is to use:
Code :

`stops=Math.abs(stopto-stopfrom);`
so a negitive number will become positive.

2) This is just a simple if else solution:
Code :

```if(stops<=3) charge=firstthreestopscost; else charge=(stops-3)*stopscost+firstthreestopscost;```
• November 14th, 2009, 09:18 AM
Json
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.

Code :

``` 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
• November 16th, 2009, 05:18 PM
JavaStudent23
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 :

```  // code omitted   if((stopNumber < 20) && (stopNumber > 1));   //code omitted```

This automatically sets the bounderies for the stops, It's done the trick so far...
• November 17th, 2009, 02:29 AM
Json
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