I have created an array of Song objects. A song contains the variables title, author, interpreter, (int)yearReleased, album, and fileName. I am using a main method to test my array of songs and my equals method. The test is supposed to fill an array with five song objects and use my equals method to ensure the new entry is not a duplicate of a previous entry. My test class compiles, but when I enter duplicate song information I keep getting an error. If anyone could give me a tip or point me in the correct direction I would greatly appreciate it. Any other tips would also be great. As a student, it is great to hear good real world advice from professionals.
import java.util.Scanner;
public class Test{
public static void main(String[] args)
{
Scanner kybd = new Scanner(System.in);
Song[] songTest = new Song[5];
boolean match;
int count = 0;
System.out.println("Enter 5 songs\n");
for(Song x:songTest)
{
do{
match = false;
x = new Song();
System.out.print("Title: ");
x.setTitle(kybd.nextLine());
System.out.print("Author: ");
x.setAuthor(kybd.nextLine());
System.out.print("Interpreter: ");
x.setInterpreter(kybd.nextLine());
System.out.print("Year released: ");
x.setYearReleased(kybd.nextInt());
kybd.nextLine();
System.out.print("Album: ");
x.setAlbum(kybd.nextLine());
System.out.print("File name: ");
x.setFileName(kybd.nextLine());
System.out.print(x);
System.out.println();
for(int i = 0; i<count; i++)
if(songTest[i].equals(x)){
match = true;
System.out.print("Duplicate");
}
}while(match);
count++;
}
}
}
public class Song
{
public String title;
public String author;
public String interpreter;
public int yearReleased;
public String album;
public String fileName;
//private vars
private int reviewScore = 0;
private int reviews = 0;
private double average;
//Mutator methods
public void setTitle(String t)
{
this.title = t;
}
public void setAuthor(String a)
{
this.author = a;
}
public void setInterpreter(String i)
{
this.interpreter = i;
}
public void setYearReleased(int y)
{
if (y>0)
this.yearReleased = y;
else
{
System.out.print ("This song is not that old");
this.yearReleased = -5;
}
}
public void setAlbum(String a)
{
this.album = a;
}
public void setFileName(String f)
{
this.fileName = f;
}
public void addReviewScore(int s)
{
if (s>0 && s<6)
{
this.reviewScore += s;
this.reviews++;
}
else
System.out.print("This is not a valid review score!");
}
//Accessor methods
public String getTitle()
{
return this.title;
}
public String getAuthor()
{
return this.author;
}
public String getInterpreter()
{
return this.interpreter;
}
public int getYearReleased()
{
return this.yearReleased;
}
public String getAlbum()
{
return this.album;
}
public String getFileName()
{
return this.fileName;
}
public double getAverage()
{
this.average = this.calculateAverage();
return this.average;
}
//Methods
public boolean equals(Song otherSong)
{
boolean isEqual = false;
//compare this song to the otherSong
isEqual =
this.title == otherSong.title &&
this.author == otherSong.author &&
this.interpreter == otherSong.interpreter &&
this.yearReleased == otherSong.yearReleased &&
this.album == otherSong.album &&
this.fileName == otherSong.fileName;
return isEqual;
}
public String toString()
{
String songInfo;
songInfo =
"***Song information***\n" +
"Title: " + this.title +
"\nAuthor: " + this.author +
"\nInterpreter: " + this.interpreter +
"\nYear Released: " + this.yearReleased +
"\nAlbum: " + this.album +
"\nFile name: " + this.fileName +
"\nYears old: " + this.yearsOld();
return songInfo;
}
public int yearsOld()
{
int yearsOld = (2012 - this.yearReleased);
return yearsOld;
}
//Private methods
private double calculateAverage()
{
this.average = ((double)this.reviewScore/(double)this.reviews);
return this.average;
}
}