Question

I currently have an Android application that displays a schedule for a ferry boat. The application can display the full schedule (just a giant list), but the selling point in the application is it will display when the next two ferries are departing and how long from the current time that departure is.

I am relatively new to Java and currently use large Switch() statements in my code. Basically it gets the current phone time and compares it to all of the times in the schedule at which point it displays the next two departure times and then calculates the difference between current time and the departure times.

I am sure that a switch statement is not the best idea for speed purposes as well as code changing purposes. For example if one time changes its a bunch of lines of code to go in and fix for that one time change. Also if the entire schedule changes everyone has to update their app for the time change to take effect. My ideal situation would be to store a file somewhere on my webserver that could be downloaded and inserted into a hashmap (I think is the correct term) that would load the new schedule if there was a time change.

Not sure how confusing this is, but it would be greatly appreciated if someone could explain how I might use a hashmap or something else you might recommend to get this task accomplished. Currently the variables are the two ferry terminals as well as the day of the week since the schedule changes per day (monday, tues-friday, saturday, sunday).

Below is a screenshot of the application so you can understand it if my post wasn't clear. Thank you in advance.

Screenshot:

screenshot

Was it helpful?

Solution

Store the schedule objects in a sorted array. You can then binary search the array for the first value greater than the current time. You'll probably use some parent array consisting of the location and applicable day of the week.

You can easily write that kind of data structure to a file that is read & parsed by the application for updates instead of being compiled into the code.

Details of this? First, understand resources in Android. If no updated schedule exists, fall back to the default resource.

Second, use an HTTP head request to check if a newer file exists. If it does, parse, download & save state. Saving Android Activity state using Save Instance State.

Finally, XML is handy for data distribution, even if it's not fast. Everybody understands it and it's easy to update or hand off.

<ferry location=0 time=2045>
    <day>1</day>
    <day>2</day>
    <day>3</day>
    <day>4</day>
    <day>5</day>
</ferry>

<ferry location=0 time=0800>
    <day>6</day>
</ferry>

OTHER TIPS

You will need something like a database to hold the schedule data. That will help you to seperate code from data. I'm not familiar with Android but i think there is a interface to sqlite database on the device.

Further, as this is an application on a small device you may connect to the schedule database on a server thru the internet connection. That way you have to maintain schedule data only in one place (on the server) and clients will use always up to date data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top