AirlineProblem.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.Arrays;
  6. public class AirlineProblem {
  7. public static void main(String[] args){
  8. Scanner scannerToReadAirlines = null;
  9. try{
  10. scannerToReadAirlines = new Scanner(new File("airlines.txt"));
  11. }
  12. catch(IOException e){
  13. System.out.println("Could not connect to file airlines.txt.");
  14. System.exit(0);
  15. }
  16. if(scannerToReadAirlines != null){
  17. ArrayList<Airline> airlinesPartnersNetwork = new ArrayList<Airline>();
  18. Airline newAirline;
  19. String lineFromFile;
  20. String[] airlineNames;
  21. while( scannerToReadAirlines.hasNext() ){
  22. lineFromFile = scannerToReadAirlines.nextLine();
  23. airlineNames = lineFromFile.split(",");
  24. newAirline = new Airline(airlineNames);
  25. airlinesPartnersNetwork.add( newAirline );
  26. }
  27. System.out.println(airlinesPartnersNetwork);
  28. Scanner keyboard = new Scanner(System.in);
  29. System.out.print("Enter airline miles are on: ");
  30. String start = keyboard.nextLine();
  31. System.out.print("Enter goal airline: ");
  32. String goal = keyboard.nextLine();
  33. ArrayList<String> pathForMiles = new ArrayList<String>();
  34. ArrayList<String> airlinesVisited = new ArrayList<String>();
  35. if( canRedeem(start, goal, pathForMiles, airlinesVisited, airlinesPartnersNetwork))
  36. System.out.println("Path to redeem miles: " + pathForMiles);
  37. else
  38. System.out.println("Cannot convert miles from " + start + " to " + goal + ".");
  39. }
  40. }
  41. private static boolean canRedeem(String current, String goal,
  42. ArrayList<String> pathForMiles, ArrayList<String> airlinesVisited,
  43. ArrayList<Airline> network){
  44. if(current.equals(goal)){
  45. //base case 1, I have found a path!
  46. pathForMiles.add(current);
  47. return true;
  48. }
  49. else if(airlinesVisited.contains(current))
  50. // base case 2, I have already been here
  51. // don't go into a cycle
  52. return false;
  53. else{
  54. // I have not been here and it isn't
  55. // the goal so check its partners
  56. // now I have been here
  57. airlinesVisited.add(current);
  58. // add this to the path
  59. pathForMiles.add(current);
  60. // find this airline in the network
  61. int pos = -1;
  62. int index = 0;
  63. while(pos == -1 && index < network.size()){
  64. if(network.get(index).getName().equals(current))
  65. pos = index;
  66. index++;
  67. }
  68. //if not in the network, no partners
  69. if( pos == - 1)
  70. return false;
  71. // loop through partners
  72. index = 0;
  73. String[] partners = network.get(pos).getPartners();
  74. boolean foundPath = false;
  75. while( !foundPath && index < partners.length){
  76. foundPath = canRedeem(partners[index], goal, pathForMiles, airlinesVisited, network);
  77. index++;
  78. }
  79. if( !foundPath )
  80. pathForMiles.remove( pathForMiles.size() - 1);
  81. return foundPath;
  82. }
  83. }
  84. private static class Airline{
  85. private String name;
  86. private ArrayList<String> partners;
  87. //pre: data != null, data.length > 0
  88. public Airline(String[] data){
  89. assert data != null && data.length > 0 : "Failed precondition";
  90. name = data[0];
  91. partners = new ArrayList<String>();
  92. for(int i = 1; i < data.length; i++)
  93. partners.add( data[i] );
  94. }
  95. public String[] getPartners(){
  96. return partners.toArray(new String[partners.size()]);
  97. }
  98. public boolean isPartner(String name){
  99. return partners.contains(name);
  100. }
  101. public String getName(){
  102. return name;
  103. }
  104. public String toString(){
  105. return name + ", partners: " + partners;
  106. }
  107. }
  108. }