Advertisement
btcworker

Screwed Code

Sep 23rd, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. //import com.google.gson.Gson;
  4. //import com.google.gson.GsonBuilder;
  5.  
  6.  // author @btcworker - Gene Cotillo
  7.  
  8. //line 74 or so will have my comments, but essentially i made all the variables defined right here static. Started getting the null pointer exception
  9. //I added values after i got it the first time, before they were not initialized since i initialize them below.
  10.  
  11. public class Movie {
  12.     public static String title = "A";
  13.     public static String overview = "B";
  14.     public static String releaseDate = "C";
  15.     public static String director = "D";
  16.     public static String genreName = "Action"   ;
  17.     public static String[] genre = {"Drama", "Action", "Comedy", "Horror"};
  18.        
  19.     private static ArrayList<Movie> movieA;
  20.    
  21.     public Movie()  {
  22.         movieA = new ArrayList<Movie>();
  23.     }
  24.     public Movie(String mTitle, String mOverview, String mReleaseDate, String mDirector, String mGenreName) {
  25.         title = mTitle;
  26.         overview = mOverview;
  27.         releaseDate = mReleaseDate;
  28.         director = mDirector;
  29.         genreName = mGenreName;
  30.     }
  31.     public static void addMovie(String mTitle, String mOverview, String mReleaseDate, String mDirector, String mGenreName) {
  32.         Scanner in = new Scanner(System.in);
  33.         int time = 1000;
  34.        
  35.         System.out.println("Please enter the information about your movie below.");
  36.         System.out.println();
  37.         sleeper(time);
  38.         System.out.print("What is the name of your movie?" );
  39.         mTitle = in.nextLine();
  40.         System.out.println();
  41.         sleeper(time);
  42.         System.out.print("Give a short description of what the movie is about: ");
  43.         mOverview = in.nextLine();
  44.         System.out.println();
  45.         sleeper(time);
  46.         System.out.print("What year did the movie come out? ");
  47.         mReleaseDate = in.nextLine();
  48.         System.out.println();
  49.         sleeper(time);
  50.         System.out.print("What is the name of the director of the film? ");
  51.         mDirector = in.nextLine();
  52.         System.out.println();
  53.         sleeper(time);
  54.         System.out.println("What Genre best describes the movie? ");
  55.         sleeper(600);
  56.         System.out.print("Type 1 for Drama, 2 for Action, 3 for Comedy or 4 for Horror" );
  57.         int userGenre = in.nextInt();
  58.            
  59.         if (userGenre == 1) {
  60.             genreName = genre[0];
  61.         }
  62.         else if (userGenre == 2) {
  63.             genreName = genre[1];
  64.         }
  65.         else if (userGenre == 3) {
  66.             genreName = genre[2];
  67.         }
  68.         else if (userGenre == 4) {
  69.             genreName = genre[3];
  70.         }
  71.         else {
  72.             System.out.println("You have entered an invalid entry for genre. We will use the default genre.");
  73.             genreName = genre[0];
  74.         }
  75.         //Everything was working fine until I changed these variables, which are called in the class above, to static.
  76.         //Only did so because Eclipse told me too... Now i get a null pointer exception when trying to type the interger for the genre choice.
  77.         movieA.add(new Movie(title, overview, releaseDate, director, genreName));
  78.        
  79.         sleeper(1200);
  80.         System.out.println();
  81.         System.out.print("Would you like to enter another movie? type 1 for yes or 2 for no");
  82.         int moreMovies = in.nextInt();
  83.         in.close();
  84.        
  85.         if (moreMovies == 1) {
  86.             start();
  87.         }
  88.         else {
  89.             System.out.println(movieA.toString().replaceAll("\\[\\]", ""));
  90.            
  91.             //originally, i was going to turn the arrayList into an object and then use json to print it out.
  92.             //worked well, but then i changed the code.        
  93.             /*
  94.              * Gson gson = new GsonBuilder().setPrettyPrinting().create();
  95.             * String json = gson.toJson();
  96.             * System.out.println("Here are the details for the movies entered:");
  97.             * System.out.println();
  98.             * System.out.println(json);
  99.             */
  100.         }
  101.     }  
  102.         public static void main(String[] args) {
  103.             start();
  104.         }
  105. public static void start() {
  106.     System.out.println("Welcome to The movie list builder!");
  107.     sleeper(900);
  108.     addMovie(title, overview, releaseDate, director, genreName);
  109. }
  110.  
  111. public static void startOver()
  112. {
  113.     System.out.println("Get ready to enter the info for your next movie.");
  114.     System.out.println();
  115.     sleeper(900);
  116.     addMovie(title, overview, releaseDate, director, genreName);
  117. }
  118. public static void sleeper(int time) {
  119.     try {
  120.         Thread.sleep(time);                
  121.     } catch(InterruptedException ex) {
  122.         Thread.currentThread().interrupt();
  123.     }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement