0

I have an array movieScedule that include object Movie. In this movie object I have the price of the ticket.

Now I want to create a method that shows the most expensive ticket for the movie.

The method:

public Movie mostExpensive()
{
  ....
  ....
  ....
}

Now I was thinking to create new movie object inside this method and then for loop to run all over the movies in the array and then another for loop to run from index 0 all over the movies and check each movie price by using getMoviePrice() method

In the end I wanted to declare the object movie that I created inside this method as the highest moviePrice and then return (the movie)

Any idea? Maybe there is a better way?

  • 1
    Sounds like a good idea - go for it! – Nir Alfasi Dec 02 '17 at 23:35
  • I dont know how to do it thats the problem hehe – Yaniv Tarrasch Dec 02 '17 at 23:38
  • Welcome to Stackoverflow, please read [How To Ask](https://stackoverflow.com/help/how-to-ask). Pay special attention to [How To Create MCVE](https://stackoverflow.com/help/mcve). The more effort you'll put into posting a good question: one which is easy to read, understand and which is [on topic](https://stackoverflow.com/help/on-topic) - the chances are higher that it will attract the relevant people and you'll get help even faster. Good luck! – Nir Alfasi Dec 02 '17 at 23:38
  • You only need one loop to find the highest price. If Move is immutable, simply return the found object; else return a newly created copy of the found object. – robertf Dec 02 '17 at 23:39

3 Answers3

1

So I assume, you have a Movie class, like so:

class Movie {
    private Double moviePrice;
}

Any you want to find the highest price, not the movie with the highest price.

Movie mostExpensive() {
    Movie mostExpensiveMovie = null;

    for (Movie movie : movieScedule) {
        if (mostExpensiveMovie == null || //use for the first element
                // compare previous film price with current one
                movie.getMoviePrice() > mostExpensiveMovie.getMoviePrice()) {
            mostExpensiveMovie = movie;
        }
    }
    return mostExpensiveMovie; // return most expensive movie
}

This will be cheaper, no need to create new Movie instance. If you want, this can also be achieved by java8 streams, check this stack post.

Optional<Movie> mostExpensiveMovie = Arrays.stream(movieScedule)
        .max(Comparator.comparing(Movie::getTicketPrice));
Beri
  • 11,470
  • 4
  • 35
  • 57
0

It is hard to understand you question corretly. Hopefully this is what you want. An easy way to obtain the most expensive ticket would be:

ArrayList<Movie> movies = new ArrayList<>();
//populate array list

public Movie getMostExpensiveTicket(ArrayList<Movie> movies) {
    Movie mostExpensive = movies.get(0); // not null save
    for (Movie movie : movies) {
        if (movie.getMoviePrice() > mostExpensive.getMoviePrice())
            mostExpensive = movie;
    }
    return mostExpensive;
}
Community
  • 1
  • 1
0

If you are using java 8 or above, I would suggest taking advantage of java streams. It would look like this:

public static Movie mostExpesive(Movie[] MoviesSchedule) {
    Optional<Movie> maxPriceMovie = Stream.of(MoviesSchedule)
            .collect(Collectors.maxBy((a, b) -> a.price - b.price));
    return maxPriceMovie.get();
}
Test1 Test2
  • 327
  • 2
  • 9