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.

Let’s say they get on the bus on ‘stop 3’ and go to ‘stop 9’, how would I code this?

From stop 3 to stop 9 there are:
3,4,5,6,7,8,9 stops. The first doesn't count because the passenger has just entered the bus so you can not charge him for the stop entered. So he traveled: 4,5,6,7,8,9 stops = 6 stops in total:
'stop 9' - 'stop 3' = '6 stops to charge'

Then use if statements:

//the fare for up to 3 stops is charged £4
int [B]charge[/B] = 4; // that would be the initial value

//after that each stop is calculated as £2
if stops more than 3 then add to [B]charge[/B] 2 for each stop beyond the 3rd

To avoid negative values you can use the absolute value. If the number of stops is negative multiply it by -1 to turn it into positive

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.