Advertisement
willieshi232

Untitled

Apr 12th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. /**
  2. * Created by WillieShi on 4/4/2016.
  3. */
  4. import java.util.*;
  5. import java.io.*;
  6. public class MarriageProblem
  7. {
  8. public static void main(String [] args)
  9. throws FileNotFoundException
  10. {
  11. //List of Women and Men's marriage choices in order
  12. ArrayList<Integer> menPreftemp = new ArrayList<>();
  13. ArrayList<Integer> womanPreftemp = new ArrayList<>();
  14.  
  15. //List of Women and Men names with numbers assigned
  16. ArrayList<String> namesM = new ArrayList<>();
  17. ArrayList<String> namesW = new ArrayList<>();
  18.  
  19. //Tree map connecting person objects to their respective list
  20. Map<String, ArrayList<Integer>> men = new TreeMap<>();
  21. Map<String, ArrayList<Integer>>women = new TreeMap<>();
  22. Scanner inputfileM = new Scanner(new File("C:\\Users\\WillieShi\\IdeaProjects\\Marriage Problem\\src\\PreferenceListGuys"));
  23. Scanner inputfileW = new Scanner(new File("C:\\Users\\WillieShi\\IdeaProjects\\Marriage Problem\\src\\PreferenceListGirls"));
  24.  
  25. //Tree map that shows the engaged path of each man or the engaged path of each women
  26. Map<String, String> menengage = new TreeMap<>();
  27. Map<String, String> womenengage = new TreeMap<>();
  28.  
  29. //Reading the text file and filling the the tree with the name with their respective list
  30. while(inputfileW.hasNextLine())
  31. {
  32. String line2 = inputfileW.nextLine();
  33. Scanner linescan2 = new Scanner(line2);
  34. String name2 = linescan2.next();
  35. namesW.add(name2);
  36. while (linescan2.hasNextInt())
  37. {
  38. int preforder2 = linescan2.nextInt();
  39. womanPreftemp.add(preforder2);
  40. }
  41. women.put(name2, womanPreftemp);
  42. womanPreftemp.clear();
  43. }
  44. while(inputfileM.hasNextLine())
  45. {
  46. String line = inputfileM.nextLine();
  47. Scanner linescan = new Scanner(line);
  48. String name = linescan.next();
  49. namesM.add(name);
  50. while (linescan.hasNextInt())
  51. {
  52. int preforder = linescan.nextInt();
  53. menPreftemp.add(preforder);
  54. }
  55. men.put(name, menPreftemp);
  56. menPreftemp.clear();
  57. }
  58. //Matchmaking algorithm
  59. for(int i = 0; i < namesM.size(); i++)
  60. {
  61. String tempman = namesM.get(i);
  62. menPreftemp = men.get(tempman);
  63. boolean test = true;
  64. while(test = true) {
  65. int x = 0;
  66. if (women.containsKey(namesW.get(menPreftemp.get(x))))
  67. {
  68. if (women.get(namesW.get(menPreftemp.get(x))) != null)
  69. {
  70. menengage.put(tempman, namesW.get(menPreftemp.get(x)));
  71. womenengage.put(namesW.get(menPreftemp.get(x)), tempman);
  72. women.remove(namesW.get(menPreftemp.get(x)));
  73. test = false;
  74. } else if (women.get(namesW.get(menPreftemp.get(x))) == null)
  75. {
  76. x++;
  77. }
  78. }
  79. }
  80. }
  81.  
  82. //Prints the final marriage path
  83. for (Map.Entry<String, String> entry : menengage.entrySet()) {
  84. System.out.println("Key: " + entry.getKey() + ". Value: " + entry.getValue());
  85. }
  86. }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement