willieshi232

Untitled

Apr 10th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 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(inputfileM.hasNextLine())
  31. {
  32. String line = inputfileM.nextLine();
  33. Scanner linescan = new Scanner(line);
  34. String name = linescan.next();
  35. namesM.add(name);
  36. while (linescan.hasNextInt())
  37. {
  38. int preforder = linescan.nextInt();
  39. menPreftemp.add(preforder);
  40. }
  41. men.put(name, menPreftemp);
  42. menPreftemp.clear();
  43. }
  44. while(inputfileW.hasNextLine())
  45. {
  46. String line = inputfileW.nextLine();
  47. Scanner linescan2 = new Scanner(line);
  48. String name = linescan2.next();
  49. namesW.add(name);
  50. while (linescan2.hasNextInt())
  51. {
  52. int preforder2 = linescan2.nextInt();
  53. womanPreftemp.add(preforder2);
  54. }
  55. women.put(name, menPreftemp);
  56. womanPreftemp.clear();
  57. }
  58. for(int i = 0; i < namesM.size(); i++)
  59. {
  60. String tempman = namesM.get(i);
  61. menPreftemp = men.get(tempman);
  62. boolean test = true;
  63. while(test = true) {
  64. int x = 0;
  65. if (women.containsKey(namesW.get(menPreftemp.get(x))))
  66. {
  67. if (women.get(namesW.get(menPreftemp.get(x))) != null)
  68. {
  69. menengage.put(tempman, namesW.get(menPreftemp.get(x)));
  70. womenengage.put(namesW.get(menPreftemp.get(x)), tempman);
  71. women.remove(namesW.get(menPreftemp.get(x)));
  72. test = false;
  73. } else if (women.get(namesW.get(menPreftemp.get(x))) == null)
  74. {
  75. x++;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment