This DataModel class provides a storage for the guide data in the MIDlet.
The class implements the singleton pattern, and the getInstance method provides access to the singleton object.
public static DataModel getInstance() { if (self == null) { self = new DataModel(); } return self; }
The user's current position is stored in the singleton object. The distanceToAttraction method returns the distance from the user's current position (if known) to the given attraction. The method returns a string, so the distance can be returned either in meters or kilometers.
private String distanceToAttraction(Attraction attraction) { final GeoCoordinate position = currentPosition; if (position == null) { return null; } double distance = position.distanceTo(attraction.getLocation()); if (distance < 1000.0) { return ((int) (distance)) + " m"; } else { String d = String.valueOf(distance % 1000).substring(0, 1); return (int) (distance / 1000) + "." + d + " km"; } }
The updateDistances method updates the distances from the user's position to the attractions.
private void updateDistances() { Vector attrs = attractions; if (attrs != null) { for (int i = 0; i < attrs.size(); i++) { Attraction a = (Attraction) attrs.elementAt(i); a.setDistance(distanceToAttraction(a)); } } }
The Guide class represents a tourist guide.
The id field uniquely identifies guides, so the equals and hashCode methods are overwritten to use only the ID field.
public final boolean equals(Object o) { if (o != null && o instanceof Guide) { return this.id.equals(((Guide) o).id); } return false; } public final int hashCode() { return this.id.hashCode(); }
The writeTo method writes the fields that are needed to download a guide from the back-end server to a data output stream.
public final void writeTo(DataOutputStream dout) throws IOException { dout.writeUTF(id); dout.writeUTF(purchaseTicket); dout.writeUTF(url); dout.writeUTF(imageUrl); dout.writeUTF(city); dout.writeUTF(account); }
The readFrom method creates the guide from the data input stream.
public static Guide readFrom(DataInputStream din) throws IOException { Guide guide = new Guide(); guide.id = din.readUTF(); guide.purchaseTicket = din.readUTF(); guide.url = din.readUTF(); guide.imageUrl = din.readUTF(); guide.city = din.readUTF(); guide.account = din.readUTF(); return guide; }
The Attraction class represents an attraction in a tourist guide. An attraction contains an image, description text, and coordinates.