Cinema

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2008
Posts: 24
Reputation: Achupa is an unknown quantity at this point 
Solved Threads: 0
Achupa Achupa is offline Offline
Newbie Poster

Cinema

 
0
  #1
Nov 10th, 2008
Hi, I had posted this question before and I did appreciate the response I got. However I still need some little help as:
1.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.
2.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.-you did not include multiple scenarios


How Do I achieve the two conditions above?

the code is as:

//Cinema class
  1. import java.util.*;
  2. public class Cinema
  3. {
  4. float cost;
  5. String Title;
  6.  
  7. //Movie class
  8. private static Movie movieNew = new Movie();
  9. private static ArrayList<Human> hm = new ArrayList();
  10.  
  11. public Cinema()
  12. {
  13.  
  14. }
  15. public Cinema (String mtitle, float pounds)
  16. {
  17. this.Title=mtitle;
  18. this.cost = pounds;
  19. }
  20.  
  21. public float getcost (int people)
  22. {
  23. return cost * people;
  24. }
  25.  
  26. /**
  27.   * Method check that the movie title and rating have been set,
  28.   * if not throw MoviePropertiesNotSetException.
  29.   */
  30. public void setMovie(Movie movieNew) throws MoviePropertiesNotSetException
  31. {
  32. if(movieNew.getTitle()!=null && movieNew.getRating()!=null)
  33. {
  34. System.out.println("The movie is called " +movieNew.getTitle()+
  35. " and is rated " +movieNew.getRating()+ ".");
  36. }
  37. else
  38. {
  39. throw new MoviePropertiesNotSetException("The Movie properties not set");
  40. }
  41. }
  42. /**
  43.   * Method check that the movie title and rating have been set,
  44.   * if not throw MoviePropertiesNotSetException.
  45.   */
  46. public void showMovie() throws MoviePropertiesNotSetException
  47. {
  48. if ((movieNew!=null) && (movieNew.getTitle()!=null) && (movieNew.getRating()!=null))
  49. {
  50. System.out.println("Showing... "+movieNew.getTitle());
  51. }
  52. else
  53. {
  54. throw new MoviePropertiesNotSetException ("The movie properties are not set.");
  55. }
  56. }
  57. /**Checks if Movie properties are set, if not throws MyException
  58.   * takes a collection (ArrayList) of Human - Adult and Child
  59.   * and checks whether they're old enough to watch a movie using
  60.   * the canWatchMovie() method in Adult and Child class.
  61.   */
  62. public void addMovieGoers(ArrayList<Human> hm) throws MoviePropertiesNotSetException,IllegalAgeException
  63. {
  64. if(movieNew.getRating()!=null && movieNew.getTitle()!=null)
  65. {
  66. String mRt = movieNew.getRating();
  67. /**
  68.   * Movie rated PG
  69.   */
  70. if(movieNew.getRating().equalsIgnoreCase("pg"))
  71. {
  72. System.out.println("Movie Goers are::");
  73. for(int i = 0; i < hm.size(); i++)
  74. {
  75. Human test = (Human)hm.get(i);
  76. if ( test.canWatchMovie(mRt))
  77. {
  78. System.out.println(test);
  79. }
  80. else
  81. {
  82. throw new IllegalAgeException("The rest are of Illegal Age!");
  83. }
  84. }
  85. }
  86. /**
  87.   * Movie rated A
  88.   */
  89. else if(movieNew.getRating().equalsIgnoreCase("a"))
  90. {
  91. System.out.println("Movie goers are: ");
  92. for ( int i = 0; i < hm.size(); i++)
  93. {
  94. Human test = (Human)hm.get(i);
  95. if ( test.canWatchMovie(mRt))
  96. {
  97. System.out.println(test);
  98. }
  99. else
  100. {
  101. throw new IllegalAgeException("The rest are of Illegal Age!");
  102. }
  103. }
  104. }
  105. /**
  106.   * Movie rated G
  107.   */
  108. else if(movieNew.getRating().equalsIgnoreCase("g"))
  109. {
  110. System.out.println("Movie goers are: ");
  111. for ( int i = 0; i < hm.size(); i++)
  112. {
  113. Human test = (Human)hm.get(i);
  114. if ( test.canWatchMovie(mRt))
  115. {
  116. System.out.println(test);
  117. }
  118. else
  119. {
  120. System.out.println("The rest are below required age.");
  121. }
  122. }
  123. }
  124. /**
  125.   * Throw Exception should the movie properties be missing
  126.   */
  127. else
  128. {
  129. throw new MoviePropertiesNotSetException("The movie properties are not set");
  130. }
  131. }
  132. }
  133. public static void main(String args[]) throws MoviePropertiesNotSetException, IllegalAgeException
  134. {
  135. Cinema cn = new Cinema();
  136. try
  137. {
  138. Human a = new Adult("Joe",43);
  139. Human b = new Adult("Sue",39);
  140. Human c = new Adult("Tracy",20);
  141. Human d = new Child("Sammy",17);
  142. Human e = new Child("Julie",12);
  143. Human f = new Child("Mona",4);
  144. hm.add(a);hm.add(b);hm.add(c);hm.add(d);
  145. hm.add(e);hm.add(f);
  146. hm.trimToSize();
  147.  
  148. //setting properties to the movie
  149. movieNew.setName("Comedy");
  150. movieNew.setRating("G");
  151. movieNew.setTitle("Mr Bin");
  152.  
  153. cn.setMovie(movieNew);
  154. cn.showMovie();
  155. cn.addMovieGoers(hm);
  156. }
  157. catch(MoviePropertiesNotSetException exp)
  158. {
  159. System.out.println(exp.getMessage());
  160. }
  161. catch(IllegalAgeException msg)
  162. {
  163. System.out.println(msg.getMessage());
  164. }
  165. finally
  166. {
  167. System.out.println("Movie is over! Have a good day.");
  168. movieNew=null;
  169. cn=null;
  170. }
  171. }
  172. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Cinema

 
0
  #2
Nov 10th, 2008
Regarding requirement 1, not too sure exactly what the issue is, and I am not too sure what the family class looks like. Your conditions are also redundant (you are checking the same condition for each case, so you don't really need to divide these out).

Regarding the second requirement, it seems you already have 1 scenario, so just add more.

Hope that helps
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 24
Reputation: Achupa is an unknown quantity at this point 
Solved Threads: 0
Achupa Achupa is offline Offline
Newbie Poster

Re: Cinema

 
0
  #3
Nov 10th, 2008
Regarding the second requirement, it seems you already have 1 scenario, so just add more
How do I add more scenarios? does it mean adding another try catch block?
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Cinema

 
0
  #4
Nov 10th, 2008
Well yes you would probably end up needing another try / catch block. Keep in mind though, a scenario is like a story.

To test some other cases, perhaps have a movie where ALL the humans watch the movie, and one where NONE watch the movie.

Does that make sense?
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 22
Reputation: janamrob is an unknown quantity at this point 
Solved Threads: 1
janamrob janamrob is offline Offline
Newbie Poster

Re: Cinema

 
0
  #5
Nov 10th, 2008
To test some other cases, perhaps have a movie where ALL the humans watch the movie, and one where NONE watch the movie.
Is this correct, as in, I've three different try catch block to show three different scenarios. Or is there a shorter way of doing it?
  1. public static void main(String args[]) throws MoviePropertiesNotSetException, IllegalAgeException
  2. {
  3. Cinema cn = new Cinema();
  4. Human a = new Adult("Joe",43);
  5. Human b = new Adult("Sue",39);
  6. Human c = new Adult("Tracy",20);
  7. Human d = new Child("Sammy",17);
  8. Human e = new Child("Julie",12);
  9. Human f = new Child("Mona",4);
  10. hm.add(a);hm.add(b);hm.add(c);hm.add(d);
  11. hm.add(e);hm.add(f);
  12. hm.trimToSize();
  13.  
  14. //scenario 1: Movie rated G
  15. try
  16. {
  17. //setting properties to the movie
  18. movieNew.setName("Comedy");
  19. movieNew.setRating("G");
  20. movieNew.setTitle("Mr Bin");
  21.  
  22. cn.setMovie(movieNew);
  23. cn.showMovie();
  24. cn.addMovieGoers(hm);
  25. }
  26. catch(MoviePropertiesNotSetException exp)
  27. {
  28. System.out.println(exp.getMessage());
  29. }
  30. catch(IllegalAgeException msg)
  31. {
  32. System.out.println(msg.getMessage());
  33. }
  34.  
  35. //Scenario 2: Movie rated PG
  36. try
  37. {
  38. //setting properties to the movie
  39. movieNew.setName("Action");
  40. movieNew.setRating("PG");
  41. movieNew.setTitle("The Fugitive");
  42.  
  43. cn.setMovie(movieNew);
  44. cn.showMovie();
  45. cn.addMovieGoers(hm);
  46. }
  47. catch(MoviePropertiesNotSetException exp)
  48. {
  49. System.out.println(exp.getMessage());
  50. }
  51. catch(IllegalAgeException msg)
  52. {
  53. System.out.println("Exception "+msg.getMessage());
  54. }
  55.  
  56. //Scenario 3: Movie Rated A
  57. try
  58. {
  59. //setting properties to the movie
  60. movieNew.setName("Action");
  61. movieNew.setRating("A");
  62. movieNew.setTitle("Lost in The Woods");
  63.  
  64. cn.setMovie(movieNew);
  65. cn.showMovie();
  66. cn.addMovieGoers(hm);
  67.  
  68. }
  69. catch(MoviePropertiesNotSetException exp)
  70. {
  71. System.out.println("Movie properties missing: "+exp.getMessage());
  72. }
  73. catch(IllegalAgeException msg)
  74. {
  75. System.out.println("Exception "+msg.getMessage());
  76. }
  77. finally
  78. {
  79. System.out.println("Movie is over! Have a good day.");
  80. movieNew=null;
  81. cn=null;
  82. }
  83. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Cinema

 
0
  #6
Nov 10th, 2008
I would take that as three scenarios. Is there a shorter way? Most likely, but I wouldn't think that is the aim here, and I wouldn't be the best person to try optimise your code.

Perhaps somebody else can help you there...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC