From 5566ce1c4277792a0ce8390f7c08772f00521831 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 7 Mar 2024 15:48:13 -0800 Subject: [PATCH 1/4] final --- .idea/misc.xml | 2 +- src/main/java/Actors.java | 48 +++++++++++++++++++---- src/main/java/Main.java | 40 ++++++++++++++++++-- src/main/java/Movie.java | 71 +++++++++++++++++++++++++++++++---- src/test/java/ActorsTest.java | 3 +- src/test/java/MovieTest.java | 2 +- 6 files changed, 146 insertions(+), 20 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 668048d..2f60269 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,5 @@ - + \ No newline at end of file diff --git a/src/main/java/Actors.java b/src/main/java/Actors.java index ebf8e2d..ce9824d 100644 --- a/src/main/java/Actors.java +++ b/src/main/java/Actors.java @@ -1,15 +1,26 @@ +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 + public static final String API_KEY = "2oc2yj5UHqESdhSIvfVrpA==kXY0Y8KYuwuPgvNz"; // TODO --> add your api key about Actors here String netWorth; Boolean isAlive; + String gender; + String nationality; + String birthday; + - public Actors(String netWorth, boolean isAlive){ + public Actors(String netWorth, boolean isAlive, String gender, String nationality, String birthday){ //TODO --> (Write a proper constructor using the get_from_api functions) + this.netWorth = netWorth; + this.isAlive = isAlive; + this.gender = gender; + this.nationality = nationality; + this.birthday = birthday; } @SuppressWarnings({"deprecation"}) /** @@ -20,9 +31,9 @@ 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); + name.replace(" ", "+")+"&apikey="+"2oc2yj5UHqESdhSIvfVrpA==kXY0Y8KYuwuPgvNz"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestProperty("X-Api-Key", API_KEY); + connection.setRequestProperty("X-Api-Key", "2oc2yj5UHqESdhSIvfVrpA==kXY0Y8KYuwuPgvNz"); System.out.println(connection); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); @@ -46,19 +57,42 @@ public String getActorData(String name) { public double getNetWorthViaApi(String actorsInfoJson){ //TODO --> (This function must return the "NetWorth") double result = 0.0; + JSONObject info = new JSONObject(actorsInfoJson); + result = info.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; + JSONObject info = new JSONObject(actorsInfoJson); + statues = info.getBoolean("is_alive"); + return statues; } public String getDateOfDeathViaApi(String actorsInfoJson){ //TODO --> (If your chosen actor is deceased it must return the date of death) --> String date = ""; - return date; + JSONObject Date = new JSONObject(actorsInfoJson); + date = Date.getString("death"); + return date; + } + public String getGender(String actorsInfoJson){ + String gender = null; + JSONObject info = new JSONObject(actorsInfoJson); + gender = info.getString("gender"); + return gender; + } + public String getNationality(String actorsInfoJson){ + String nationality = null; + JSONObject info = new JSONObject(actorsInfoJson); + nationality = info.getString("nationality"); + return nationality; + } + public String getBirthday(String actorsInfoJson){ + String birthday = null; + JSONObject info = new JSONObject(actorsInfoJson); + birthday = info.getString("birthday"); + return birthday; } - } \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 92f3d9c..d04e8bc 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,9 +1,43 @@ +import java.io.IOException; +import java.util.ArrayList; +import java.util.Scanner; + public class Main { - public static void main(String[] args) { - // TODO --> complete main function + public static void main(String[] args) throws IOException { + // TODO --> complete main function. runMenu(); + } - public static void runMenu() { + + public static void runMenu() throws IOException { // TODO + Scanner scanner = new Scanner(System.in); + System.out.println("Hello"); + System.out.println("Which one do you want?"); + System.out.println("1-Mvie"); + System.out.println("2-Actor/Actress"); + int n = scanner.nextInt(); + if (n == 1) { + System.out.println("Enter the name of the movie"); + String name = scanner.next(); + Movie movie = new Movie(new ArrayList<>(), "", 0, "", "", "", ""); + System.out.println(movie.getImdbVotesViaApi((movie.getMovieData(name)))); + System.out.println(movie.getRatingViaApi((movie.getMovieData(name)))); + System.out.println(movie.getDirector(movie.getMovieData(name))); + System.out.println(movie.getWriter(movie.getMovieData(name))); + System.out.println(movie.getActorListViaApi(movie.getMovieData(name))); + System.out.println(movie.getGener(movie.getMovieData(name))); + System.out.println(movie.getLanguage(movie.getMovieData(name))); + } + if (n == 2) { + System.out.println("Enter the name of the actor/actress"); + String name = scanner.next(); + Actors actors = new Actors("", false, "", "", ""); + System.out.println(actors.getNetWorthViaApi(actors.getActorData(name))); + System.out.println(actors.isAlive(actors.getActorData(name))); + System.out.println(actors.getGender(actors.getActorData(name))); + System.out.println(actors.getNationality(actors.getActorData(name))); + System.out.println(actors.getDateOfDeathViaApi(actors.getActorData(name))); + } } } \ No newline at end of file diff --git a/src/main/java/Movie.java b/src/main/java/Movie.java index 34b5d4c..e981f2c 100644 --- a/src/main/java/Movie.java +++ b/src/main/java/Movie.java @@ -1,17 +1,33 @@ +import org.json.JSONArray; +import org.json.JSONObject; + 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.List; + 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 = "96ae8e2d"; // TODO --> add your api key about Movie here int ImdbVotes; ArrayList actorsList; String rating; + String director; + String gener; + String writer; + String language; - public Movie(ArrayList actorsList, String rating, int ImdbVotes){ + public Movie(ArrayList actorsList, String rating, int ImdbVotes, String director, String gener, String writer, String language){ //TODO --> (Write a proper constructor using the get_from_api functions) + this.actorsList = actorsList; + this.rating = rating; + this.ImdbVotes = ImdbVotes; + this.director = director; + this.gener = gener; + this.writer = writer; + this.language = language; } @SuppressWarnings("deprecation") @@ -23,9 +39,9 @@ public Movie(ArrayList actorsList, String rating, int ImdbVotes){ */ 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="+"96ae8e2d"); URLConnection Url = url.openConnection(); - Url.setRequestProperty("Authorization", "Key" + API_KEY); + Url.setRequestProperty("Authorization", "Key" + "96ae8e2d"); BufferedReader reader = new BufferedReader(new InputStreamReader(Url.getInputStream())); String line; StringBuilder stringBuilder = new StringBuilder(); @@ -40,17 +56,58 @@ 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; + JSONObject info = new JSONObject(moviesInfoJson); + ImdbVotes = Integer.parseInt(info.getString("imdbVotes").replace(",","")); + return ImdbVotes; } 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; + //String rating = null; + JSONObject info = new JSONObject(moviesInfoJson); + JSONArray ratings = info.getJSONArray("Ratings"); + JSONObject rate = ratings.getJSONObject(0); + String value = rate.getString("Value"); + + return value; } - public void getActorListViaApi(String movieInfoJson){ + public ArrayList getActorListViaApi(String movieInfoJson){ //TODO --> (This function must return the "Actors" in actorsList) + JSONObject info = new JSONObject(movieInfoJson); + //List actors = new ArrayList<>(); + String actors = info.getString("Actors"); + String[] arraylist = actors.split(", "); + for (String i : arraylist) { + actorsList.add(i); + } + + return actorsList; + } + public String getDirector(String movieInfoJson){ + String director = null; + JSONObject info = new JSONObject(movieInfoJson); + director = info.getString("Director"); + return director; + } + public String getGener(String movieInfoJson){ + String gener = null; + JSONObject info = new JSONObject(movieInfoJson); + gener = info.getString("Genre"); + return gener; + } + public String getWriter(String movieInfoJson){ + String writer = null; + JSONObject info = new JSONObject(movieInfoJson); + writer = info.getString("Writer"); + return writer; + } + public String getLanguage(String movieInfoJson){ + String language = null; + JSONObject info = new JSONObject(movieInfoJson); + language = info.getString("Language"); + return language; } } \ No newline at end of file diff --git a/src/test/java/ActorsTest.java b/src/test/java/ActorsTest.java index 457e28b..9dc406d 100644 --- a/src/test/java/ActorsTest.java +++ b/src/test/java/ActorsTest.java @@ -2,11 +2,12 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class ActorsTest { + static String data1 , data2; static Actors actors; @BeforeAll static void setUp() { - actors = new Actors("",false); + actors = new Actors("",false, "", "", ""); data1 = actors.getActorData("jennifer lawrence"); data2 = actors.getActorData("robin williams"); } diff --git a/src/test/java/MovieTest.java b/src/test/java/MovieTest.java index ce8a14c..d95e9ae 100644 --- a/src/test/java/MovieTest.java +++ b/src/test/java/MovieTest.java @@ -11,7 +11,7 @@ public class MovieTest { @BeforeAll static void setUp() throws IOException { - movie = new Movie(new ArrayList<>(),"",0); + movie = new Movie (new ArrayList<>(),"", 0, "", "", "", ""); data1 = movie.getMovieData("maze runner"); // movie data2 = movie.getMovieData("this is us"); // series } From 29cecb6d3be9390147028464f2c59d7247c860f4 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 8 Mar 2024 06:31:29 -0800 Subject: [PATCH 2/4] finaly --- src/main/java/Actors.java | 33 +++++++++++++++++++++++++-------- src/main/java/Main.java | 31 +++++++++++++++++++++++-------- src/main/java/Movie.java | 16 ++++++++++++---- src/test/java/MovieTest.java | 2 +- 4 files changed, 61 insertions(+), 21 deletions(-) diff --git a/src/main/java/Actors.java b/src/main/java/Actors.java index ce9824d..349491a 100644 --- a/src/main/java/Actors.java +++ b/src/main/java/Actors.java @@ -1,3 +1,4 @@ +import org.json.JSONArray; import org.json.JSONObject; import java.io.BufferedReader; @@ -31,9 +32,9 @@ public Actors(String netWorth, boolean isAlive, String gender, String nationalit public String getActorData(String name) { try { URL url = new URL("https://api.api-ninjas.com/v1/celebrity?name="+ - name.replace(" ", "+")+"&apikey="+"2oc2yj5UHqESdhSIvfVrpA==kXY0Y8KYuwuPgvNz"); + name.replace(" ", "+")+"&apikey="+API_KEY); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestProperty("X-Api-Key", "2oc2yj5UHqESdhSIvfVrpA==kXY0Y8KYuwuPgvNz"); + connection.setRequestProperty("X-Api-Key", API_KEY); System.out.println(connection); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); @@ -57,7 +58,8 @@ public String getActorData(String name) { public double getNetWorthViaApi(String actorsInfoJson){ //TODO --> (This function must return the "NetWorth") double result = 0.0; - JSONObject info = new JSONObject(actorsInfoJson); + JSONArray array = new JSONArray(actorsInfoJson); + JSONObject info = array.getJSONObject(0); result = info.getDouble("net_worth"); return result; } @@ -65,33 +67,48 @@ public double getNetWorthViaApi(String actorsInfoJson){ public boolean isAlive(String actorsInfoJson){ //TODO --> (If your chosen actor is alive it must return true otherwise it must return false) boolean statues = false; - JSONObject info = new JSONObject(actorsInfoJson); + JSONArray array = new JSONArray(actorsInfoJson); + JSONObject info = array.getJSONObject(0); statues = info.getBoolean("is_alive"); - return statues; + return statues; } public String getDateOfDeathViaApi(String actorsInfoJson){ //TODO --> (If your chosen actor is deceased it must return the date of death) --> String date = ""; + boolean statues = false; + JSONArray array = new JSONArray(actorsInfoJson); + JSONObject info = array.getJSONObject(0); + statues = info.getBoolean("is_alive"); + if (statues == false) { JSONObject Date = new JSONObject(actorsInfoJson); date = Date.getString("death"); + } + else { + date = "_"; + } return date; } public String getGender(String actorsInfoJson){ String gender = null; - JSONObject info = new JSONObject(actorsInfoJson); + JSONArray array = new JSONArray(actorsInfoJson); + JSONObject info = array.getJSONObject(0); gender = info.getString("gender"); return gender; } public String getNationality(String actorsInfoJson){ String nationality = null; - JSONObject info = new JSONObject(actorsInfoJson); + JSONArray array = new JSONArray(actorsInfoJson); + JSONObject info = array.getJSONObject(0); nationality = info.getString("nationality"); return nationality; } public String getBirthday(String actorsInfoJson){ String birthday = null; - JSONObject info = new JSONObject(actorsInfoJson); + JSONArray array = new JSONArray(actorsInfoJson); + JSONObject info = array.getJSONObject(0); + + birthday = info.getString("birthday"); return birthday; } diff --git a/src/main/java/Main.java b/src/main/java/Main.java index d04e8bc..07fb192 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,3 +1,6 @@ +import org.json.JSONArray; +import org.json.JSONObject; + import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; @@ -20,14 +23,25 @@ public static void runMenu() throws IOException { if (n == 1) { System.out.println("Enter the name of the movie"); String name = scanner.next(); - Movie movie = new Movie(new ArrayList<>(), "", 0, "", "", "", ""); - System.out.println(movie.getImdbVotesViaApi((movie.getMovieData(name)))); - System.out.println(movie.getRatingViaApi((movie.getMovieData(name)))); - System.out.println(movie.getDirector(movie.getMovieData(name))); - System.out.println(movie.getWriter(movie.getMovieData(name))); - System.out.println(movie.getActorListViaApi(movie.getMovieData(name))); - System.out.println(movie.getGener(movie.getMovieData(name))); - System.out.println(movie.getLanguage(movie.getMovieData(name))); + Movie movie = new Movie(new ArrayList<>(), "", 0, "", "", "", "", ""); + String found = movie.getResponsive(movie.getMovieData(name)); + while (true) { + if (found == "false") { + System.out.println("Movie not found, pleas try again!"); + name = scanner.next(); + found = movie.getResponsive(movie.getMovieData(name)); + } + if (found == "true") { + System.out.println(movie.getImdbVotesViaApi((movie.getMovieData(name)))); + System.out.println(movie.getRatingViaApi((movie.getMovieData(name)))); + System.out.println(movie.getDirector(movie.getMovieData(name))); + System.out.println(movie.getWriter(movie.getMovieData(name))); + System.out.println(movie.getActorListViaApi(movie.getMovieData(name))); + System.out.println(movie.getGener(movie.getMovieData(name))); + System.out.println(movie.getLanguage(movie.getMovieData(name))); + break; + } + } } if (n == 2) { System.out.println("Enter the name of the actor/actress"); @@ -38,6 +52,7 @@ public static void runMenu() throws IOException { System.out.println(actors.getGender(actors.getActorData(name))); System.out.println(actors.getNationality(actors.getActorData(name))); System.out.println(actors.getDateOfDeathViaApi(actors.getActorData(name))); + System.out.println(actors.getBirthday(actors.getActorData(name))); } } } \ No newline at end of file diff --git a/src/main/java/Movie.java b/src/main/java/Movie.java index e981f2c..74d83f4 100644 --- a/src/main/java/Movie.java +++ b/src/main/java/Movie.java @@ -10,7 +10,7 @@ import java.util.List; public class Movie { - public static final String API_KEY = "96ae8e2d"; // TODO --> add your api key about Movie here + public static final String API_KEY = "4cc68bea"; // TODO --> add your api key about Movie here int ImdbVotes; ArrayList actorsList; String rating; @@ -18,8 +18,9 @@ public class Movie { String gener; String writer; String language; + String responsive; - public Movie(ArrayList actorsList, String rating, int ImdbVotes, String director, String gener, String writer, String language){ + public Movie(ArrayList actorsList, String rating, int ImdbVotes, String director, String gener, String writer, String language, String responsive){ //TODO --> (Write a proper constructor using the get_from_api functions) this.actorsList = actorsList; this.rating = rating; @@ -28,6 +29,7 @@ public Movie(ArrayList actorsList, String rating, int ImdbVotes, String this.gener = gener; this.writer = writer; this.language = language; + this.responsive = responsive; } @SuppressWarnings("deprecation") @@ -39,9 +41,9 @@ public Movie(ArrayList actorsList, String rating, int ImdbVotes, String */ public String getMovieData(String title) throws IOException { - URL url = new URL("https://www.omdbapi.com/?t="+title+"&apikey="+"96ae8e2d"); + URL url = new URL("https://www.omdbapi.com/?t="+title+"&apikey="+ API_KEY); URLConnection Url = url.openConnection(); - Url.setRequestProperty("Authorization", "Key" + "96ae8e2d"); + Url.setRequestProperty("Authorization", "Key" + API_KEY); BufferedReader reader = new BufferedReader(new InputStreamReader(Url.getInputStream())); String line; StringBuilder stringBuilder = new StringBuilder(); @@ -110,4 +112,10 @@ public String getLanguage(String movieInfoJson){ language = info.getString("Language"); return language; } + public String getResponsive(String movieInfoJson){ + String responsive = null; + JSONObject info = new JSONObject(movieInfoJson); + responsive = info.getString("Responsive"); + return responsive; + } } \ No newline at end of file diff --git a/src/test/java/MovieTest.java b/src/test/java/MovieTest.java index d95e9ae..cba34e4 100644 --- a/src/test/java/MovieTest.java +++ b/src/test/java/MovieTest.java @@ -11,7 +11,7 @@ public class MovieTest { @BeforeAll static void setUp() throws IOException { - movie = new Movie (new ArrayList<>(),"", 0, "", "", "", ""); + movie = new Movie (new ArrayList<>(),"", 0, "", "", "", "", ""); data1 = movie.getMovieData("maze runner"); // movie data2 = movie.getMovieData("this is us"); // series } From bf013c420e241bad73dbecae581b7a641b9911f8 Mon Sep 17 00:00:00 2001 From: sarinahassani <160701638+sarinahassani@users.noreply.github.com> Date: Fri, 8 Mar 2024 07:29:26 -0800 Subject: [PATCH 3/4] Update Report-Template.md --- Report-Template.md | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/Report-Template.md b/Report-Template.md index 1db99da..e00b8f3 100644 --- a/Report-Template.md +++ b/Report-Template.md @@ -1,56 +1,43 @@ # Project Title -Simple overview of use/purpose. +Search Movie & Actor. ## Description -An in-depth paragraph about your project and overview of use. +In this program, you can enter the name of your favorite actor and get information about them. ## Getting Started +... + ### Dependencies -* Describe any prerequisites, libraries, OS version, etc., needed before installing program. -* ex. Windows 10 +You should get Gradle version:8.6 in this program you need this libraries:ArrayList, JSONArray, JSONObject, IOException, InputStreamReader, net.URLConnection, io.BufferedReader, net.HttpURLConnection. ### Installing -* How/where to download your program -* Any modifications needed to be made to files/folders +If you see this program you should install intilij. If you run this program you should install Gradle too. ### Executing program -* How to run the program -* Step-by-step bullets -``` -code blocks for commands -``` +After installing the program, enter the program file and open the main part, then press the green triangle next to the main function and the program will run like this. ## Help -Any advise for common problems or issues. -``` -command to run if program contains helper info -``` +To learn better "API", you can refer to Jadi's YouTube channel. You can use the following sites to learn programming better. +KHAN ACADEMY, CODE SCHOOL, COURSERA. ## Authors -Contributors names and contact info - -ex. Dominique Pizzie -ex. [@DomPizzie](https://twitter.com/dompizzie) +Sarina Hassani. ## 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 + ## Acknowledgments @@ -59,4 +46,4 @@ Inspiration, code snippets, etc. * [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 +* [fvcproductions](https://gist.github.com/fvcproductions/1bfc2d4aecb01a834b46) From 940032cc02aa283a8f6c85e4f9fe3780a7b64642 Mon Sep 17 00:00:00 2001 From: sarinahassani <160701638+sarinahassani@users.noreply.github.com> Date: Fri, 8 Mar 2024 07:32:47 -0800 Subject: [PATCH 4/4] finaly report --- Report-Template.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Report-Template.md b/Report-Template.md index e00b8f3..17fb175 100644 --- a/Report-Template.md +++ b/Report-Template.md @@ -37,7 +37,8 @@ Sarina Hassani. ## License - + https://omdbapi.com (for Movie) + and https://api-ninjas.com/api/celebrity (for actor) ## Acknowledgments