Hi, I have a class MovieRating below:

import java.util.*;

public class MovieRating 
{
    private Map ageMap = new HashMap();
    private String movieRating;
    public MovieRating()
    {  
        ageMap.put("PG", 18); //parental Guidance
        ageMap.put("G", 4);  //general viewing
        ageMap.put("A", 27);  //Adult material
    }
    public String getRating() 
    {
        return movieRating;
    }

    public boolean getStatus(String rating, int age)
    {
        int mAge,gAge,aAge;
        mAge = Integer.getInteger((String) ageMap.get("PG"));
        gAge = Integer.getInteger((String) ageMap.get("G"));
        aAge = Integer.getInteger((String) ageMap.get("A"));
        if (age < mAge )
        {
            return false;            
        }
        else if (age >= mAge)
        {
            return true;
        } 
        else if(age<aAge)
        {
            return false;                  
        }
        else if(age>=aAge)
        {
            return true;
        }
        else if(age<gAge)
        {
            return false;
        }
        else if(age>=gAge)
        {
            return true;
        }        
       return false;
   }
    public int getPGuidance() 
    {
         // Retrieve the minimum age allowed to watch movie rated PG.
         return (Integer)ageMap.get("PG");
    }
    public int getGeneral() 
    {
         // Retrieve the age allowed to watch movie rated G.
         return (Integer)ageMap.get("G");
    }
    public int getAdult() 
    {
         // Retrieve the age allowed to watch movie rated Adult.
         return (Integer)ageMap.get("A");
    }
    
    public Object getValue()
    {
//        return ageMap.values();
        return ageMap.toString();
    }
}

In the public boolean getStatus(String rating, int age) method, i would like the method to get the movie rating and minimum age allowed to watch the movie, then compare with the age of the person to determine whether to return true or false. Can someone modify for me the getStatus() method.

you need to check the current rating, not all the ratings

public boolean getStatus(String rating, int age) {

int minAge = 0;

if(rating == "PG"){
	minAge = getPGuidance();
}
else if (rating == "G"){
	minAge = getGeneral();
}
else if (rating == "A"){
	minAge = getAdult();
}

if(age >= minAge){
	return true
}
else {
	return false;
}
}

and remember that if you want to compare String Objects, you'll need to use equals or equalsIgnoreCase instead of '=='

if(rating == "PG"){
	minAge = getPGuidance();
}

should be

if(rating.equalsIgnoreCase("pg")){
  minAge = getPGuidance();
}
This article has been dead for over six months. Start a new discussion instead.