| 1 | |
|---|
| 2 | package atmsdriver.model; |
|---|
| 3 | |
|---|
| 4 | import java.util.ArrayList; |
|---|
| 5 | import java.util.Iterator; |
|---|
| 6 | import java.util.List; |
|---|
| 7 | import java.util.Scanner; |
|---|
| 8 | |
|---|
| 9 | /** |
|---|
| 10 | * PostmileCoords is a collection of Postmiles (id and LatLong Coordinates). |
|---|
| 11 | * @author jdalbey |
|---|
| 12 | */ |
|---|
| 13 | public class PostmileCoords implements Iterable |
|---|
| 14 | { |
|---|
| 15 | private List<Postmile> postmileList; |
|---|
| 16 | public PostmileCoords() |
|---|
| 17 | { |
|---|
| 18 | postmileList = new ArrayList<Postmile>(); |
|---|
| 19 | } |
|---|
| 20 | /** Accessor to value at given index */ |
|---|
| 21 | public Postmile get(int idx) |
|---|
| 22 | { |
|---|
| 23 | return postmileList.get(idx); |
|---|
| 24 | } |
|---|
| 25 | /** Return the size */ |
|---|
| 26 | public int size() |
|---|
| 27 | { |
|---|
| 28 | return postmileList.size(); |
|---|
| 29 | } |
|---|
| 30 | /** Find the postmile with the given name */ |
|---|
| 31 | public Postmile find(String target) |
|---|
| 32 | { |
|---|
| 33 | int idx = 0; |
|---|
| 34 | while (idx < postmileList.size() && !postmileList.get(idx).nameEquals(target)) |
|---|
| 35 | { |
|---|
| 36 | idx++; |
|---|
| 37 | } |
|---|
| 38 | if (idx < postmileList.size()) |
|---|
| 39 | { |
|---|
| 40 | return postmileList.get(idx); |
|---|
| 41 | } |
|---|
| 42 | else |
|---|
| 43 | { |
|---|
| 44 | return null; |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | /** Load the postmile list from external file */ |
|---|
| 48 | public void load(Scanner scan) |
|---|
| 49 | { |
|---|
| 50 | String out = scan.next(); |
|---|
| 51 | // split into an array |
|---|
| 52 | String[] results = out.split("\\n"); |
|---|
| 53 | for (String item : results) |
|---|
| 54 | { |
|---|
| 55 | String[] fields = item.split(","); |
|---|
| 56 | Postmile pm = new Postmile(fields[0],fields[1],fields[2]); |
|---|
| 57 | postmileList.add(pm); |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | @Override |
|---|
| 62 | public Iterator iterator() |
|---|
| 63 | { |
|---|
| 64 | return postmileList.iterator(); |
|---|
| 65 | } |
|---|
| 66 | /** |
|---|
| 67 | * Postmile is a postmile id and its LatLong Coordinates. |
|---|
| 68 | */ |
|---|
| 69 | final static class Postmile |
|---|
| 70 | { |
|---|
| 71 | String name; // the postmile name or id |
|---|
| 72 | String latitude; // the latitude coordinate for this postmile |
|---|
| 73 | String longitude; // the longitude coordinate for this postmile |
|---|
| 74 | public Postmile(String name, String lat, String longitude) |
|---|
| 75 | { |
|---|
| 76 | this.name = name.trim(); |
|---|
| 77 | this.latitude = lat.trim(); |
|---|
| 78 | this.longitude = longitude.trim(); |
|---|
| 79 | } |
|---|
| 80 | public boolean nameEquals(String target) |
|---|
| 81 | { |
|---|
| 82 | return this.name.equals(target); |
|---|
| 83 | } |
|---|
| 84 | @Override public boolean equals(Object aThat) |
|---|
| 85 | { |
|---|
| 86 | if (this == aThat) return true; |
|---|
| 87 | if (!(aThat instanceof Postmile)) return false; |
|---|
| 88 | Postmile that = (Postmile) aThat; |
|---|
| 89 | boolean r1 = this.name.equals(that.name); |
|---|
| 90 | boolean r2 = this.latitude.equals(that.latitude); |
|---|
| 91 | boolean r3 = this.longitude.equals(that.longitude); |
|---|
| 92 | return r1 && r2 && r3; |
|---|
| 93 | } |
|---|
| 94 | @Override public String toString() |
|---|
| 95 | { |
|---|
| 96 | return name + ": " + latitude +","+ longitude; |
|---|
| 97 | } |
|---|
| 98 | public String toJson() |
|---|
| 99 | { |
|---|
| 100 | String kFeature = "\n{\n \"type\": \"Feature\",\n \"id\": \""; |
|---|
| 101 | String kPoint = "\", \n \"geometry\":\n {\n \"type\": \"Point\",\n \"coordinates\": ["; |
|---|
| 102 | String kProp = "]\n" + |
|---|
| 103 | " },\n" + |
|---|
| 104 | " \"properties\": \n" + |
|---|
| 105 | " {\"color\": \"green\""; |
|---|
| 106 | String kTail = "}\n" + |
|---|
| 107 | " },"; |
|---|
| 108 | return kFeature + name + kPoint + longitude + "," + latitude + kProp + kTail; |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | } |
|---|