Sunteți pe pagina 1din 13

Nick Gabel

Linear Data Structures


10/7/2014
Project 2

Linear Data Structures: Project 2


Asking for User input:

Main Window:

Males Choice:

Females Choice:

CODE LISTING:
/* Author: Nick Gabel
* This Class calls the Gui class for Stable Matching
*/
import java.io.FileNotFoundException;
public class StableMatchingCaller {
public static void main(String args[]) throws FileNotFoundException{
StableMatchingGui mainGUI = new StableMatchingGui();
}
}

/* Author: Nick Gabel


* This is the Man class to be used in Stable Matching
*/
public class Man {
String[] preferences;
boolean isEngaged;
String name;
int
fianceeIndex;
public Man(String name){
this.name = name;
}
public Man(String name, boolean isEngaged){
this.name
= name;
this.isEngaged = isEngaged;
}
public Man( String name,boolean isEngaged,
int fianceeIndex) {
this.isEngaged = isEngaged;
this.name
= name;
this.fianceeIndex = fianceeIndex;
}
public int getFianceeIndex() {
return fianceeIndex;
}
public void setFianceeIndex(int fianceeIndex) {
this.fianceeIndex = fianceeIndex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isEngaged() {
return isEngaged;
}
public void setEngaged(boolean isEngaged) {
this.isEngaged = isEngaged;
}
public void buildPreferencesArray(int nPeople){
preferences = new String[nPeople];
}
}

/* Author: Nick Gabel


* This is the Woman class to be used in Stable Matching
*/
public class Woman {
String[] preferences;
boolean isEngaged;
String name;
int
fianceeIndex;
public Woman(String name) {
this.name = name;
}
public Woman(String name, boolean isEngaged){
this.name
= name;
this.isEngaged = isEngaged;
}
public Woman(String name, boolean isEngaged,
int fianceeIndex) {
this.isEngaged = isEngaged;
this.name
= name;
this.fianceeIndex = fianceeIndex;
}
public boolean isEngaged() {
return isEngaged;
}
public void setEngaged(boolean isEngaged) {
this.isEngaged = isEngaged;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getFianceeIndex() {
return fianceeIndex;
}
public void setFianceeIndex(int fianceeIndex) {
this.fianceeIndex = fianceeIndex;
}
public void buildPreferencesArray(int nPeople){
preferences = new String[nPeople];
}
}

/* Author: Nick Gabel


* This program utilizes the Gayle-Shapley algorithm
* using two distinct sets of individuals with specific
* preferences on choosing a mate. This program was
* written to accommodate any number of individuals, so
* long as there the same number of men and women.
*/
import java.io.*;
import java.util.Scanner;
public class StableMatchingEngine {
static int numPeople;
static
Man[] menArray;
static
Woman[] womenArray;
Boolean areAllEngaged;
public static void matchAll(char gender1){
if(gender1 == 'm'){
while(!areAllEngaged(gender1)){
for(int i = 0; i < numPeople ; i++ ){
matchSingleMan(menArray[i].getName());
}
}
}
if(gender1 == 'f'){
while(!areAllEngaged(gender1)){
for(int i = 0; i < numPeople ; i++){
matchSingleWoman(womenArray[i].getName());
}
}
}
}
public static String[] getMalePrefList(){
int numCompleted;
numCompleted = 0;
String[] prefList;
prefList = new String[numPeople*(numPeople+1)];
for(int i = 0 ; i < numPeople ; i++ ){
prefList[prefList.length/numPeople*numCompleted] = menArray[i].getName() +
's Preferences:";
for(int j = 1 ; j < numPeople+1 ; j++){
prefList[prefList.length/numPeople*numCompleted+j]
= menArray[i].preferences[j-1];
}
numCompleted++;
}
return prefList;
}

public static String[] getFemalePrefList(){


int
numCompleted;
String[] prefList;
numCompleted = 0;
prefList = new String[numPeople*(numPeople+1)];
for(int i=0;i<numPeople;i++){
prefList[prefList.length/numPeople*numCompleted] = womenArray[i].getName() +
"'s Preferences:";
for(int j=1; j<numPeople+1;j++){
prefList[prefList.length/numPeople*numCompleted+j] =
womenArray[i].preferences[j-1];
}
numCompleted++;
}
return prefList;
}
public static String getCouples(){
String couples;
couples = "";
for(int i = 0; i < numPeople; i++){
String man;
String woman;
man
= menArray[i].getName();
woman = womenArray[menArray[i].getFianceeIndex()].getName();
couples += man + " is married to " + woman + "\n";

}
return couples;

public static void matchSingleMan(String m){


//Takes name of man and applies Gayle-Shapley match
Woman candidate;
int formerFianceeIndex;
int currIndex;
currIndex = getMaleIndex(m);
while(!menArray[currIndex].isEngaged()){
preferenceLoop:
for(int i = 0; i < numPeople ; i++){
candidate =
womenArray[getFemaleIndex(menArray[currIndex].preferences[i])];
if(!candidate.isEngaged()){
engage(m,candidate.name);
break preferenceLoop;
}
if(currIndex<candidate.getFianceeIndex()){
formerFianceeIndex = candidate.getFianceeIndex();
menArray[formerFianceeIndex].setEngaged(false);
menArray[formerFianceeIndex].setFianceeIndex(numPeople+1);
engage(m,candidate.name);
break preferenceLoop;
}else
continue;
}
}
}

public static void matchSingleWoman(String m){


//Takes name of woman and applies Gayle-Shapley match
Man candidate;
int formerFianceeIndex;
int currIndex;

currIndex = getFemaleIndex(m);
while(!womenArray[currIndex].isEngaged()){
preferenceLoop:
for(int i = 0 ; i < numPeople ; i++){
candidate =
menArray[getMaleIndex(womenArray[currIndex].preferences[i])];
if(!candidate.isEngaged()){
engage(candidate.name,m);
break preferenceLoop;
}
if(currIndex < candidate.getFianceeIndex()){
formerFianceeIndex = candidate.getFianceeIndex();
womenArray[formerFianceeIndex].setEngaged(false);
womenArray[formerFianceeIndex].setFianceeIndex(numPeople+1);
engage(candidate.name,m);
break preferenceLoop;
}else
continue;
}
}

public static void engage(String man, String woman){


menArray[getMaleIndex(man)].setEngaged(true);
menArray[getMaleIndex(man)].setFianceeIndex(getFemaleIndex(woman));
womenArray[getFemaleIndex(woman)].setEngaged(true);
womenArray[getFemaleIndex(woman)].setFianceeIndex(getMaleIndex(man));
}
public static boolean areAllEngaged(char c){
int numEngaged;
switch (c){
default:
System.out.println("Default switch, something went wrong");
return false;
case'm':
numEngaged=0;
for(int i = 0 ; i < numPeople ; i++){
if(menArray[i].isEngaged())
numEngaged++;
else
continue;
}
if(numEngaged == numPeople)
return true;
else
return false;

case 'f':
numEngaged=0;
for(int i = 0 ; i < numPeople ; i++){
if(womenArray[i].isEngaged())
numEngaged++;
else
continue;
}
if(numEngaged==numPeople)
return true;
else
return false;
}
}
public static int getMaleIndex(String targetName){
int index;
index = 0;
for(int i = 0; i < numPeople ; i++){
if(menArray[i].getName().equalsIgnoreCase(targetName)){
index = i;
return index;
}
else
continue;
}
return index;
}
public static int getFemaleIndex(String targetName){
int index;

index = 0;
for(int i = 0 ; i < numPeople ; i++){
if(womenArray[i].getName().equalsIgnoreCase(targetName)){
index = i;
return index;
}
else continue;
}
return index;

public static String[] getMaleNames(){


String[] names = new String[numPeople];
for(int i = 0 ; i < numPeople ; i++){
names[i] = menArray[i].getName();
}
return names;
}
public static String[] getFemaleNames(){
String[] names = new String[numPeople];
for(int i = 0 ; i < numPeople ; i++){
names[i] = womenArray[i].getName();
}
return names;
}

public static void resetEngagements(){


for(int i = 0 ; i < numPeople ; i++){
menArray[i].setEngaged(false);
menArray[i].setFianceeIndex(numPeople+1);
womenArray[i].setEngaged(false);
womenArray[i].setFianceeIndex(numPeople+1);
}
}
public static void initiateData(String fileName) throws FileNotFoundException {
//Creates arrays of men and women
File preferences;
Scanner fileScanner;
preferences = new File(fileName);
fileScanner = new Scanner(preferences);
while(fileScanner.hasNext()){
numPeople
= fileScanner.nextInt();
menArray
= new Man[numPeople];
womenArray
= new Woman[numPeople];
System.out.println(numPeople);
for(int i = 0 ; i < numPeople ; i++){
menArray[i] = new Man(fileScanner.next(),false,numPeople+1);
}
for(int i = 0 ; i < numPeople; i++){
womenArray[i] = new Woman(fileScanner.next(),false,numPeople+1);
}

}
}
}

for(int i = 0 ; i < numPeople; i++){


menArray[i].buildPreferencesArray(numPeople);
menArray[i].setName(fileScanner.next());
for(int j = 0 ; j < numPeople ; j++){
menArray[i].preferences[j] = fileScanner.next();
}
}
for(int i = 0 ; i < numPeople ; i++){
womenArray[i].buildPreferencesArray(numPeople);
womenArray[i].setName(fileScanner.next());
for(int j = 0 ; j < numPeople ; j++){
womenArray[i].preferences[j] = fileScanner.next();
}
}

/* Author: Nick Gabel


* This is the GUI class to be used in Stable Matching
*/
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class StableMatchingGui extends JFrame{
//Instance Variables
String
fileName
= new String();
JFrame
mainFrame
= new JFrame();
JRadioButton maleB
= new JRadioButton("Males Propose");
JRadioButton femaleB
= new JRadioButton("Females Propose");
JButton
match
= new JButton("Enforce Marriages");
ButtonGroup genderBG
= new ButtonGroup();
JPanel
NContainer = new JPanel();
JPanel
SContainer = new JPanel();
JPanel
EContainer = new JPanel();
JPanel
WContainer = new JPanel();
JPanel
CContainer = new JPanel();
JLabel
malePrefs
= new JLabel("Male Preferences");
JLabel
femalePrefs = new JLabel("Female Preferences");
JLabel
males
= new JLabel("Males");
JLabel
females
= new JLabel("Females");
JLabel
numPeople;
JList
maleList;
JList
femaleList;
JList
malePrefJList;
JList
femalePrefJList;
JScrollPane
maleListScroller;
JScrollPane
femaleListScroller;
JScrollPane
malePrefJListScroller;
JScrollPane
femalePrefJListScroller;
String[]
maleNames;
String[]
femaleNames;
String[]
malePrefList;
String[]
femalePrefList;
JTable
jTableMale;
JTable
jTableFemale;
JScrollPane
maleTablePane;
JScrollPane
femaleTablePane;
JLabel
numPeopleLabel;
DefaultTableModel westMaleModel;
DefaultTableModel eastFemaleModel;

StableMatchingGui() throws FileNotFoundException{


fileName = JOptionPane.showInputDialog("Please enter a filename");
StableMatchingEngine.initiateData(fileName);
numPeopleLabel = new JLabel(String.valueOf(StableMatchingEngine.numPeople));

//South Container
numPeople = new JLabel("There are "
+ StableMatchingEngine.numPeople*2 + " people.");
SContainer.setLayout(new FlowLayout());
SContainer.add(numPeople);
genderBG.add(maleB);
genderBG.add(femaleB);
SContainer.add(maleB);
SContainer.add(femaleB);
SContainer.add(match);
//North Container
NContainer.setLayout(new FlowLayout());
maleNames = StableMatchingEngine.getMaleNames();
femaleNames = StableMatchingEngine.getFemaleNames();
femaleList = new JList(femaleNames);
maleList = new JList(maleNames);
maleList.setLayoutOrientation(JList.VERTICAL);
femaleList.setLayoutOrientation(JList.VERTICAL);
maleList.setVisibleRowCount(-1);
femaleList.setVisibleRowCount(-1);
maleListScroller = new JScrollPane(maleList);
femaleListScroller = new JScrollPane(femaleList);
maleListScroller.setPreferredSize(new Dimension(200,100));
femaleListScroller.setPreferredSize(new Dimension(200,100));
NContainer.add(maleListScroller);
NContainer.add(femaleListScroller);
//Center Container
CContainer.setLayout(new FlowLayout());
malePrefList
= StableMatchingEngine.getMalePrefList();
malePrefJList
= new JList(malePrefList);
malePrefJListScroller = new JScrollPane(malePrefJList);
femalePrefList
= StableMatchingEngine.getFemalePrefList();
femalePrefJList
= new JList(femalePrefList);
femalePrefJListScroller = new JScrollPane(femalePrefJList);
malePrefJList.setLayoutOrientation(JList.VERTICAL);
femalePrefJList.setLayoutOrientation(JList.VERTICAL);
malePrefJList.setVisibleRowCount(-1);
femalePrefJList.setVisibleRowCount(-1);
malePrefJListScroller.setPreferredSize(new Dimension (200,100));
femalePrefJListScroller.setPreferredSize(new Dimension (200,100));
CContainer.add(malePrefJListScroller);
CContainer.add(femalePrefJListScroller);
//Main Frame
mainFrame.setTitle("Engineer's Guide to Choosing a Spouse");
mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
mainFrame.setSize(640,320);
mainFrame.setLocationRelativeTo(null);
mainFrame.setLayout(new BorderLayout());
mainFrame.add(NContainer,BorderLayout.NORTH);
mainFrame.add(EContainer,BorderLayout.EAST);
mainFrame.add(WContainer,BorderLayout.WEST);
mainFrame.add(SContainer, BorderLayout.SOUTH);
mainFrame.add(CContainer, BorderLayout.CENTER);
mainFrame.setVisible(true);

match.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(maleB.isSelected()){
StableMatchingEngine.resetEngagements();
StableMatchingEngine.matchAll('m');
JOptionPane.showMessageDialog(mainFrame,
StableMatchingEngine.getCouples());
}
if(femaleB.isSelected()){
StableMatchingEngine.resetEngagements();
StableMatchingEngine.matchAll('f');
JOptionPane.showMessageDialog(mainFrame,
StableMatchingEngine.getCouples());

}
}
}

});

}else if(!maleB.isSelected() && !femaleB.isSelected()){


JOptionPane.showMessageDialog(mainFrame,"You must select a +
gender to match.");
}

S-ar putea să vă placă și