# Designing transport service app in Android

I am new to this SE and so bear with me. Sorry for the long post. But this much info is needed to explain !

### Background:

I am student and working in one science institute. Our university has bunch of transport services which runs on daily basis based on pre-existing timings.

For example :

On Mon to Sat: 07:00, 11:00, 16:00 ...
On Sundays: 09:30, 12:30, 21:00 ...


There can be any number of trips per day or even each day can have different trips. So I am designing app which can give users next available transport.

monday = { "07:00", "11:00", "16:00",...}
sunday = { "09:30", "12:30", "21:00", ...}


Let's say I have above as input information. Whenever I run this program, it should give me back next trip when we have input in following form

run() #On Monday 09:00
>>Output : 11:00
run() #On Sunday 13:30
>>Output: 21:00


Initially I thought it is very easy job. But unfortunately, my university added trips post midnight , For example

monday =  { "07:00", "11:00", "16:00",..., "01:30"}
sunday = { "09:30", "12:30", "21:00", ..., "02:00"}


Here trip mentioned in Monday list "01:30" is actually on Tuesday. This broke my existing algorithm. So currently I am using following algorithm,

Step1: Distribute each input into 3 parts : first trip, trips after that and trips after midnight e.g

monday =  { "07:00", "11:00", "16:00",..., "01:30"}
monday_first = {"07:00"}
monday_rest = {"11:00", "16:00"...}
monday_post_midnight = {"01:30"}


Step2 : Combine lists from yesterday's midnight, first trip and rest of the trips

e.g.

new_monday_trip = sunday_post_midnight + monday_first + monday_rest
new_monday_trip = {"02:00", "07:00","11:00", "16:00"...}


Step3: Check current time and get next trip from list. If it is present then show it to user or get first item from next day.

I was wondering if is there any better way.

Note: Each trip input will be always in chronological order

### Example Input Set

monday to saturday =  { "07:00", "11:00", "12:30", "16:00", "21:00", "00:00", "01:30"}
sunday = { "09:30", "12:30", "21:00", "00:30","01:30", "02:00"}


Code I am currently using,

DayTrips.Java

public class DayTrips {
private List<String> rawTrips;
private List<String> today = new ArrayList<>();
private List<String> tomorrow = new ArrayList<>();

public DayTrips(List<String> rawTrips) throws ParseException {
this.rawTrips = rawTrips;
Date firstTrip = null;
SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.getDefault());
firstTrip = format.parse(rawTrips.get(0));
//Now if date is before first date, it should be after midnight trip
for (String trip : rawTrips) {
if (firstTrip.before(format.parse(trip))) {
} else if (firstTrip.after(format.parse(trip))) {
}
}
}

public List<String> getToday() {
}

public List<String> getTomorrow() {
}
}


Week.Java

public class Week {

private SparseArray<List<String>> weekMap;
private List<String> defaultTrips = null;

/**
* WeekMap will contains trips for any day of week. Keys are equal to Calender.DAY_OF_WEEK
* of respective days. Default trips should be in following priority
* Monday > Tuesday > .... > Sunday
*/
public Week(SparseArray<List<String>> weekMap) {
this.weekMap = weekMap;
//Get default trips in given priority
for (int i = Calendar.MONDAY; i <= Calendar.SATURDAY; i++) {
if (weekMap.get(i) != null) {
defaultTrips = weekMap.get(i);
break;
}
}
//In Case only Sunday Trips are available
if (defaultTrips == null) {
defaultTrips  = weekMap.get(Calendar.SUNDAY);
}
}

/**
* This method should return next transport trip for given timing
*
* @param calendar : User defined time
*/
public String[] nextTrip(Calendar calendar) throws ParseException {
Date now = new Date(calendar.getTimeInMillis()); //Record input time
SimpleDateFormat format = new SimpleDateFormat("HH:mm", Locale.getDefault());

//Check with trips for post midnight from yesterday
List<String> beforeFirstTrip = new DayTrips(weekMap.get(calendar.get(Calendar.DAY_OF_WEEK), defaultTrips)).getTomorrow();

for (String s : beforeFirstTrip) {
if (format.parse(s).after(now) || format.parse(s).equals(now)) {
return new String[]{s, "yesterday"};
}
}

//Check today's trips
List<String> todaysTrip = new DayTrips(weekMap.get(calendar.get(Calendar.DAY_OF_WEEK), defaultTrips)).getToday();

for (String s : todaysTrip) {
if (format.parse(s).after(now) || format.parse(s).equals(now)) {
return new String[]{s, "today"};
}
}

//Check tomorrow's trips (i.e. post midnight today)
List<String> tomorrowTrip = new DayTrips(weekMap.get(calendar.get(Calendar.DAY_OF_WEEK), defaultTrips)).getTomorrow();
for (String s : tomorrowTrip) {
//In this case we need to check if time is between last trip of today and first trip of tomorrow
//Hence we should use before instead after for comparing
if (format.parse(s).before(now) || format.parse(s).equals(now)) {
return new String[]{s, "today_after_midnight"};
}
}

//Else get first trip of tomorrow

• @BKSpurgeon Thank for looking into this. Input format is exactly how I have added in question. I will have string arrays (or lists) with each element in "HH:mm" form for few days. Like monday = { "07:00", "11:00", "12:30", "16:00", "21:00", "00:00", "01:30"}, sunday = { "09:30", "12:30", "21:00", "00:30","01:30", "02:00"}. But my problem is now they also have after midnight dates which makes problem difficult. That is why I come up with solution I gave above. At least one good thing is inside each array (or list) all elements will be in chronological order. – Dexter Jun 17 '17 at 12:12