Home > Back-end >  How to write a compareTo method when comparing multiple variables at once?
How to write a compareTo method when comparing multiple variables at once?

Time:10-05

I have an assignment where I have an object with 3 String variables (title, author, date). I need to implement Comparable (which I have done), then write a compareTo method, and then write a method match(String query) where it uses the compareTo method to check if the title, author or date contains the query.

I'm confused because my understanding of compareTo is taking a variable and comparing its value between two instances of the object, but in my case I don't want to compare two object instances together and I'm looking at more than one variable.

Edit: Thank you all for the answers, it turned out the compareTo was only to display the match results in alphabetical order.

I had been confused as when asking in the class forum about comparing the query in the match method, his response had been to implement Comparable and write compareTo. I'm still not sure why he said that, but after using it for alphabetizing the resulting matches only, the output works and matches his example demo.

CodePudding user response:

You just need to have an method to convert query to album and then use compareTo method. Adjust the convert method to match your requirement.

Here is pseudo code.

Album{
    String title;
    String author;
    Date date;
    ... getter and setter method
    
    public boolean match(String query){
        Album album = convertToAlbum(query);
        return compareTo(album) == 0;
    }

    private Album convertToAlbum(String query){
        Album album = new Album();
        String[] arrs = query.split(";");
        album.setTitle(arrs[0]);
        album.setAuthor(arrs[1]);
        album.setDate(parseDate(arrs[2]));
        return album;
    }

    public int compareTo(Album album){
       // your implement code.
    }
}

In case you don't want to use compareTo, just don't use it.

public boolean match(String query){
            return title.contains(query) || author.contains(query) ...;
        }

CodePudding user response:

There is a difference between Comparable#compareTo and match(String) and what they are trying to achieve.

Comparable defines a means to order a series of objects based on some kind of pre-defined algorithm for the object.

match(String) is trying to determine if one or more of the properties "matches" the defined String

Without any more information on the requirements, compareTo might look something like ...

@Override
public int compareTo(Book other) {
    return getTitle().compareTo(other.getTitle();
}

Where as matches needs to compare the values of each property against the supplied value, something like...

public boolean matches(String query) {
    return getTitle().contains(query) ||
            getAuthor().contains(query) ||
            getDate().contains(query);
}

nb: I'm assuming all the object properties are String

nbb: This is also doing a case sensitive comparison, again "requirements"

CodePudding user response:

The match method for a single obect is like

  boolean match(String query) {
      return title.contains(query) || 
             author.contains(query) || 
             date.contains(query);
  }

You'll need to iterate that over all Albums and return a list of matches.

Meanwhile, the compareTo method is like

  int compareTo(Album other) {
      return title.compareToIgnoreCase(other.title);
  }
  • Related