Hi, I'm trying to use a map in my application. the class MovieRating which contains the map is as 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("G"));
        if (age < mAge )
        {
            return false;            
        }
        else if (age >= mAge)
        {
            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();
    }
    
}

I now want to use the three ratings and age in the map in a method in a different class to determine whether the various individuals of various age groups can watch a certain movie or not. the method is called getMovieGoers and is as below: NOTE: This method is in a different class called Family within the same package. It returns a collection of moviegoers from the arraylist.

public void getMovieGoers(MovieRating mRt)
     {
          Adult a = new Adult("Robert",33);
          Adult b = new Adult("Sue",27);
          Adult c = new Adult("Tracy",20);
          ArrayList ads  = new ArrayList();
          ads.add(a); ads.add(b);ads.add(c);
          Collection myAdults =ads;
         if(mRt.getRating()!=null)
         {
             try
             {
                 if(ourChild.getAge()< mRt.getAdult() || ourChild.getAge()<mRt.getPGuidance() )
                 {
                     System.out.println(ourChild.getName()+" cannot watch movie rated A, but can watch"+
                             " movie rated PG under parental advice. The following adults can watch: "+
                             ""+myAdults+"");
                 }
                 else if(ourChild.getAge()>= mRt.getPGuidance() && ourChild.getAge()<mRt.getAdult())
                 {
                     System.out.println(ourChild.getName()+" can freely watch the movie rated PG."+
                             " but cannot watch movies rated A. The following can watch both: "+myAdults+"");
                 }
                 else
                 {
                     System.out.println("The following can watch movies rated G: "+ourChild.getName()+
                             "," +myAdults+"");
                 }
             
             }
             catch(Exception e)
             {
                System.out.println("Movie rating not found."); 
             }
             
         }
    }

When I run my application and call the method in the main, it does not list the moviegoers and I suspect the problem is picking the rating and age pair from the map in the MovieRating class. Some help please.....

Recommended Answers

All 6 Replies

well... you're code is quite unreadable. please try using code tags, it 'll make it a lot easier.
you also go through a lot of effort, using HashMaps while simple int variables would suffice, but I guess that's up to you to choose.
lastly, in the method you want us to check, you use the variable ourChild, while you don't show a declaration or set a value to it in the code you show, so please show the entire code, so we know what we're dealing with.
also, don't it's no use creating the Collection myAdults. you already have that Collection, called ads

well... you're code is quite unreadable. please try using code tags, it 'll make it a lot easier.
you also go through a lot of effort, using HashMaps while simple int variables would suffice, but I guess that's up to you to choose.
lastly, in the method you want us to check, you use the variable ourChild, while you don't show a declaration or set a value to it in the code you show, so please show the entire code, so we know what we're dealing with.
also, don't it's no use creating the Collection myAdults. you already have that Collection, called ads

MovieRating class

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();
    }
}

Child class

class Child extends Human
{
    int childAge;
   
   
    public Child(String name, int age)
    {
        super(name); 
        this.age=age;
    }

    public boolean isObjectEmpty(Object obj)
    {
        boolean result = obj==null;
        return result;
    }
    
    @Override
    public void setAge(int age)
    {
       childAge = age;
    }
    @Override
    public int getAge()	
    { 
        return this.age;
    }
    public String getName()
    {
        return this.name;
    }
    public boolean canWatchMovie(MovieRating mRt) 
    {
       return mRt.getStatus(this.mrating , this.age);
    }
       
    public static void main(String args[])
    {
        Child ch = new Child("Joe",7);      
    }      
}

Family class

import java.util.*;
import java.io.*;
public class Family 
{
    private Dog ourDog;
    private Cats ourCat;
    private Rabbit ourRabbit;
    private Adult adults;

    String me,myPatner;
    private Child ourChild;
    int Age;
   
    public Family(String myName, String patner)
    {
        /*
       * uses 'this' to refer to
       * the current object being manipulated
       */
        this.me =myName;
        this.myPatner=patner;
        adults = new Adult("Robert",26);
        ourChild=new Child("Joe",7);
        ourDog=new Dog("");
        ourCat=new Cats("");
        ourRabbit = new Rabbit("");
               
    }

     public void getMovieGoers(MovieRating mRt)
     {
          Adult a = new Adult("Robert",33);
          Adult b = new Adult("Sue",27);
          Adult c = new Adult("Tracy",20);
          ArrayList ads  = new ArrayList();
          ads.add(a); ads.add(b);ads.add(c);
          Collection myAdults =ads;
         if(mRt.getRating()!=null)
         {
             try
             {
                 if(ourChild.getAge()< mRt.getAdult() || ourChild.getAge()<mRt.getPGuidance() )
                 {
                     System.out.println(ourChild.getName()+" cannot watch movie rated A, but can watch"+
                             " movie rated PG under parental advice. The following adults can watch: "+
                             ""+myAdults+"");
                 }
                 else if(ourChild.getAge()>= mRt.getPGuidance() && ourChild.getAge()<mRt.getAdult())
                 {
                     System.out.println(ourChild.getName()+" can freely watch the movie rated PG."+
                             " but cannot watch movies rated A. The following can watch both: "+myAdults+"");
                 }
                 else
                 {
                     System.out.println("The following can watch movies rated G: "+ourChild.getName()+
                             "," +myAdults+"");
                 }
             
             }
             catch(Exception e)
             {
                System.out.println("Movie rating not found."); 
             }
             
         }
    }

    public static void main(String args[])throws IOException
    {
        Adult ad = new Adult("Robert",26);
        MovieRating mRt = new MovieRating();
         /*
         * new instance of the Family
         */
        Family fam= new Family("Robert","Mrembo");
        
        /*
         * set string values to variables
         */
        fam.ourCat.catType("Angora");
        fam.ourCat.setCatColor("Brownish white");
        fam.ourDog.dogType("African Shepherd Dog");
        fam.ourDog.setDogColor("Black");
        fam.ourRabbit.setRabbitColor("White");
        fam.ourRabbit.rabbitType("Akorino");

        System.out.println("Hi, I'm "+ad.getName()+" and my partner is Mrs Robert" +
              "We own the following pets: " +
        " " + fam.ourDog.MydogType+ " " + fam.ourDog.dog_color +  " in color;" +
        " a small " + fam.ourCat.cat_color + " " +fam.ourCat.MyCatType+ " cat;"+
        " and a "+ fam.ourRabbit.rabbit_color +" "+ fam.ourRabbit.MyRabbitType +
                                     " rabbit.");
        fam.getMovieGoers(mRt);
    }
}

NOTE: Don't worry about the Cat, Dog and Rabbit classes. They are not included here.


Adult class

class Adult extends Human
{
    String movieRating,aName,pName;
    int mAge;
    public Adult(String name, int age)
    {
        super(name);
        this.age=age;
    }
    protected String getName()
    {
       return this.name;
    }
    @Override
    public int getAge()
    {
        return this.age;
    }
    protected void getPatnerName(String partnerName)
    {
       pName = partnerName;
    }  
   
    @Override
    public boolean canWatchMovie(MovieRating mRt) 
    {
        return true;
    }
    public static void main(String args[])
    {
        Adult me = new Adult("Robert",26);
        System.out.println("I'm " + me.getName() + " and i'm "+ me.getAge() + " years old" +
            " that means I'm an Adult");
    }

    
}

My main problem is the method getMovieGoers(MovieRating mRt) in Family class. and how to use rating as the key and minimum age as the value from the Map in this method to get movie goers for each rating.

I think you'd do better to first learn the basic logic of OO programming.
you're Family class, for instance:

you'd have a Family with just one adult, with (max) one child, ...
next to that, the names 'me' and 'myName' are a bit ... confusing.
or, of course, you want to be able to create just one family (yours), or you want to be part of every family.

in a better implementation, a more OO approach, your Family.java should look more like this:

public class Family{
    
     private Adult father;
     private Adult mother;

     private Child[] children;

     public Family(Adult father, Adult mother, Children[] children){
       setFather(father);
       setMother(mother);
       setChildren(children);
     }
  
     public Adult getFather(){ return this.father;}
     public Adult getMother(){ return this.mother;}
     public Child[] getChildren(){ return this.children;}

     public void setFather(Adult father){ this.father = father;}
     public void setMother(Adult mother) { this.mother = mother;}
     public void setChildren(Child[] children){ this.children = children;}

  }

this is about all there has to be in it. about the watchin movies or not, that does not belong in family, that should be in the class that uses family

well there's a getMovieGoers method in the Family class that takes a rating as a parameter and returns a collection of all the family members that can watch the movie. This method is to be called in the main method of family.

The rating is to be picked from the Map in MovieRating class, because there are some movies which can oly be watched by Adults or Children of certain age. I hope it's not confusing.

well there's a getMovieGoers method in the Family class that takes a rating as a parameter and returns a collection of all the family members that can watch the movie. This method is to be called in the main method of family.

well, than you're better of with an approach like this:

...
putlic static void main(String args[]){
  Movie movie = new Movie();
  Children[] children = {new Child("Frank",10), new Child("Lisa",18)};
  Family one = new Family(new Adult("John",37), new Adult("Mary",35),
  ArrayList visitors = (ArrayList)one.getMovieGoers(movie.getRating());

}

and in the actual Family class:

public Collection getMovieGoers(int rating){
  // do not go and create Adults or children here, use your class var's
  Collection returnValue = new Collection();
  // I consider rating here to be the starting age that is allowed for the movie
  if ( father.getAge() >= rating)
     returnValue.add(father);
  if ( mother.getAge() >= rating)
     returnValue.add(mother);
  for(int i = 0; i < children.length; i++){
    if (children[i].getAge() >= rating)
      returnValue.add(children[i]);
  }
  return returnValue;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.