Skip to content
Advertisement

I want to be able to compare two manga’s ratings but I don’t know how to make it so I can input two titles that then grabs the scores to compare

I have the data for 10 different manga set in an array list and they go as title, rating, ongoing or not, and amount of chapters. I want to be able to input two different manga titles into a scanner and then have it compare the rating of the two to see which is higher. This is the code I have so far. Thanks for your help in advance.

public class TopMangaData {

private String title;
private double rating;
private boolean onGoing;
private int chapters;

public TopMangaData(String title, double rating, boolean onGoing, int chapters) {
    this.title = title;
    this.rating = rating;
    this.onGoing = onGoing;
    this.chapters = chapters;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public double getRating() {
    return rating;
}

public void setRating(double rating) {
    this.rating = rating;
}

public boolean getOnGoing() {
    return onGoing;
}

public void setOnGoing(boolean onGoing) {
    this.onGoing = onGoing;
}

public int getChapters() {
    return chapters;
}

public void setChapters(int chapters) {
    this.chapters = chapters;
}

public String toString() {
    return "nTop Manga Data nTitle: " + title + "nRating: " + rating + "nOn going: " + onGoing + "nChapters: " + chapters;
}

}

import java.util.ArrayList;

import java.util.Scanner;

public class TopMangaDataRunner { public static void main(String[] args) {

    ArrayList<TopMangaData> TopMangaData = new ArrayList<TopMangaData>(10); {
        TopMangaData.add(new TopMangaData("Berserk", 9.43, false, 380));
        TopMangaData.add(new TopMangaData("JoJo's Bizarre Adventure Part 7: Steel Ball Run", 9.27, false, 96));
        TopMangaData.add(new TopMangaData("One Piece", 9.17, true, 1041));
        TopMangaData.add(new TopMangaData("Vagabond", 9.16, false, 327));
        TopMangaData.add(new TopMangaData("Monster", 9.12, false, 162));
        TopMangaData.add(new TopMangaData("Fullmetal Alchemist", 9.07, false, 116));
        TopMangaData.add(new TopMangaData("Grand Blue", 9.06, true, 75));
        TopMangaData.add(new TopMangaData("Goodnight Punpun", 9.05, false, 147));
        TopMangaData.add(new TopMangaData("Slam Dunk", 9.04, false, 276));
        TopMangaData.add(new TopMangaData("Vinland Saga", 8.99, true, 190));
        
        for(TopMangaData m :TopMangaData) {
            System.out.println(m.toString());
        }
        Scanner scan = new Scanner(System.in);
        
        String firstComparison;
        String secondComparison;
        
        System.out.println("");
        System.out.println("Want to compare two of these top manga's scores?");
        System.out.println("Input the first manga you would like to compare:");
        
        firstComparison = scan.nextLine();
        
        System.out.println("Input the second manga you would like to compare:");
        
        secondComparison = scan.nextLine();
        
        

        
        }
            
    }

}

Advertisement

Answer

First you’re going to need to find the objects from your list that you want to compare. Easiest way is to just iterate over your list and check the title (assuming the title is within your list):

int firstComparisonValue = 0;
for(TopMangaData m :TopMangaData)
        if(m.getTitle().equals(firstComparison))
            firstComparisonValue = m.getRating();

int secondComparisonValue = 0;
for(TopMangaData m :TopMangaData)
        if(m.getTitle().equals(secondComparison))
            secondComparisonValue = m.getRating();

Then just compare your values and print out the title of the large one

if(firstComparisonValue > secondComparisonValue){
   System.out.println("First manga has higher rating")
}
else if (firstComparisonValue < secondComparisonValue){
   System.out.println("Second manga has higher rating")
}
else {
   System.out.println("Both have the same rating!")
}
Advertisement