Hi, I'm new in java. Can someone help me with this problem. I've attempted it and the code is as shown after the questions:
Question:
1. Create a new Movie class which has two properties, title and rating.
2. Create a new Cinema class, add method addMovieGoers which takes a collection of Humans and another-(Done)- method showMovie which does not take any arguments.(---DONE---) Your Cinema class should also have a setMovie method which takes a Movie as an argument.
3. When the addMovieGoers is called on a cinema instance the movie goers should be added to a private collection.
4. Add a new method to your family class that returns all family members.
5. In the main method of the Cinema class call a new static method that creates a family and a cinema then use the addMovieGoers method of the cinema to add the family members. Next create a new movie and use it to set the movie property of the cinema. Finally call cinema.showMovie.
Now, if you were careful your code may have executed without exception but there are quite a few loopholes in your code, to close them do the following.
1. In the showMovie method test whether or not the movie property has been set, if not throw an exception,<Pending> also add code to empty the Cinema whether the movie was shown or not.
2. In the setMovie method check that the movie title and rating have been set, if not throw an exception.
3. In the addMovieGoers method, check that the movie property has been set and if not throw an exception.
4. Also in the addMovieGoers method check that every member of the collection is a human and that they are old enough to watch the movie, if not throw an exception. Note that although the family has getMovieGoers method, you can't be sure it was used to build the collection.
5. Static main )Finally add another static method to the cinema class that demonstrates the usage of the cinema class. Include multiple scenarios to show how the exceptions work.
6. Remember about creating your own exceptions and passing them to the controlling module (here method with scenarios). After an exception occurs your code should stop execution of current scenario and go to another one.
MY CODE:
My code is as below and it's not working properly, so I need your help please.
//Movie class
public class Movie
{
String movieRating;
public Movie(String rated, String mtitle)
{
this.mrating=rated;
this.title=mtitle;
}
public void setRating(String Rating)
{
movieRating = Rating;
}
// Get the rating
public String getRating()
{
return movieRating;
}
public void setTitle(String title)
{
this.title = title;
}
public String getTitle()
{
return title;
}
@Override
public String toString()
{
return "Movie"
+" title="+getTitle() +
" rating="+getRating();
}
public static void main(String args [])
{
Movie mv= new Movie("","");
mv.toString();
}
private String title;
private String mrating;
}
//Cinema class
import java.util.*;
import java.io.*;
public class Cinema
{
public static final int maxRate = 3;
public static final int minRate = 1;
float cost;
String Title,humans;
ArrayList mGoers;
public Cinema (String mtitle, float pounds)
{
this.Title=mtitle;
this.cost = pounds;
mGoers = new ArrayList();
}
public float getcost (int people)
{
return cost * people;
}
//methods for adding movie goers which takes a collection of Humans
public void addMovieGoers()
{
int minAge=18;
Family fam = new Family("",0);
fam.listAll();
// for (int cnt=0; cnt<mGoers.size(); cnt++)
// {
// Adult stud = (Adult) mGoers.get(cnt);
// System.out.println(stud);
// }
}
/**
* Method check that the movie title and rating have been set,
* if not throw an exception.
*/
public void setMovie(Movie movieNew) throws IOException
{
try
{
if (!isObjectEmpty(movieNew.getTitle()) && !isObjectEmpty(movieNew.getRating()))
{
System.out.println("The movie is called " +movieNew.getTitle()+
" and is rated " +movieNew.getRating()+ ".");
}
else
{
System.out.println("Movie title and rating not set");
}
}
catch(Exception e)
{
System.out.println("Title and Rating not Set");
}
}
public void showMovie()
{
try
{
Movie mv = new Movie("","");
String mRating = mv.getRating();
String mTitle = mv.getTitle();
if((mRating!=null)&& (mTitle!=null))
{
System.out.println("Title is "+mTitle+" and is Rated "
+mRating+"");
}
else
{
System.out.println("Either Title or Rating is missing");
}
}
catch(Exception e)
{
System.out.println("The Movie properties not set");
}
finally
{
System.out.println("The movie is over.");
}
}
/**
* Checks is indiacated Object is empty.
* restrictions for raiting type (rating must be >=1 and <=3)
*/
public boolean isObjectEmpty(Object obj)
{
int rating=1;
boolean result = obj==null;
if((rating<1)||(rating>3))
{
System.out.println("The rating is out of bounds");
}
else
{
System.out.println("Movie properly rated");
}
return result;
}
public static void main(String args[])
{
String temp;
int rate;
System.out.println("What's the Movie rating:");
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
try
{
temp= stdin.readLine();
rate=Integer.parseInt(temp);
if((rate<1)||(rate>3))
{
System.out.println("Rate out of bounds!");
}
else
{
System.out.println("The following can go for a movie:");
Cinema cin = new Cinema("PG",maxRate);
cin.addMovieGoers();
}
}
catch(Exception e)
{
System.out.println("Movie not properly rated.");
}
finally
{
System.out.println("Cinema closed now.");
}
}
}
//Family class
import java.util.*;
import java.io.*;
//import java.util.Iterator;
public class Family extends Human
{
// String me,myPatner,famMembers;
int myAge;
// private Child ourChild;
String rating;
ArrayList familia;
public Family(String name, int Age)
{
this.name=name;
this.age=Age;
// ourChild=new Child("",0);
familia = new ArrayList();
}
// protected String getPName(String pName)
// {
// return famMembers = pName;
//
// }
public String getMovieGoers()
{
return rating;
}
//return all family members
public void listAll()
{
for (int cnt=0; cnt<familia.size(); cnt++)
{
System.out.println(familia.get(cnt));
}
}
public void addHumans(Adult newAdult)
{
try
{
familia.add(newAdult);
}
catch (NullPointerException npe)
{
System.err.println("Error: " + npe);
}
}
public static void main(String args[])
{
Family familia = new Family("",0);
Adult father = new Adult("Russel" , 32);
Adult mother = new Adult("Lisa",28);
// Populate the list using the .addHumans() methods
familia.addHumans(father);
familia.addHumans(mother);
//calls listAll() method to display the family members
familia.listAll();
}
}
//MovieRating class
import java.util.HashMap;
import java.util.Map;
public class MovieRating
{
private Map ageMap = new HashMap();
public MovieRating()
{
ageMap.put("G", new Integer(18));
ageMap.put("PG", new Integer(16));
}
public boolean getStatus(String rating, int age)
{
Integer rateAge = (Integer)ageMap.get(rating);
if(rateAge != null)
{
int minAge = rateAge.intValue();
if (age < minAge )
{
return false;
}
else
{
return true;
}
}
return false;
}
}
I'll really appreciate your help/corrections.