Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Data structures in Java

Name: OP, the faggot 2012-06-10 7:51

Hey /prog/, I need some orientation!

I am new to Java, learning it since begin of semester. We were given an assignment where we have to simulate a Music Library. Each Artist have Albums, and Albums have Songs. It could be that there are more than one song with the same name but from different artists. My problem is with the complexity of the data administration. We have to parse all this info from .dat files (Album.dat, Artist.dat, Songs.dat) not only with names but with Artist-ID and Album-ID too! I though "yeah, this could be helpful- I can use this as Key in a TreeMap or whatever", thing is, I was thinking of making a HashMap of Artists using Artist-ID as Key, where each Artist contains Albums, and each album contains a Set of Songs, BUT, I know (and understand) it should work the whole way around! Songs contain Albums and Artist, along name Bitrate, etc. Why? Objects look like this:

<code>public class Artist {

    private String name;
    private String style; // Set of strings
    private String website;
}

public class Album {

    private String mainArtist;
    private String name;
    private int numberOfTracks;
    private String label;
}

public class Songs {
   
    private Artist artist;
    private Album album;
    private String titel;
    private int trackNumber;
    private String format;
    private int lenght; // seconds
    private int bitrate; // beats per minute
}</code>

Now, if the data in the .dat files look like this, e.g.:

<code>//File: artist.dat
//Columns: Artist-ID; Name; Style; Website;

1;Vangelis;new age,electronic;-;
2;Jean-Michel Jarre;electronic;http://www.jeanmicheljarre.com/</code>;
<code>//File: albums.dat
//Columns: Album-ID; Artist-ID; Name; Number Tracks; Label;

1;1;Chariots Of Fire;7;Polydor;
2;1; 1492: Conquest of Paradise;12;Atlantic;
3;2;Aero;20;Wea;
4;2;Equinoxe;8;Dreyfus;</code>
<code>//File: songs.dat
//Columns: Artist-ID; Album-ID; Title; Track Number; Format; Length; Bitrate

1;1;Titles;1;mp3;213;192kbps;
2;4; Equinoxe Part 1;1;mp3;143;192kbps;</code>

which Collection(s) should I use
>implying I do have to use both IDs, but maybe you, kind sir have a better idea
and how, please tell me how, can I use the collection(s) in order to store all elements. I thought about ignoring IDs, but as you can see, there is no artist-names in albums.dat and songs.dat. Help please!

Name: Anonymous 2012-06-10 18:20

>>14
Do not heed the advice of >>11

The data model they gave you is strange, but the dependencies between classes are pretty simple:
 artist -> (nothing)
 album -> artist
 song -> artist, album

In other words, to create an Artist you just need to parse the line from the artists.dat file. To create an Album, you need to parse the line from the albums.dat file, and you must have already parsed all the artists, so you can look up the artist-ID for the album.

This means we want to parse artist.dat first, then albums.dat, then finally songs.dat. We will write an initializer method which parses the files in that order. When parsing the first 2 files, we create temporary maps, so we can look up the Artist (or Album) by its ID in the successive steps. The final output is a Collection<Song>.


public static Collection<Song> initialize() {
  // Parse artist.dat
   Map<Integer, Artist> artistMap = new HashMap<Integer, Artist>();
  Scanner sc = new Scanner(new File("artist.dat"));
  while (sc.hasNextLine()) {
    int artistID = sc.nextInt();
    // TODO parse name, styles, and website here
    Artist artist = new Artist(artistID, name, styles, website);
    artistMap.put(artistID, artist);
  }
  sc.close();

  // Parse albums.dat
  Map<Integer, Album> albumMap = new HashMap<Integer, Album>();
  sc = new Scanner(new File("albums.dat"));
  while (sc.hasNextLine()) {
    int albumID = sc.nextInt();
    sc.next(";");
    int artistID = sc.nextInt();
    String albumName = sc.next("[^;]");
    // TODO parse numberOfTracks, label

    // Look up the Artist for this album
    Artist artist = artistMap.get(artistID);
    Album album = new Album(artist.getName(), albumName, numberOfTracks, label);
    albumMap.put(albumID, album);
  }
  sc.close();

  // Parse songs.dat
  Collection<Song> songs = new ArrayList<Song>();
  sc = new Scanner(new File("songs.dat"));
  while(sc.hasNextLine()) {
    int artistID = sc.nextInt();
    sc.next(";");
    int albumID = sc.nextInt();
    // TODO title, trackNumber, format, length, bitrate
    Song song = new Song(artistMap.get(artistID), albumMap.get(albumID), title, trackNumber, format, length, bitrate);
    songs.add(song);
  }

  System.out.println("hax my anus");
  return songs;
}


Writing the songs, albums, and artists to their respective files should be straightforward. It's essentially the same procedure as above, but in reverse order and obviously with write operations rather than reads.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List