public class MovieController extends Controller {

  public static List<Movie> movies;

  static {
    movies = new ArrayList<>();
    movies.add(new Movie(1, "dsfnejhf", "Movie on Racing", "abcd", "xyz"));
    movies.add(new Movie(2, "F2", "Comedy Movie", "Venkatesh", "Tamanna"));
    movies.add(new Movie(3, "Titanic", "Movie", "Hero", "Heroine"));
    movies.add(new Movie(4, "Saaho", "action", "Prabhas", "Shradda kapoor"));
    movies.add(new Movie(5, "Bhahubali 1", "action", "Prabhas", "Tamanna"));

  }

  public Result insert(Http.Request request) {
    JsonNode jsonNode = request.body().asJson();
    if (jsonNode == null) {
        return badRequest("insufficient movie information");
    }
    Movie movie = Json.fromJson(jsonNode, Movie.class);
    movies.add(movie);
    return ok(Json.toJson(movie));
  }

this is Movie class and above MovieController class now how to write code for increment of id value from existing id number

public class Movie {


  private int id;
  private String movieName;
  private String description;
  private String hero;
  private String heroine;

  public Movie(int id, String movieName, String description, String hero, String heroine) {
    this.id = id;
    this.movieName = movieName;
    this.description = description;
    this.hero = hero;
    this.heroine = heroine;

  }

  public Result getMoviesById(int id) {
    Movie movie = findById(id);
    if (movie == null) {
        return notFound("movie not found");
    }
    return ok(Json.toJson(movie));
  }
1

There are 1 best solutions below

4
FredvN On

Just get the maximum id from the movies list (0 for a empty list) and increase this with 1

int maxId = movies.stream()
                .mapToInt(m -> m.getId())
                .max()
                .orElse(0);
int nextId = maxId + 1;

an example with a mimimal MovieController:

import java.util.ArrayList;
import java.util.List;

public class MovieController  {

    public static List<Movie> movies;

    static {
        movies = new ArrayList<>();
        movies.add(new Movie(1, "dsfnejhf", "Movie on Racing", "abcd", "xyz"));
        movies.add(new Movie(2, "F2", "Comedy Movie", "Venkatesh", "Tamanna"));
        movies.add(new Movie(3, "Titanic", "Movie", "Hero", "Heroine"));
        movies.add(new Movie(4, "Saaho", "action", "Prabhas", "Shradda kapoor"));
        movies.add(new Movie(5, "Bhahubali 1", "action", "Prabhas", "Tamanna"));

    }

    public Movie insert(Movie movie) {
        int maxId = movies.stream()
                .mapToInt(m -> m.getId())
                .max()
                .orElse(0);
        movie.setId(++maxId);
        movies.add(movie);
        return movie;
    }

    public static void main(String[] args) {
        MovieController mc = new MovieController() ;
        System.out.println("start---");
        for (Movie m : movies) {
            System.out.println(m);
        }
        Movie newMovie = new Movie(-1,"aa","bb", "h1", "h2");
        newMovie = mc.insert(newMovie);
        System.out.println("after insert---");
        for (Movie m : movies) {
            System.out.println(m);
        }
    }
}

and a minimal Movie:

public class Movie {

    private int id;
    private String movieName;
    private String description;
    private String hero;
    private String heroine;


    public Movie(int id, String movieName, String description, String hero, String heroine) {
        this.id = id;
        this.movieName = movieName;
        this.description = description;
        this.hero = hero;
        this.heroine = heroine;

    }

    public Movie getMoviesById(int id) {
        Movie movie = findById(id);
        if (movie == null) {
            return null;
        }
        return movie;
    }

    private Movie findById(int id) {
        return null;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Movie{" +
                "id=" + id +
                ", movieName='" + movieName + '\'' +
                ", description='" + description + '\'' +
                ", hero='" + hero + '\'' +
                ", heroine='" + heroine + '\'' +
                '}';
    }
}

It will add a movie to the list with id 6.