Reputation: 21
I want to compute ticket price between each stations based on the price matrix:
a b c
a 0 2 3
b 4 0 1
c 7 2 0
Example: from a to b
print 2
or from c to a
print 7
based on values in the above price matrix.
Here it is, I want to print the railway ticket fare based on the two station lists: "from:" list and "to:" list. I want to print fare after comparing. There is a fixed fare for each combination. For example station a to station b, the fare is 10. For any one station to other station there is a fixed fare.
Upvotes: 1
Views: 250
Reputation: 13789
You could also use guava's Table.
Exmaple:
Table<Integer, String, String> table = HashBasedTable.create();
table.put(1, "a", "1a");
table.put(1, "b", "1b");
table.put(2, "a", "2a");
table.put(2, "b", "2b");
Upvotes: 0
Reputation: 837
I would create a class, which is responsible for storing the fares.
public class FareStorage {
Map<TownCombination, Double> fares;
//...
public double getFare(String townA, String townB) {
return fares.get(new TownCombination(townA, townB));
}
public void addFare(String townA, String townB, double fare) {
fares.put(new TownCombination(townA, townB));
}
class TownCombination {
String town1;
String town2;
//If a fare from A to B is equals the fare from B to A,
//then the A-B and the B-A combinations should be equal.
//Override hashCode and equals the way you want.
}
}
It is not complete, but I hope you get the idea. This is how you can use it:
FareStorage storage = new FareStorage();
storage.addFare("A", "B", 10.2);
//....
double fare = storage.get("A", "B");
Upvotes: 1