Coding Challenge #3 Getting Data

in #coding7 years ago

I'm glad we're done with the first two steps (Basic principles and object creation) and we can head now to our third step.

Getting Data

Now, what we'll use is an API of "theMovieDB" which will allow us easily to get on their data.

On: https://developers.themoviedb.org/3/movies/get-top-rated-movies you will find even the exact code we will need to query the data.

The task will be:

The user enters the number of movies he wants to "download". You will then use one of the API example codes on the linked website to get those movies.

Some small details:

You will have to apply for an API key on the movie DB.

Then you'll use the code you can find at the linked page to get 1 page of the "most popular movies"

If the number of movies the user entered is bigger than that you will have to start the next query with the next page

What you are going to do is a "get request" which is the basic method to get data from a certain web source.

You will receive data in JSON format, that means you will have to parse the data which comes in.
Fortunately this is very easy with several libraries: http://www.geeksforgeeks.org/parse-json-java/

For now, I don't want you to worry about the response not coming immediately, working asynchronously will be the next step.

And before I forget it!
If you're entering only now or you want to check if I really solved the last challenge:

The class representing a movie:
public class Movie
{
private final String title;
private String overview;
private String releaseDate;
private String[] genres;

    public Movie(String mTitle)
    {
        title = mTitle;
    }

    @Override
    public String toString()
    {
        return String.format("Title %s, released on the: %s", title, releaseDate);
    }

    //----- Getter and Setters -----//

    public String getTitle()
    {
        return title;
    }

    public String getOverview()
    {
        return overview;
    }

    public void setOverview(final String overview)
    {
        this.overview = overview;
    }

    public String getReleaseDate()
    {
        return releaseDate;
    }

    public void setReleaseDate(final String releaseDate)
    {
        this.releaseDate = releaseDate;
    }

    public String[] getGenres()
    {
    return genres;
    }

    public void setGenres(final String[] genres)
    {
        this.genres = genres;
    }
}

The main class:

import java.util.ArrayList;
import java.util.Scanner;

public class Start
{
    private static ArrayList<Movie> movies = new ArrayList<>();

    public static void main(String...args)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Welcome to the movie creator interface");
        System.out.println("Please type the number of movies you would like to create");

        int amount = scanner.nextInt();
        discardRestOfTheLine(scanner);

        for(int i = 0; i < amount; i++)
        {
            System.out.println("Creating movie number: " + (i+1));
            readInMovie(scanner);
        }

        for(Movie movie: movies)
        {
            System.out.println(movie.toString());
        }
    }

    private static void discardRestOfTheLine(final Scanner scanner)
    {
        scanner.nextLine();
    }

    private static void readInMovie(final Scanner scanner)
    {
        System.out.println("Please write the title of the movie");
        String title = scanner.nextLine();
        Movie movie = new Movie(title);

        System.out.println("Please write a short overview");
        movie.setOverview(scanner.nextLine());

        System.out.println("Please enter the release date of the movie");
        movie.setReleaseDate(scanner.nextLine());

        System.out.println("How many genres do you want to assign to the movie?");

        int amount = scanner.nextInt();

        String[] genres = new String[amount];

        for(int i = 0; i < amount; i++)
        {
            System.out.println("Enter #" + (i+1));
            genres[i] = scanner.next();
        }
        movie.setGenres(genres);
        discardRestOfTheLine(scanner);
        System.out.println("Finished creating movie: " + movie.getTitle());
        movies.add(movie);
    }
}

Now, have fun coding, we started already!

IMG_20170925_095313.jpg

Sort:  

If you have any questions or problems just contact me here or on Steem Chat so I can help you out =)

The @OriginalWorks bot has determined this post by @raycoms to be original material and upvoted it!

ezgif.com-resize.gif

To call @OriginalWorks, simply reply to any post with @originalworks or !originalworks in your message!

To enter this post into the daily RESTEEM contest, upvote this comment! The user with the most upvotes on their @OriginalWorks comment will win!

For more information, Click Here!
Special thanks to @reggaemuffin for being a supporter! Vote him as a witness to help make Steemit a better place!

post your very interesting, I like your post, because it can add insight for me and all the people in stemeet, I love to read and see new things in my life, hopefully others also like your post, I hope posts next can provide thing better yet, so that I know many things in my life, thank you :)

Congratulations @raycoms, this post is the fifth most rewarded post (based on pending payouts) in the last 12 hours written by a Dust account holder (accounts that hold between 0 and 0.01 Mega Vests). The total number of posts by Dust account holders during this period was 3806 and the total pending payments to posts in this category was $1020.74. To see the full list of highest paid posts across all accounts categories, click here.

If you do not wish to receive these messages in future, please reply stop to this comment.

Coin Marketplace

STEEM 0.37
TRX 0.12
JST 0.040
BTC 70162.45
ETH 3540.43
USDT 1.00
SBD 4.79