English 中文(简体)
Java: CD 图书馆辅导——哪些数据类型储存剪辑、艺术家和歌曲?
原标题:Java: CD Library tutorial - which data type to store cd, artists and songs?

在课后,现在没有接口/校外班:

  1. Test - for creating test data
  2. Music - should store all CDs
  3. CD - should store Artist and CD title + all Songs
  4. Artist - only contains String name
  5. Song - contains default-constructor with (Artist anArtist, String songtitle, String Duration)
  6. Duration - for the song duration and later calculations

i 审判一名ArrayList,但当我加入另一个裁谈会时,它就得过手。 我需要一个数据类型,储存数据,并与另一个阵列连接。 例如:储存CD1的艺术家和所有权,并与包含CD1所有歌曲的其他阵列建立联系。

感谢

班级代码 测试:

Artist aArtist = new Artist("artist1");
        CD aCD = myMusic.addCD(aArtist, "CD1");
        aCD.addSong(aArtist, "song1", "1:00");

aArtist = new Artist("artist2");
        CD aCD = myMusic.addCD(aArtist, "CD2"); //Overwrites arraylist from CD1
        aCD.addSong(aArtist, "song2", "2:00");

i 改变我的阿雷拉主义者的班级音乐和班级CD... i) 认为解决这一理论比较容易,但这里是法典:(输出所有保存的歌曲和CD所有权)。

音乐班:

    import java.util.ArrayList;

public class Music {
    private ArrayList<CD> myCDs = new ArrayList<CD>();

    public CD addCD(Artist anArtist, String cdTitle) {

        CD aCD = new CD(anArtist, cdTitle);
        myCDs.add(aCD);
        return aCD;
    }

    public void showData() {

        for (int i = 0; i < myCDs.size(); i++) {
            for (int j = 0; j < myCDs.get(i).getMyCD().size(); j++) {

                System.out.println(myCDs.get(i).getMyCD().get(j).getSong());
            }
        }

    }
}

CD类:

  import java.util.ArrayList;

public class CD {
    private String cdTitle;
    private ArrayList<Song> myCD = new ArrayList<Song>();

    public CD(Artist aArtist, String cdTitle) {
        setMyCD(aArtist, cdTitle);
    }

    public ArrayList<Song> getMyCD() {
        return myCD;
    }

    public String getMyCDtitle() {
        return cdTitle;
    }

    public void setMyCD(Artist anArtist, String cdTitle) {
        this.cdTitle = anArtist.getName() + ": " + cdTitle;
    }

    public void addSong(Artist anArtist, String aTitle, String aDuration) {

        myCD.add(new Song(anArtist, aTitle, aDuration));

    }
}

中学:

 public class Song {
    private String song;

    public String getSong() {
        return song;
    }

    public void setSong(String song) {
        this.song = song;
    }

    public Song(Artist anArtist, String songtitle, String aDuration) {
        Duration songDuration = new Duration(aDuration);
        setSong(anArtist.getName() + ": " + songtitle + " ("
                + songDuration.getSongDuration() + ")");

    }

}

in general: how would you solve such a project ? i would appreciate every advice or uml diagram.

最佳回答

This may be a problem with aArtist = new Artist("artist2"); CD aCD = myMusic.addCD(aArtist, "CD2"); //Overwrites arraylist from CD1 aCD.addSong(aArtist, "song2", "2:00"); You use the same Artist object. If you post your addCD method implementation it will be more open for us.

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签