File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Check difference between no. of stops, calculate cost Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Check difference between no. of stops, calculate cost" Watch "Check difference between no. of stops, calculate cost" New topic

Check difference between no. of stops, calculate cost

Fernando Malta

Joined: Nov 14, 2009
Posts: 1
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;

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,
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46334
Welcome to JavaRanch

There is no such thing as "urgent" here. You can find a method in the Math class which gives you the absolute value of a number, so it turns -5 to 5.

You will have to write down on a piece of paper how you would charge £2 per stop for more than three stops. And that is more than the buses round here charge
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

the best way to start writing code is to NOT write ANY code.

The first thing i'd do is write down on paper how the logic should work. I.e. what kinds of things do i need to know? Do I need to know what time someone boarded a bus? the city they are in? where they got on? etc.

once i knew what was important, i'd start looking at logic. There appear to be two conditions for your scenario, traveling three or less stops, or traveling more than three.

the logic for under 3 is easy - it's always £4.

over 3 stops is a little harder, but it appears to be £4 plus some amount you'll have to calculate.

once you have it figured out on paper, start coding small pieces of it and testing the small parts. Do NOT just try and code the whole thing at once and test it when you think you're done, or you'll have too many problems.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: Check difference between no. of stops, calculate cost
It's not a secret anymore!