实现可比并存储到文件的Java程序 [英] Java program that implements comparable and stored to file

查看:51
本文介绍了实现可比并存储到文件的Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个具有以下属性的 DVD java类:
-movieTitle(String)
-yearOfRelease(int)
-ageClassification(字符串,例如PG 13)
-类型(String)
-playingMinutes(int)

2. DVD类用于实现Comparable接口.要实现可比性,以便按年顺序维护/分类DVD,并且根据标题,年份是相同的.

3.编写一个具有文本字段和具有功能的按钮的GUI程序.并且此程序应以有意义的格式将数据存储到文本文件中,例如格式不是dvd@1fv,而是这样的:Titanic 1997 PG13 Romantic drama 90mins

我已经用属性编码了DVD类,现在我遇到了一个问题:(实现了Comparable,它按年份降序排列,如果年份按电影标题相同,则为
.
注意:使用arrayList来存储电影.

请帮忙.

感谢您抽出宝贵的时间阅读我的帖子.

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.

推荐答案

我对排序标准的解释:

My interpretation of the sorting criteria:

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


这篇关于实现可比并存储到文件的Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆