Find Itinerary from a given list of tickets
import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; /** * * @author Kartik Mandal * */ public class ItinearyTest { /** * If it is sorted order data in the list. * Find out source and destination. * @return */ private static List<FlightInfo> getFilteredFlightInfos(String originAirportCode, String destinationAirportCode, List<FlightInfo> source) { if (originAirportCode == null && destinationAirportCode == null) { return source; } List<FlightInfo> filteredFlightInfos = new ArrayList<>(); boolean sourceFound = false; boolean destinationFound = false; for (FlightInfo flightInfo : source) { if (!sourceFound) { if (flightInfo.getOrigin().equals(originAirportCode)) { sourceFound = true; } } if (!destinationFound) { if (flightInfo.getDestination() .equals(destinationAirportCode)) { destinationFound = true; } } if (sourceFound || destinationFound) { filteredFlightInfos.add(FlightInfo); } if (sourceFound && destinationFound) { return filteredFlightInfos; } } throw new RuntimeException("Error"); } /** * If it is unsorted data Create a HashMap of given pair of itinerary. Find * the starting point of itinerary. Create a reverse HashMap. Traverse * 'travelItinerary'. For every key of dataset, check if it is there in * 'reverseMap'. If a key is not present, then we found the starting point. * Now select the sub itinerary from and to of the range and put in * linkedList. Match the filter data. * * @param originAirportCode * @param destinationAirportCode * @param selectOrigin * @param travelItinerary * @return */ private static boolean isAirportCodeMatched(String originAirportCode, String destinationAirportCode, String selectOrigin, Map<String, String> travelItinerary) { if (originAirportCode == null && destinationAirportCode == null) { return true; } Map<String, String> reverseItineraryMap = new HashMap<String, String>(); for (Map.Entry<String, String> entry : travelItinerary.entrySet()) reverseItineraryMap.put(entry.getValue(), entry.getKey()); String start = null; for (Map.Entry<String, String> entry : travelItinerary.entrySet()) { if (!reverseItineraryMap.containsKey(entry.getKey())) { start = entry.getKey(); break; } } if (start == null) { return false; } String origin = start; String destination = travelItinerary.get(start); List<String> list = new LinkedList<>(); boolean flag = false; if (start.equals(originAirportCode)) { list.add(start); while (destination != null) { if (destination.equals(destinationAirportCode)) { flag = true; break; } list.add(destination); destination = travelItinerary.get(destination); } } else { destination = travelItinerary.get(start); while (destination != null) { if (origin.equals(originAirportCode)) { list.add(origin); break; } origin = destination; destination = travelItinerary.get(destination); } while (destination != null) { if (destination.equals(destinationAirportCode)) { flag = true; break; } list.add(destination); destination = travelItinerary.get(destination); } } if (list.contains(originAirportCode) && flag) { return list.contains(selectOrigin); } else { return false; } } public static void main(String[] args) { System.out .println("==========================UnSorted List from sorce to destination ======================"); Map<String, String> travelItinerary = new HashMap<String, String>(); travelItinerary.put("MSP", "ORD"); travelItinerary.put("TXL", "JFK"); travelItinerary.put("ORD", "TXL"); travelItinerary.put("JFK", "BOM"); travelItinerary.put("BOM", "CCU"); System.out.println(isAirportCodeMatched("ORD", "CCU", "JFK", travelItinerary)); System.out .println("==========================Sorted List from sorce to destination ======================"); List<FlightInfoTO> list = new ArrayList<FlightInfoTO>(); FlightInfoTO f1 = new FlightInfoTO("MSP", "ORD"); FlightInfoTO f2 = new FlightInfoTO("ORD", "TXL"); FlightInfoTO f3 = new FlightInfoTO("TXL", "JFK"); FlightInfoTO f4 = new FlightInfoTO("JFK", "BOM"); FlightInfoTO f5 = new FlightInfoTO("BOM", "CCU"); list.add(f1); list.add(f2); list.add(f3); list.add(f4); list.add(f5); List<FlightInfoTO> sss = getFilteredFlightInfoTOs("ORD", "CCU", list); System.out.println(sss); } }
public class FlightInfo { private String origin; private String destination; public FlightInfo(String origin, String destination) { super(); this.origin = origin; this.destination = destination; } public String getOrigin() { return origin; } public void setOrigin(String origin) { this.origin = origin; } public String getDestination() { return destination; } public void setDestination(String destination) { this.destination = destination; } @Override public String toString() { return "FlightInfo [origin=" + origin + ", destination=" + destination + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((destination == null) ? 0 : destination.hashCode()); result = prime * result + ((origin == null) ? 0 : origin.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; FlightInfo other = (FlightInfo) obj; if (destination == null) { if (other.destination != null) return false; } else if (!destination.equals(other.destination)) return false; if (origin == null) { if (other.origin != null) return false; } else if (!origin.equals(other.origin)) return false; return true; } }
Given a list of tickets, find itinerary in order using the given list. Example: Input: "Chennai" -> "Banglore" "Bombay" -> "Delhi" "Goa" -> "Chennai" "Delhi" -> "Goa" Output: Bombay->Delhi, Delhi->Goa, Goa->Chennai, Chennai->Banglore,Reference: 1Reference: 2
Other Links: Post and Get method under standing Get and Post Method understand Inheritance Understand Part 3 Robot move GENERATING KEYSTORE FILES