// CSE 143, Winter 2009, Marty Stepp // Homework 3: Stable Marriage // // This program runs the stable marriage simulation and prints the results. // It reads and sets up the input data for all the men and women that will // be passed in to your MatchMaker object. import java.io.*; import java.util.*; public class StableMarriageMain { public static void main(String[] args) throws FileNotFoundException { // prompt for input file name Scanner console = new Scanner(System.in); System.out.println("Welcome to the CSE 143 Stable Marriage simulator!"); System.out.print("Input file name? "); String filename = console.nextLine(); // read data from input file Scanner input = new Scanner(new File(filename)); Set men = new TreeSet(); Set women = new TreeSet(); readInput(input, men, women); // prompt for rounds System.out.print("How many rounds (0 to repeat until stable)? "); int rounds = console.nextInt(); System.out.println(); // do match-making and print results MatchMaker maker = new MatchMaker(men, women); while (true) { if (rounds == 0) { if (maker.isStable()) { break; } } else if (maker.getRound() >= rounds) { break; } maker.nextMatchRound(); System.out.println("Round " + maker.getRound()); System.out.println(maker); } if (maker.isStable()) { System.out.println(maker.getRound() + " rounds performed and stable state reached."); } else { System.out.println(maker.getRound() + " rounds performed but none were stable."); } } // Reads the input from the given file and puts it into the two given sets. public static void readInput(Scanner input, Set men, Set women) { Set listToUse = men; while (input.hasNextLine()) { // read a person's info String line = input.nextLine(); if (line.startsWith("#")) { // this is a comment line in the file; ignore it } else if (line.length() == 0) { listToUse = women; } else { Scanner lineScan = new Scanner(line); lineScan.useDelimiter("[:,]"); String name = lineScan.next(); Person person = new Person(name); while (lineScan.hasNext()) { String partner = lineScan.next(); person.getPreferences().add(partner); person.getRankings().put(partner, person.getPreferences().size()); } listToUse.add(person); } } // let's verify that the file has valid data for (Person man : men) { verifyPerson(man, women, men, women); } for (Person woman : women) { verifyPerson(woman, men, men, women); } } // makes sure this person's data from the file is mostly valid private static void verifyPerson(Person person, Set oppositeSex, Set men, Set women) { // make sure this person has some ranking for each member of opposite sex for (Person mate : oppositeSex) { if (!person.getRankings().containsKey(mate.getName())) { throw new RuntimeException(person.toStringLong() + " does not contain a ranking for " + mate.getName() + "\nmen : " + men + "\nwomen: " + women); } } // make sure there are not the wrong number of rankings/prefs if (person.getPreferences().size() != oppositeSex.size()) { throw new RuntimeException(person.toStringLong() + " has " + person.getPreferences().size() + " preferences but should have " + oppositeSex.size() + "\nmen : " + men + "\nwomen: " + women); } if (person.getRankings().keySet().size() != oppositeSex.size()) { throw new RuntimeException(person.toStringLong() + " has " + person.getPreferences().size() + " rankings but should have " + oppositeSex.size() + "\nmen : " + men + "\nwomen: " + women); } } }