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

Pages: 1-

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: OP, the faggot 2012-06-10 8:05

Oh, BBcode? My bad

[Java]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
}[/Java]

Name: OP, the faggot 2012-06-10 8:09

Well, fuck me

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
}

Name: Anonymous 2012-06-10 8:30

-Knock knock
-Who's there?

.
.
.
































































.
.
.



























-Java

Name: OP, the faggot 2012-06-10 9:01

>>4

I don't get it.

Heeeeeeeeelp!

Name: Anonymous 2012-06-10 10:06

Why is your assignment to build a poor man's database in Java?

Name: Anonymous 2012-06-10 10:08

1. decide on the field(s) that are keys, for instance name being key of artist
2. use hashmap/hashtable for each type of things
3. learn another language

Name: Anonymous 2012-06-10 10:13

private int numberOfTracks;

Why? Why not simply have something like an ArrayList to hold all of the songs and a method which calculates the length of that ArrayList?

Name: OP, the faggot 2012-06-10 11:26

>>8

Because the Library can have incomplete Albums, like album xyz with 12 songs, but Library has only 5 songs of it.

>>7
I am thinking about something like
HashMap library = new HashMap<Artist, List<Songs>>
but still I don't know how to add the elements from the different .dat files.

Can someone please come with a method for it? pseudo-code will be enough!

>>6
Well, what else can you do with Collections anyway?

Name: Decryptic 2012-06-10 12:16

It makes more sense to have the class Song, and all of the other things--i.e. genre, album, artist--as properties of that class.  So:

public class Song
{
    public enum Genre { Rock, Metal, Pop, Britpop, Alt }
    public Genre MyGenre;
    public string Title;
    public string Artist;
    public string Album;

    public Song()
    {

    }

    public Song(string title, Genre genre, string artist, string album)
    {
        Title = title; MyGenre = genre, Artist = artist, Album = album;
    }
}


So that way you could do:

Song song1 = new Song("Come Together", Song.Genre.Britpop, "Beetles", "Abbey Road");

Then you could add all the misc. properties in there.  And to be more organized, you should put a static class for misc. properties within your Song class.

Name: Anonymous 2012-06-10 12:23

Use SQLite for the database, here is a JDBC driver for sqlite written in Java
http://www.zentus.com/sqlitejdbc/

You will need to read ID3 tags, here's a library for that
http://javamusictag.sourceforge.net/

hth

Name: Anonymous 2012-06-10 13:01

>>10

Thanks, your idea makes totally sense but the classes/objects are preset.


>>11
Thank you! I going to check this out.

Name: Anonymous 2012-06-10 14:26

OO
shit

Name: OP, the faggot 2012-06-10 14:54

>>11
Well shit, nothing I can use from there. Everything is pretty different of what I am trying to implement :-/

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.

Name: OP, the faggot 2012-06-10 19:18

>>15

OH MAI GAH! I can totally work with this. Is pretty close from what I have being writing. It needs some work but I get the idea. Thank you very much, kind sir.

System.out.println("hax my anus");  

I'm totally letting this in if got this to work before going to sleep! Fuck my teacher!

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