Description: design an underground railway system that keeps track
of customer travel times between different stations
functions:
1. void checkIn(int id, String stationName, int t)
customer with id check in stationName at time t
2. void checkOut(int id, String stationName, int t)
customer with id check out stationName at time t
3. double getAverageTime(String startStation, String endStation)
calculate the average time it takes to traverl from start to end stations
Result:
high level: 2 HashMaps
use a HashMap to store the id & checkInData
Map<Integer, CheckInfo> checkInData
use another HashMap to store the startStation->endStation, & the TimeInfo
Map<String, TimeInfo> averageTimes
here I used 2 helper classes, CheckInfo and TimeInfo
checkInfo stores the check in station & time
TimeInfo stores the totalTime to travel from start->end station & counts of travels
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
privatestaticclassCheckInfo{ String station; int time; publicCheckInfo(String station, int time){ this.station = station; this.time = time; } }
privatestaticclassTimeInfo{ int totalTime; int occurrence; publicTimeInfo(int totalTime, int occurrence){ this.totalTime = totalTime; this.occurrence = occurrence; } }
* when checkIn is called, put the id & checkInfo into the checkInData HashMap
* when checkOut is called, get the checkInfo using given id
and calculate the travel time, put the stations & TimeInfo into the averageTimes HashMap
* when getAverageTime is called, simply get the TimeInfo from averageTimes HashMap
and do a math calculation to get the averageTime