jeudi 29 novembre 2018

How can I make sure that the entered input matches the array index in order to output the right solution?

So basically, I am trying to output both the top film for a specific year and the total income that movies made that year. Both of my methods are not working correctly and I am especially having trouble in getting the year that was input to match with the year that the movies have in the arrays in order to output the total income and top film of that year.

Here is the getInput.getUserInput()

    public static int getUserInput(int minYear, int maxYear) {
        int year = 0;
        boolean keepLooping = true;
        Scanner input = new Scanner(System.in);
        while (keepLooping) {
            System.out.printf("\nEnter a year from %s - %s:", minYear, maxYear);
            year = input.nextInt();
            if (year < minYear || year > maxYear) {
                System.out.printf("Invalid entry . . .");
            } else {
                keepLooping = false;
            }
        }
        return year;
    }

Here is the class

        public class Films {
            private String filmTitle;
            private double filmIncome;
            private int premiereYear;

        Films(String title, double income, int year) {
            filmTitle = title;
            filmIncome = income;
            premiereYear = year;

        }

        public String getFilmTitle() {
            return filmTitle;
        }

        public void setFilmTitle(String filmTitle) {
            this.filmTitle = filmTitle;
        }

        public double getFilmIncome() {
            return filmIncome;
        }

        public void setFilmIncome(double filmIncome) {
            this.filmIncome = filmIncome;
        }

        public int getPremiereYear() {
            return premiereYear;
        }

        public void setPremiereYear(int premiereYear) {
            this.premiereYear = premiereYear;
        }

    }

Here is the file that runs the program

public static void main(String[] args) {
        Films[] f = new Films[8];
        f[0] = new Films("Frozen", 1290.0, 2013);
        f[1] = new Films("The Lion King", 968.4, 1994);
        f[2] = new Films("Zootopia", 1023.7, 2016);
        f[3] = new Films("Incredibles 2", 1240.3, 2018);
        f[4] = new Films("Finding Dory", 1028.5, 2016);
        f[5] = new Films("Shrek 2", 919.8, 2004);
        f[6] = new Films("The Jungle Book", 966.5, 2016);
        f[7] = new Films("Despicable Me 2", 970.7, 2013);

        int yearEntered = getInput.getUserInput(1991, 2018);
        System.out.printf("\nThank you received %s", yearEntered);

        Films total = getTotalIncome(f, yearEntered);
        System.out.printf("\nThe total amount made for %s was $%2.2f", yearEntered, total.getFilmIncome());

        Films top = getTopFilm(f, yearEntered);
        if (top == null) {
            System.out.printf("0");
        } else {
            System.out.printf("\nThe greatest income made by a movie in %s was %s at $%2.2f", yearEntered,
                    top.getFilmTitle(), top.getFilmIncome());
        }

    }

    private static Films getTotalIncome(Films[] f, int yearEntered) {
        Films totalIncome = null;
        double add = 0;
        for (int i = 0; i < f.length; i++) {
            if (f[i].getPremiereYear() == yearEntered) {
                add += f[i].getFilmIncome();
                totalIncome = f[i];
            }

        }
        return totalIncome;

    }

    private static Films getTopFilm(Films[] f, int yearEntered) {
        Films topFilm = null;
        double max = 0;
        for (int i = 0; i < f.length; i++) {
            if (f[i].getPremiereYear() != yearEntered) {
                continue;
            }
            if (f[i].getFilmIncome() > max) {
                topFilm = f[i];
            }
        }
        return topFilm;
    }

Aucun commentaire:

Enregistrer un commentaire