0

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

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
mattdonders
  • 1,328
  • 1
  • 19
  • 42
  • Sounds like a good place to use the command pattern. http://en.wikipedia.org/wiki/Command_pattern Switches do tend to be efficient, though worse for code maintainability. – Matt Ball Oct 11 '11 at 17:40

2 Answers2

1

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>
Community
  • 1
  • 1
Jeff Ferland
  • 17,832
  • 7
  • 46
  • 76
  • 1
    +1, though a small note. OP wants to display the next _two_ values. That's what makes your solution the best: find the first value greater than the current time, and return that one and the one after it (with suitable logic for end-of-day, making sure to deal with different calendars for weekdays, weekends, and holidays). – CPerkins Oct 11 '11 at 17:50
  • In relation to your final comment regarding end-of-day values, wouldn't I just be able to check for an ArrayIndexOutOfBoundsException and then query the next corresponding array for the first time? Also as a follow-up question to this since this already seems like the best answer, is there a standard to arrays per file? Or could I make a file called TimeTableArrays.java and store all of my arrays in there and then reference them from one more file called DisplayNextTime.java (for example)? – mattdonders Oct 11 '11 at 17:58
  • @mattdonders It's my mindset that exceptions are for mistakes rather than program control, so I would personally use direct if/else bounds checking. Either will work, though. You can store the array in any class you wish and reference it externally. I would really write this out to a formatted file that is read by the app rather than a class, though. – Jeff Ferland Oct 11 '11 at 18:06
  • Any advice on a "formatted file"? Is there a recommended format for importing something into an array? Also what is the speed of this process if the application needs to redownload this file from the internet? Figure I can store it initially in a class within the app in case the user does not have internet access. – mattdonders Oct 11 '11 at 18:45
0

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.

PeterMmm
  • 24,152
  • 13
  • 73
  • 111
  • My only fear of this would that it requires an internet connection to work. I would like this application to be available offline as well if (for example) they are taking the Subway they can check the schedule while down there without phone service. – mattdonders Oct 11 '11 at 17:50
  • Then you need some local copy of the important data (local area where user is) till inet connection is available again. That does normally a sub-process of your programm (replication). IMHO the whole thing of mobile apps is that they working always on the best data available. – PeterMmm Oct 11 '11 at 18:05