• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Help with IF statements

djaliboy

Lurker
Hi guys, im in need of some help with my project, so any help would be greatly appreciated.

My app is to be able to find out the cost of a journey between two stations. The cost of the journey depends on which zones the station is located in.

So far i have two spinners which have a list of stations :-

Station A = Zone 2
Station B = Zone 3
Station C = Zone 3
Station D = Zone 1
Station E = Zone 2 etc...

The only method i could think of is very long and tiring, which is to do an if statement for each possible journey e.g if "spinner1 = station A" and "spinner 2 = Station B" then "cost = 25"

I was hoping to find a quicker method based on the zones, for example all the journeys from Zone 1 to Zone 2 = "Cost=20"

any examples or pointers in the right direction would be great :)
 
even though am not entirely sure what you are doing here
but another idea is to have a 2dim array (matrix)
and have the values inside
for example, if it was like this
Code:
Station A to B = 25
Station A to C = 30

Station B to A = 10
Station B to C = 50

Station C to A = 15
Station C to B = 40
create a matrix that looks like the following

Code:
X    A     B     C
-------------------
A    0     25   30

B    10    0    50

C    15    40    0

so if you want to access from A to B you can just go to matrix[0][1];
ofcourse you'll need to enter them manually at first , but that'll be faster than going through the whole if Statements

you can also make it so the matrix contains the prices for zones, and make some if statements determining in which zone is this station
I recommend using hashsets

Code:
Set<String> Zone1 = new Hashset();

then just add the names of the stations manually inside the set using

Code:
Zone1.add("Station 1");

and when looking you'll do it like this

Code:
if(Zone1.Contains(Station)){
Source = 1;
}

where Station is the name of the station you looking for and source is an int defining the zone

hope I was clear in that :D
 
Back
Top Bottom