Click here to Skip to main content
15,886,075 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to code a DVD java class that has the following attributes:
-- movieTitle(String)
-- yearOfRelease(int)
-- ageClassification (String eg. PG 13)
-- genre (String)
-- playingMinutes (int)

2. The DVD class is to implement the Comparable interface. Comparable is to be implemented so that the DVD's are maintained/sorted in year order, and where the year is the same sort according to title.

3. Code a GUI program that has textfields and button that has functionality. and this this program should store into a text file data in a meaningful format e.g. not in this format dvd@1fv but like this: Titanic 1997 PG13 Romantic drama 90mins

I have coded the DVD class with the attributes now I have a problem :( with implementing Comparable that sorts by year in descending order, then if years the same by movie title.

Note: Use arrayList to store movies.

Please Help.

Thanx for taking your time to read my post.
Posted
Updated 6-Feb-11 2:56am
v2
Comments
Henry Minute 6-Feb-11 8:58am    
What problem are you having in implementing IComparable?

Show us what you have tried and explain how and where it is going wrong. Please remember to surround your code with <pre lang="java"> your code goes here </pre> tags.
Estys 6-Feb-11 8:58am    
What's the problem? Comparing the titles?
NhlanhlaK 6-Feb-11 9:16am    
public int compareTo(Object other) { int result; if (movieTitle.equals(((dvdClass)other).movieTitle)) result = year.compareTo(((dvdClass)other).year); else result = movieTitle.compareTo(((dvdClass)other).movieTitle); return result; } } It can sort my title but im having a problem when sorting by year. Been doing this for 5 days and its frustrating now. PLz help...

1 solution

My interpretation of the sorting criteria:

Java
public int compareTo(Object otherDVD){

    /*
    If passed object is not a DVD, throw Exception.
    */

    if(!(otherDVD instanceof DVD)){
        throw new ClassCastException("Invalid object");
    }

    int oyearOfRelease = ((DVD)otherDVD).yearOfRelease;

    if(this.yearOfRelease() > oyearOfRelease)
        return 1;
    else if ( this.yearOfRelease() < oyearOfRelease )
        return -1;
    else
        return this.movieTitle.compareTo(((DVD)otherDVD).movieTitle);

}


Cheers
 
Share this answer
 
v2
Comments
NhlanhlaK 6-Feb-11 10:26am    
Thanx a billion...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900