willieshi232

Untitled

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