diff --git a/.idea/misc.xml b/.idea/misc.xml index 668048d..e3b8aac 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,8 @@ - + + + + \ No newline at end of file diff --git a/Report-Template.md b/Report-Template.md index 1db99da..c9e7dee 100644 --- a/Report-Template.md +++ b/Report-Template.md @@ -3,60 +3,41 @@ Simple overview of use/purpose. ## Description - +This project is coded with java and using APIs from https://omdbapi.com and https://api-ninjas.com/api/celebrity. +The first API used to accessig movies and series details(such as duration , cast , ...) and the next one was used to show details of the searched actor's name +such as age , nationality , ... An in-depth paragraph about your project and overview of use. ## Getting Started ### Dependencies -* Describe any prerequisites, libraries, OS version, etc., needed before installing program. -* ex. Windows 10 +* For running this project , you need to install gradle as a package manager to download some + packages that are needed . you can install gradle from their site for free. ### Installing - -* How/where to download your program -* Any modifications needed to be made to files/folders +* Intellij IDEA and JDK(JDK21 is recommended ) are needed to run the program. +* All my project are available on github. This is the link : https://github.com/kourosh-mojdehi ### Executing program -* How to run the program -* Step-by-step bullets -``` -code blocks for commands -``` +* open the project in intllij IDEA or any other IDEs. + Then run the main.java class using the gren buttonn on the top. ## Help +Read README if you needed more help. +You can contact the authors down below for more help. -Any advise for common problems or issues. -``` -command to run if program contains helper info -``` ## Authors -Contributors names and contact info - -ex. Dominique Pizzie -ex. [@DomPizzie](https://twitter.com/dompizzie) +By kourosh mojdehi +contact on email: k.rashidimojdehi@gmail.com ## Version History - -* 0.2 - * Various bug fixes and optimizations - * See [commit change]() or See [release history]() * 0.1 - * Initial Release - -## License - -This project is licensed under the [NAME HERE] License - see the LICENSE.md file for details + * The Project completed and commited. -## Acknowledgments -Inspiration, code snippets, etc. -* [awesome-readme](https://github.com/matiassingers/awesome-readme) -* [PurpleBooth](https://gist.github.com/PurpleBooth/109311bb0361f32d87a2) -* [dbader](https://github.com/dbader/readme-template) -* [zenorocha](https://gist.github.com/zenorocha/4526327) -* [fvcproductions](https://gist.github.com/fvcproductions/1bfc2d4aecb01a834b46) \ No newline at end of file +## Thanks to +omdbapi.com and api-ninjas.com developers. diff --git a/src/main/java/Actors.java b/src/main/java/Actors.java index ebf8e2d..9162e73 100644 --- a/src/main/java/Actors.java +++ b/src/main/java/Actors.java @@ -1,16 +1,28 @@ +import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.HttpURLConnection; + public class Actors { - public static final String API_KEY = "Your API_KEY"; // TODO --> add your api key about Actors here - String netWorth; + public static final String API_KEY = "xBCdo/WzbzYrfkOUmGk6Nw==7NtV8oJzJA1PJMbB"; + + String name; + Double netWorth; Boolean isAlive; + String dateOfDeath; - public Actors(String netWorth, boolean isAlive){ - //TODO --> (Write a proper constructor using the get_from_api functions) + public Actors(String netWorth, boolean isAlive) { //in constructor, I added some input validation for the netWorth parameter. + // The replaceAll method is used to remove any non-numeric characters + //(except for the decimal point) from the netWorth string before parsing it as a Double. + // This ensures that only numeric values are assigned to the netWorth attribute. + if (netWorth != null && !netWorth.isEmpty()) { + this.netWorth = Double.parseDouble(netWorth.replaceAll("[^0-9.]", "")); + } + this.isAlive = isAlive; } + @SuppressWarnings({"deprecation"}) /** * Retrieves data for the specified actor. @@ -19,8 +31,8 @@ public Actors(String netWorth, boolean isAlive){ */ public String getActorData(String name) { try { - URL url = new URL("https://api.api-ninjas.com/v1/celebrity?name="+ - name.replace(" ", "+")+"&apikey="+API_KEY); + URL url = new URL("https://api.api-ninjas.com/v1/celebrity?name=" + + name.replace(" ", "+") + "&apikey=" + API_KEY); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("X-Api-Key", API_KEY); System.out.println(connection); @@ -34,7 +46,7 @@ public String getActorData(String name) { } in.close(); - return response.toString(); + return response.substring(1, response.length()-1); } else { return "Error: " + connection.getResponseCode() + " " + connection.getResponseMessage(); } @@ -43,22 +55,25 @@ public String getActorData(String name) { return null; } } - public double getNetWorthViaApi(String actorsInfoJson){ - //TODO --> (This function must return the "NetWorth") - double result = 0.0; + + public double getNetWorthViaApi(String actorsInfoJson) { + + JSONObject jsonObject = new JSONObject(actorsInfoJson); + Double result = jsonObject.getDouble("net_worth"); return result; } - public boolean isAlive(String actorsInfoJson){ - //TODO --> (If your chosen actor is alive it must return true otherwise it must return false) - boolean statues = false; - return statues; + public boolean isAlive(String actorsInfoJson) { + JSONObject jsonObject = new JSONObject(actorsInfoJson); + boolean result = jsonObject.getBoolean("is_alive"); + return result; } - public String getDateOfDeathViaApi(String actorsInfoJson){ - //TODO --> (If your chosen actor is deceased it must return the date of death) --> - String date = ""; - return date; + public String getDateOfDeathViaApi(String actorsInfoJson) { + JSONObject jsonObject = new JSONObject(actorsInfoJson); + String result = jsonObject.getString("death"); + return result; } + } \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 92f3d9c..541e408 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,9 +1,83 @@ +import org.json.JSONObject; +import java.util.ArrayList; +import java.io.IOException; +import java.util.Scanner; + public class Main { - public static void main(String[] args) { - // TODO --> complete main function - runMenu(); + + public static void main(String[] args) throws IOException { + Scanner scanner = new Scanner(System.in); + Actors actors = new Actors("0", true); + Movie movie = new Movie(new ArrayList<>(), "", 0); + + while (true) { + System.out.println("\nChoose an option:"); + System.out.println("1. Searching about an actor "); + System.out.println("2. Searching about a movie"); + System.out.println("3. Exit"); + System.out.print("Enter your choice: "); + try { + int choice = scanner.nextInt(); + + switch (choice) { + case 1: + lookUpActor(scanner, actors); + break; + case 2: + lookUpMovie(scanner, movie); + break; + case 3: + System.out.println("Goodbye!"); + scanner.close(); + return; + } + } + catch(Exception e){ + System.out.println("Invalid choice, please try again."); + scanner.next(); // clears the scanner buffer and prevents the infinite loop + } + } } - public static void runMenu() { - // TODO + + private static void lookUpActor(Scanner scanner, Actors actors) throws IOException { + System.out.print("Enter the actor's name: "); + scanner.nextLine(); + String actorName = scanner.nextLine(); + + String actorData = actors.getActorData(actorName); + + if (actorData != null) { + System.out.println("Actor data:"); + //System.out.println("Name: " + actors.name); + System.out.println("Net worth: $" + actors.getNetWorthViaApi(actorData)); + System.out.println("Is alive: " + actors.isAlive(actorData)); + + if (!actors.isAlive(actorData)) { + System.out.println("Date of death: " + actors.getDateOfDeathViaApi(actorData)); + } + } else { + System.out.println("Failed to retrieve actor data."); + } + } + + private static void lookUpMovie(Scanner scanner, Movie movie) throws IOException { + System.out.print("Enter a movie's title: "); + scanner.nextLine(); // Consume newline left-over + String movieTitle = scanner.nextLine(); + + String movieData = movie.getMovieData(movieTitle); + + if (!movieData.contains("False")) { + JSONObject movieJsonObject = new JSONObject(movieData); + String title = movieJsonObject.getString("Title"); + + System.out.println("Movie data:"); + System.out.println("Title: " + title); + System.out.println("Rating: " + movie.getRatingViaApi(movieData)); + System.out.println("IMDb votes: " + movie.getImdbVotesViaApi(movieData)); + System.out.println("Actors: " + movie.getActorListViaApi(movieData)); + } else { + System.out.println("Movie not found."); + } } } \ No newline at end of file diff --git a/src/main/java/Movie.java b/src/main/java/Movie.java index 34b5d4c..fa5037d 100644 --- a/src/main/java/Movie.java +++ b/src/main/java/Movie.java @@ -1,56 +1,94 @@ +import org.json.JSONArray; +import org.json.JSONObject; +import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; -import java.io.BufferedReader; import java.util.ArrayList; +import java.util.Arrays; + public class Movie { - public static final String API_KEY = "Your API_KEY"; // TODO --> add your api key about Movie here + public static final String API_KEY = "7c353a6f"; //api key from omdbapi int ImdbVotes; ArrayList actorsList; String rating; - public Movie(ArrayList actorsList, String rating, int ImdbVotes){ - //TODO --> (Write a proper constructor using the get_from_api functions) + + public Movie(ArrayList actorsList, String rating, int ImdbVotes) { //initializing the object + this.actorsList = actorsList; + this.rating = rating; + this.ImdbVotes = ImdbVotes; } - @SuppressWarnings("deprecation") + /** - * Retrieves data for the specified movie. + * retrieves data for the specified movie. * - * @param title the name of the title for which MovieData should be retrieved - * @return a string representation of the MovieData, or null if an error occurred + * @param title the name of the title for which moviedata should be retrieved + * @return a string representation of the moviedata, or null if an error occurred */ public String getMovieData(String title) throws IOException { - URL url = new URL("https://www.omdbapi.com/?t="+title+"&apikey="+API_KEY); + URL url = new URL("https://www.omdbapi.com/?t=" + title + "&apikey=" + API_KEY); URLConnection Url = url.openConnection(); - Url.setRequestProperty("Authorization", "Key" + API_KEY); + Url.setRequestProperty("apikey", API_KEY); BufferedReader reader = new BufferedReader(new InputStreamReader(Url.getInputStream())); String line; StringBuilder stringBuilder = new StringBuilder(); - while ((line = reader.readLine())!=null) { + while ((line = reader.readLine()) != null) { stringBuilder.append(line); } reader.close(); - //handle an error if the chosen movie is not found - return stringBuilder.toString(); + + String response = stringBuilder.toString(); + + if (response.contains("False")) { //looking for an error + return "Movie is not found."; + } + return response; } - public int getImdbVotesViaApi(String moviesInfoJson){ - //TODO --> (This function must change and return the "ImdbVotes" as an Integer) - // NOTICE :: you are not permitted to convert this function to return a String instead of an int !!! - int ImdbVotes = 0; - return ImdbVotes; + + public int getImdbVotesViaApi(String moviesInfoJson) { + JSONObject moviesJsonObject = new JSONObject(moviesInfoJson); + if (moviesJsonObject.has("imdbVotes")) { + String imdbVotesStr = moviesJsonObject.getString("imdbVotes"); + imdbVotesStr = imdbVotesStr.replaceAll("[^\\d]", ""); //removes characters which are not numbers + try { //parse the imdbvotes string to an integer + int imdbVotesInt = Integer.parseInt(imdbVotesStr); + return imdbVotesInt; + } catch (NumberFormatException e) { + System.err.println("Error parsing IMDB votes: " + e.getMessage()); + } + } + return 0; //imdbvotes field is not found } - public String getRatingViaApi(String moviesInfoJson){ - //TODO --> (This function must return the rating in the "Ratings" part - // where the source is "Internet Movie Database") --> - String rating = ""; - return rating; + + public String getRatingViaApi(String moviesInfoJson) { + JSONObject jsonObject = new JSONObject(moviesInfoJson); + JSONArray ratings = jsonObject.getJSONArray("Ratings"); + JSONObject rating = ratings.getJSONObject(0); + String result = rating.getString("Value"); + return result; } - public void getActorListViaApi(String movieInfoJson){ - //TODO --> (This function must return the "Actors" in actorsList) + + public String getActorListViaApi(String movieInfoJson) { + JSONObject actorsJsonObject = new JSONObject(movieInfoJson); + String[] actorsListArray = actorsList.toArray(new String[actorsList.size()]); + if (actorsJsonObject.has("Actors")) { //checks if the actors field exists in the json data + String actorsString = actorsJsonObject.getString("Actors"); + String[] actorsArray = actorsString.split(", "); + for (String actor : actorsArray) { //adds each actor name to the actors list + actorsList.add(actor); + } + actorsListArray = actorsList.toArray(new String[actorsList.size()]); //finalize the actors list string + return Arrays.toString(actorsListArray); + } else { + System.out.println("actor information is not found."); + } + return Arrays.toString(actorsListArray); + } } \ No newline at end of file