Skip to content

Instantly share code, notes, and snippets.

@weihungchin
Last active February 7, 2021 08:24
Show Gist options
  • Save weihungchin/a4b1fa24ca1ac52ef382f3d831533c14 to your computer and use it in GitHub Desktop.
Save weihungchin/a4b1fa24ca1ac52ef382f3d831533c14 to your computer and use it in GitHub Desktop.
class Song {
private String genre;
private String artist;
public Song(String genre, String artist) {
this.genre = genre;
this.artist = artist;
}
// Getter setter
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
// hashcode/equals
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Song that = (Song) o;
if (genre != that.genre)
return false;
return artist != null ?
artist.equals(that.artist) :
that.artist == null;
}
@Override
public int hashCode() {
int result = genre != null ? genre.hashCode() : 0;
result = 31 * result + (artist != null ?
artist.hashCode() : 0);
return result;
}
// toString
@Override
public String toString() {
return "Song{" +
"genre='" + genre + '\'' +
", artist='" + artist +
'}';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment