The Matching Game in Java

Posted by Samath
  1281
March 20, 2015

 

In recent times most persons have resorted to finding friends through various social media. It is presumably easier to meet persons in this way than through other means such as hanging out in the city’s hot spots. For two person to be friends they must be compatible. Compatibility is determined by comparing one person’s likes and dislikes against the other person’s likes and dislikes.


How it works
Each person has a list of things he/she likes, and a set of things which he/she absolutely cannot tolerate (i.e. dislikes). On meeting another person (a stranger) he/she finds out about the stranger’s likes and dislikes by asking probing questions.

Example:
“How do you like football?”
>> I like football
“How do you like politics?”
>> I dislike politics

A compatibility scored is calculated using the formula:
compatibility = numOfCommonLikes + numOfCommonDislikes - numOfDisagreements
where:

numOfCommonLikes is the number of things the two persons like in common,
numOfCommonDislikes is a count of things that the two persons dislike in common
numOfDisagreements is a count of the things that one person likes, but the other dislikes

You are required to write a class named Person that represents a person. Among other functions the class shall implement functionality to calculate a compatibility score for two persons.

1. Write the constructor for the Person class. This method shall accept a person’s last name, first
name, and sex (in that order) and shall initialize the corresponding attributes on a newly created
Person object. The parameter types are to be String, String, and char respectively.

2. Write the following getter (accessor) methods:
      a. getLastName – returns the last name as a string.
      b. getFirstName – returns the first name as a string. 
      c. getSex – returns the person’s sex as a character.

3. Write the mutator method addPreference that accepts a parameter of type Preference and an integer value (-1, or 1). The method shall add the preference to the person’s list of dislikes (if the integer value is -1), or the list of likes (if the integer value is 1). No mutation should be done if the preference is already in either the list of likes or the list of dislikes.

4. Write the mutator method removePreference that accepts a parameter of type Preference and removes the indicated preference from any list in which it exists (ie. Like, or dislike).

5. Write the toString method so that it returns a string formatted as below:
First Name:    <person’s first name here>
Last Name:    <person’s last name here?
        LIKES:       <list of likes here>
        DISLIKES: <list of dislikes here>

6. Write the method howDoYouLike that accepts a single parameter of type Preference and returns an integer value (type byte) of 1 If the parameter is in the person’s list of likes, 0 if the parameter is in neither the list of likes nor the list of dislikes, and -1 if the parameter is in the person’s list of dislikes.

7. Write the method compatibility that is to accept a parameter of type Person (let us call this stranger) and return a compatibility score with the stranger (an integer value) as described in the formula above.

Preference Enumeration:

public enum Preference {
                            SPORTS,     // Watching/playing sports
                            MOVIES,     // Going to, or watching movies
                            DANCING,    // Going dancing
                            MUSIC,      // Listening to music
                            READING,    // Reading books
                            VIDEOGAMES, // Playing computer games
                            COMPUTERS,  // Using computers
                            CHURCH     // Going to church
}

Person Class:

import java.util.*;

public class Person 
{
    private String last_name;
    private String first_name;
    private char sex;
    private ArrayList<Preference> p_dislikes = new ArrayList<Preference>();
    private ArrayList<Preference> p_likes = new ArrayList<Preference>();

    
    /**
    * This is the person constructor it is used to initialize the private attributes
    */
    Person(String lname, String fname, char s)
    {
        last_name = lname;
        first_name = fname;
        sex = s;
    }
    
    /**
    * getter that returns the person's last name
    */        
    public String getLastName()
    {
        return last_name;
    }
    
    /**
    * getter that returns the person's first name
    */  
    public String getFirstName()
    {
        return first_name;
    }
    
    /**
    * getter that returns the person's sex
    */ 
    public char getSex()
    {
        return sex;
    }
    
    /**
    * Function that check if a person is compatible with a stranger, its does this 
    * by checking the person's like against the stranger's like and see if they match.
    * It also do the same thing for the dislikes.
    */
    public int compatibility(Person stranger)
    {
        int compatibility = 0;
        int numOfCommonLikes = 0;
        int numOfCommonDislikes = 0;
        int numOfDisagreements = 0;
        
        
        for(Preference pref : p_likes)
        {
            if(stranger.p_likes.contains(pref))
            {
                numOfCommonLikes++;
            }
        }
        
        
        for(Preference pref : p_dislikes)
        {
            if(stranger.p_dislikes.contains(pref))
            {
                numOfCommonDislikes++;
            }
        }
        
        for(Preference pref : p_dislikes)
        {
            if(stranger.p_likes.contains(pref))
            {
                numOfDisagreements++;
            }
        }
        
        
        for(Preference pref : p_likes)
        {
            if(stranger.p_dislikes.contains(pref))
            {
                numOfDisagreements++;
            }
        }
        compatibility = numOfCommonLikes + numOfCommonDislikes - numOfDisagreements;
        
        return compatibility;
    }
    
    /**
    * This function check if a person like a particular thing
    */
    public int howDoYouLike(Preference pref)
    {
        int numResult = Integer.MAX_VALUE;
        boolean findNum = false;
        
                if (p_likes.contains(pref))
                {
                    numResult = 1;
                    findNum = true;
                }      
         
                if (p_dislikes.contains(pref))
                {
                    numResult = -1;
                    findNum = true;
                }       
         
         if(findNum == false)
         {
             numResult = 0;
         }
         
         return numResult;
        
    }
    
    /**
    * return the first name, last name and list of likes and dislikes for a person
    */
    public String toString()
    {
        String result;
        String myDislikes = "";
        String myLikes = "";
        
        for(Preference pref : p_likes)
        {
            myLikes = myLikes + "\t\t\n"+pref.name();
        }
        
        for(Preference pref : p_dislikes)
        {
            myDislikes = myDislikes + "\t\t\n"+pref.name();
        }
        result = "\n\nFirst Name: " + getFirstName() +"\nLast Name: "+getLastName()+"\nLIKES: "+myLikes+"\n\nDISLIKES: "+myDislikes;
        return result;
    }
    
    /**
    * remove a particular Preference from a person's like or dislike list
    */
    public void removePreference(Preference pref)
    {
                if (p_likes.contains(pref))
                {
                    p_likes.remove(pref);
                } 
                else if (p_dislikes.contains(pref))
                {
                    p_dislikes.remove(pref);
                } 
    }
    
     /**
    * add a particular Preference to a person's like or dislike list
    */
    public void addPreference(Preference pref, int likeOrDislike)
    {
        int index = 0;
        Boolean addpref = true;
        
        if(likeOrDislike == 1)
        {
            while (index < p_likes.size())
            {
                if (p_likes.get(index).equals(pref))
                {
                    addpref = false;
                }
                        
                index++;
            }
            
            if(addpref == true)
            {
                p_likes.add(pref);
            }

        }
        else if(likeOrDislike == -1)
        {
            while (index < p_dislikes.size())
            {
                if (p_dislikes.get(index).equals(pref))
                {
                    addpref = false;
                }
                        
                index++;
            }
            
            if(addpref == true)
            {
                p_dislikes.add(pref);
            }
        }
    }
}