Creating methods with Scenarios

Reply

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

Creating methods with Scenarios

 
0
  #1
Oct 27th, 2008
Hi, I have a Cinema class as shown in the code below. However I'm required to create my own exceptions and pass them to the controlling module (here method with scenarios). After an exception occurs the code should stop execution of current scenario and go to another one. The code should contains plenty of System.out.println statements so that the flow is clear.
Can someone give me a hand on this please, I'm a newbie in Java and I don't know how to go about this creating my own exceptions.

  1. import java.util.*;
  2. import java.io.*;
  3. public class Cinema
  4. {
  5. float cost;
  6. String Title;
  7.  
  8. String mrPG="PG";
  9. String mrA="A";
  10.  
  11. Movie movieNew = new Movie();
  12. //MovieRating class
  13. MovieRating mrating = new MovieRating();
  14. private ArrayList<Human> hm = new ArrayList();
  15.  
  16. public Cinema()
  17. {
  18.  
  19. }
  20. public Cinema (String mtitle, float pounds)
  21. {
  22. this.Title=mtitle;
  23. this.cost = pounds;
  24. }
  25.  
  26. public float getcost (int people)
  27. {
  28. return cost * people;
  29. }
  30.  
  31. /**
  32.   * Method check that the movie title and rating have been set,
  33.   * if not throw an exception.
  34.   */
  35. public void setMovie(Movie movieNew) throws IOException
  36. {
  37. try
  38. {
  39. if(movieNew.getTitle()!=null && movieNew.getRating()!=null)
  40. {
  41. System.out.println("The movie is called " +movieNew.getTitle()+
  42. " and is rated " +movieNew.getRating()+ ".");
  43. }
  44. else
  45. {
  46. System.out.println("Movie title and rating not set");
  47. }
  48. }
  49. catch(Exception e)
  50. {
  51. System.out.println("Movie properties not set.");
  52. }
  53. }
  54. /*
  55.   * this method first checks if the movie properties are set.
  56.   */
  57. public void showMovie() throws IOException
  58. {
  59. try
  60. {
  61. if(movieNew.getTitle()!=null)
  62. {
  63. System.out.println(movieNew.getTitle()+" Now Showing"); }
  64. else
  65. {
  66. System.out.println("Show has no Movie title.");
  67. }
  68. }
  69. catch(Exception e)
  70. {
  71. System.out.println("Check the movie properties.");
  72. }
  73. finally
  74. {
  75. System.out.println("Thanks for coming.");
  76. }
  77. }
  78.  
  79. public void addMovieGoers(Human h)
  80. {
  81. Human a = new Adult("Joe",43);
  82. Human b = new Adult("Sue",39);
  83. Human c = new Adult("Tracy",20);
  84. Human d = new Child("Sammy",17);
  85. Human e = new Child("Julie",12);
  86. Human f = new Child("Mona",6);
  87. hm.add(a);hm.add(b);hm.add(c);hm.add(d);
  88. hm.add(e);hm.add(f);
  89.  
  90. hm.trimToSize();
  91. String toPrint = "";
  92. try
  93. {
  94. if(movieNew.getRating()!=null && movieNew.getTitle()!=null)
  95. {
  96. if(mrPG.equals(movieNew.getRating())) //movies rated PG
  97. {
  98. System.out.println("The following can only watch under parental guidance:");
  99. for(int i = 0; i < hm.size(); i++)
  100. {
  101. Human test = (Human)hm.get(i);
  102. if ( test instanceof Child)
  103. {
  104. if(toPrint.equals(""))
  105. {
  106. System.out.println(test);
  107. }
  108. else
  109. {
  110. System.out.println("Children: " + toPrint);
  111. }
  112. }
  113. }
  114. }
  115. else if(mrA.equals(movieNew.getRating())) //movies rated Adult
  116. {
  117. System.out.println("This movie is rated A");
  118. for ( int i = 0; i < hm.size(); i++)
  119. {
  120. Human test = (Human)hm.get(i);
  121. if ( test instanceof Adult)
  122. {
  123. if(toPrint.equals(""))
  124. {
  125. System.out.println(test);
  126. }
  127. else
  128. {
  129. System.out.println("Adults: " + toPrint);
  130. }
  131. }
  132. }
  133. }
  134. else //Movies rated G
  135. {
  136. System.out.println("This is movie rated G. So all can watch"+
  137. ": ");
  138. for ( int i = 0; i < hm.size(); i++)
  139. {
  140. System.out.println(i);
  141. }
  142. }
  143. }
  144. }
  145. catch(Exception x)
  146. {
  147. System.out.println("Movie properties are missing. Please set the Title"+
  148. " and rating first.");
  149. }
  150.  
  151. }
  152.  
  153. public static void main(String args[]) throws IOException
  154. {
  155. Cinema cn = new Cinema();
  156. Human h = new Human();
  157. //instantiating and setting properties to the movie
  158. Movie movieNew = new Movie();
  159. movieNew.setName("Comedy");
  160. movieNew.setRating("PG");
  161. movieNew.setTitle("Mr Bin");
  162.  
  163. System.out.println("Setting Movie properties:");
  164. cn.setMovie(movieNew);
  165.  
  166. System.out.println("Movie Goers are: ");
  167. cn.addMovieGoers(h);
  168.  
  169. }
  170. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: Creating methods with Scenarios

 
0
  #2
Oct 27th, 2008
Woah!! Woah!! Woah!! First things first, don't catch Exception unless you really need to! Exception will catch *all* exceptions. I'd rather create my own exception class which inherits the base class. Then you can catch that one!
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: Creating methods with Scenarios

 
0
  #3
Oct 27th, 2008
okay, I've created my exception class as:

  1. public class MyException extends Exception
  2. {
  3. String exception;
  4. public MyException()
  5. {
  6. super();
  7. exception="Unknown";
  8. }
  9. public MyException(String exp)
  10. {
  11. super(exp);
  12. this.exception=exp;
  13. }
  14. public String getException()
  15. {
  16. return this.exception;
  17. }
  18. }

and changed my method to look like this:
  1. public void setMovie(Movie movieNew) throws MyException
  2. {
  3. if(movieNew.getTitle()!=null && movieNew.getRating()!=null)
  4. {
  5. System.out.println("The movie is called " +movieNew.getTitle()+
  6. " and is rated " +movieNew.getRating()+ ".");
  7. }
  8. else
  9. {
  10. throw new MyException("The Movie properties not set");
  11. }
  12. }

So my question is, is this how to create my own exceptions and passing them to the controlling module (here method with scenarios)? and, After an exception occurs does the code stop execution of current scenario and go to another one. or what else should I include.

Another requirement to my Cinema class is to 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.

the main method is:
  1. public static void main(String args[]) throws MyException
  2. {
  3. Cinema cn = new Cinema();
  4. Human h = new Human();
  5. //instantiating and setting properties to the movie
  6. Movie movieNew = new Movie();
  7. movieNew.setName("Comedy");
  8. movieNew.setRating("G");
  9. movieNew.setTitle("Mr Bin");
  10.  
  11. System.out.println("Setting Movie properties:");
  12. cn.setMovie(movieNew);
  13.  
  14. System.out.println("Current Show:");
  15. cn.showMovie();
  16.  
  17. System.out.println("Movie Goers are: ");
  18. cn.addMovieGoers(h);
  19.  
  20. }

Finally, how do I add code to empty the Cinema whether the movie was shown or not. (In the showMovie() method below)
  1. public void showMovie() throws MyException
  2. {
  3. movieNew.setName("Comedy");
  4. movieNew.setRating("G");
  5. movieNew.setTitle("Mr Bin");
  6. if(movieNew.getTitle()!=null || movieNew.getRating()!=null)
  7. {
  8. System.out.println(movieNew.getTitle()+" Now Showing");
  9. }
  10. else
  11. {
  12. throw new MyException ("The movie properties are not set.");
  13. }
  14. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 164
Reputation: orko is an unknown quantity at this point 
Solved Threads: 10
orko orko is offline Offline
Junior Poster

Re: Creating methods with Scenarios

 
0
  #4
Oct 27th, 2008
Originally Posted by Achupa View Post
So my question is, is this how to create my own exceptions and passing them to the controlling module (here method with scenarios)? and, After an exception occurs does the code stop execution of current scenario and go to another one. or what else should I include.
You also can use try catch to use your exceptions. See following example:
  1. public int SomMethod() {
  2. try {
  3. //Your codes
  4. }
  5. catch (MyException e) {
  6. //do whatever you wanted to do.
  7. }
  8. }

Originally Posted by Achupa View Post
Another requirement to my Cinema class is to 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 can call a static method by:
  1. public static returnType methodName (argument)
You don't have to create any object in order to call this method. You can call the method any time you want- from static method. So, you will basically call it from your main function (as that is by default static). Here are two examples of calling a static method:
  1. public static void main(String args[]) throws MyException
  2. {
  3. //direct call to a static method:
  4. yourStaticMethod(arg);
  5. //Through class
  6. Cinema.yourStaticMethod(arg); //NOTE: Cinema is the name of the class, not the object.
  7.  
  8. }


Originally Posted by Achupa View Post
Finally, how do I add code to empty the Cinema whether the movie was shown or not. (In the showMovie() method below)
Your question is not clear to me. You may use another property called showStatus which will contain the information about the show or a property called showTime to contain the time, which will compare time with current time and display if the show is available or not. Is that what you wanted?
A Perfect World
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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