Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by WillieShi on 4/4/2016.
- */
- import java.util.*;
- import java.io.*;
- public class MarriageProblem
- {
- public static void main(String [] args)
- throws FileNotFoundException
- {
- //List of Women and Men's marriage choices in order
- ArrayList<Integer> menPreftemp = new ArrayList<>();
- ArrayList<Integer> womanPreftemp = new ArrayList<>();
- //List of Women and Men names with numbers assigned
- ArrayList<String> namesM = new ArrayList<>();
- ArrayList<String> namesW = new ArrayList<>();
- //Tree map connecting person objects to their respective list
- Map<String, ArrayList<Integer>> men = new TreeMap<>();
- Map<String, ArrayList<Integer>>women = new TreeMap<>();
- Scanner inputfileM = new Scanner(new File("C:\\Users\\WillieShi\\IdeaProjects\\Marriage Problem\\src\\PreferenceListGuys"));
- Scanner inputfileW = new Scanner(new File("C:\\Users\\WillieShi\\IdeaProjects\\Marriage Problem\\src\\PreferenceListGirls"));
- //Tree map that shows the engaged path of each man or the engaged path of each women
- Map<String, String> menengage = new TreeMap<>();
- Map<String, String> womenengage = new TreeMap<>();
- //Reading the text file and filling the the tree with the name with their respective list
- while(inputfileW.hasNextLine())
- {
- String line2 = inputfileW.nextLine();
- Scanner linescan2 = new Scanner(line2);
- String name2 = linescan2.next();
- namesW.add(name2);
- while (linescan2.hasNextInt())
- {
- int preforder2 = linescan2.nextInt();
- womanPreftemp.add(preforder2);
- }
- women.put(name2, womanPreftemp);
- womanPreftemp.clear();
- }
- while(inputfileM.hasNextLine())
- {
- String line = inputfileM.nextLine();
- Scanner linescan = new Scanner(line);
- String name = linescan.next();
- namesM.add(name);
- while (linescan.hasNextInt())
- {
- int preforder = linescan.nextInt();
- menPreftemp.add(preforder);
- }
- men.put(name, menPreftemp);
- menPreftemp.clear();
- }
- //Matchmaking algorithm
- for(int i = 0; i < namesM.size(); i++)
- {
- String tempman = namesM.get(i);
- menPreftemp = men.get(tempman);
- boolean test = true;
- while(test = true) {
- int x = 0;
- if (women.containsKey(namesW.get(menPreftemp.get(x))))
- {
- if (women.get(namesW.get(menPreftemp.get(x))) != null)
- {
- menengage.put(tempman, namesW.get(menPreftemp.get(x)));
- womenengage.put(namesW.get(menPreftemp.get(x)), tempman);
- women.remove(namesW.get(menPreftemp.get(x)));
- test = false;
- } else if (women.get(namesW.get(menPreftemp.get(x))) == null)
- {
- x++;
- }
- }
- }
- }
- //Prints the final marriage path
- for (Map.Entry<String, String> entry : menengage.entrySet()) {
- System.out.println("Key: " + entry.getKey() + ". Value: " + entry.getValue());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement