14 Replies - 578 Views - Last Post: 31 October 2018 - 06:55 PM Rate Topic: -----

#1 robgeek   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 153
  • Joined: 15-January 13

Question about heritage.

Posted 27 October 2018 - 10:19 AM

Hi!
I'm trying to create a library of artists where one artist has one or more albums and each album has one or more musics.
So I created the following classes and in my main method given a music I try to get the name of the album in wich its belongs and the author(Artist).
And I do that, but when I try to get the number of fans of the artist I get always 0.

I think is because the music object is not the same as artist even the music object has an artist. But how can i fix that?

I think I could do that passing this number(number of fans) using constructor. But if I have many other attributes in Artist as well as Album.
Would be a huge amount of arguments in the Music contructor.

Maybe i created these classes wrong.
What do you think?

import java.util.ArrayList;

public class Artist {
	private String name;
	private int numFans;
	private ArrayList<Album> discography;

	public Artist(String name) {this.name = name;}

	public String getArtistName() {return name;}

	public int getFansArtist() {return numFans;}

	public ArrayList<Album> getDiscography() {return discography;}

	public void setNumFansArtist(int numFans) {this.numFans = numFans;}

	public void addAlbum(Album album) {
		if(discography == null)
			discography = new ArrayList<>();

		discography.add(album);
	}
}

import java.util.ArrayList;

public class Album extends Artist {
	private String title;
	private int numFans;
	private ArrayList<Music> songs;

	public Album(String title, String nameArtist) {
		super(nameArtist);
		this.title = title;
	}

	public String getTitleAlbum() {return title;}

	public int getFansAlbum() {return numFans;}

	public ArrayList<Music> getSongs() {return songs;}

	public void setNumFansAlbum(int numFans) {this.numFans = numFans;}

	public void addSong(Music music) {
		if(songs == null)
			songs = new ArrayList<>();

		songs.add(music);
	}
}

public class Music extends Album {
	private String title;
	private double time;

	public Music(String title, String nameAlbum, String nameArtist, double time) {
		super(nameAlbum, nameArtist);
		this.title = title;
		this.time = time;
	}

	public String getTitleMusic() {return title;}

	public double getTime() {return time;}

	public void setTime(double time) {this.time = time;}
}

import java.util.ArrayList;
import java.util.Random;

public class Catalog {
	private final int total = 15;
	private ArrayList<Artist> catalog;

	public Catalog() {
		catalog = new ArrayList<>();
		createArtists();
	}

	public ArrayList<Artist> getCatalog() {return catalog;}

	private void createArtists() {
		Random r = new Random();

		for(int i = 0; i < total; i++) {
			Artist artist = new Artist("Artist " + i);
			artist.setNumFansArtist(r.nextInt(1000000));
			createAlbums(artist);
			catalog.add(artist);
		}

	}

	private void createAlbums(Artist artist) {
		Random r = new Random();

		for(int i = 0; i < total; i++) {
			Album album = new Album(("Album " + i), artist.getArtistName());
			album.setNumFansAlbum(r.nextInt(1000000));
			createSongs(album);
			artist.addAlbum(album);
		}

	}

	private void createSongs(Album album) {
		
		for(int i = 0; i < total; i++) {
			Music music = new Music(("Music " + i), album.getTitleAlbum(), album.getArtistName(), 5);
			album.addSong(music);
		}

	}
}

public class Main {
	public static void main(String[] args) {
		Catalog catalog = new Catalog();

		// Just to test
		Artist artist = catalog.getCatalog().get(2);
		Album album = artist.getDiscography().get(1);
		Music music = album.getSongs().get(4);

		System.out.println("Song:   " + music.getTitleMusic());
		System.out.println("Album:  " + music.getTitleAlbum());
		System.out.println("Artist: " + music.getArtistName());
		System.out.println("Music  - Artist fans: " + music.getFansArtist());

		System.out.println("Artist - Artist fans: " + artist.getFansArtist());
	}
}

Quote

[[email protected] Test]$ java Main
Song: Music 4
Album: Album 1
Artist: Artist 2
Music - Artist fans: 0
Artist - Artist fans: 315431

This post has been edited by robgeek: 27 October 2018 - 10:22 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Question about heritage.

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14490
  • View blog
  • Posts: 58,094
  • Joined: 12-June 08

Re: Question about heritage.

Posted 27 October 2018 - 10:28 AM

An aside - Albums have _songs_ or _tracks_ not musics. You would also probably want to keep track of the track number.

You should think where you are tracking the fans. Do you want that in the album level.. or artist level. Album level may have multiple same people liking albums so adding them up for artist doesn't make sense.
Was This Post Helpful? 1
  • +
  • -

#3 robgeek   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 153
  • Joined: 15-January 13

Re: Question about heritage.

Posted 27 October 2018 - 02:32 PM

Quote

Album level may have multiple same people liking albums so adding them up for artist doesn't make sense.


You're probably right. I will remove this attribute from Artist. But thats not the case. I'm I implementing catalog wrong or I the classes are bad designed? How can I fix that output?
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3599
  • View blog
  • Posts: 16,456
  • Joined: 20-September 08

Re: Question about heritage.

Posted 27 October 2018 - 02:55 PM

Quote

public class Album extends Artist 

In what possible way is an album an artist?
Was This Post Helpful? 1
  • +
  • -

#5 robgeek   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 153
  • Joined: 15-January 13

Re: Question about heritage.

Posted 27 October 2018 - 03:45 PM

Quote

In what possible way is an album an artist?


Well, the idea was that an album has to be from an artist and the same thing with a music to an album.

This post has been edited by robgeek: 27 October 2018 - 03:46 PM

Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3599
  • View blog
  • Posts: 16,456
  • Joined: 20-September 08

Re: Question about heritage.

Posted 28 October 2018 - 01:29 AM

No. The relation between Artist and Album is has a..
Artist has 0..n Album

Music seems to be over-abstract to no good effect. Just Song might do, or Track. Actually the latter is normally used of albums
Was This Post Helpful? 1
  • +
  • -

#7 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1547
  • View blog
  • Posts: 4,925
  • Joined: 20-March 10

Re: Question about heritage.

Posted 28 October 2018 - 03:36 AM

I don't think Inheritance is the correct solution here look at 'implements' instead, making Artist and Tracks 'interfaces' might be more logically sound.
Was This Post Helpful? 2
  • +
  • -

#8 MentalFloss   User is online

  • .
  • member icon

Reputation: 591
  • View blog
  • Posts: 1,545
  • Joined: 02-September 09

Re: Question about heritage.

Posted 28 October 2018 - 11:07 AM

I think it's more like an Album has 1..n Artists because there are collaborations, and there has to be at least one artist. For example, consider Slash's album: https://en.wikipedia...)#Track_listing

Every song has a different vocalist, and they are all artists. So, the entire album could be considered to have 13+slash=14 artists on one album.

So, the Album might contain a collection of Artists, but then which artist goes to which track? Instead, An album should contain a collection of Tracks, and a Track should have a collection of Artists.

Album HAS Tracks HAS Artists. Nothing IS anything else.

EDIT:

Actually, I would introduce Group or Band or something. These would be core members attributed to an album for every track. Album HAS Band and a Band HAS Artists. Then each Track HAS Artists as well - these are the one-off collaborators, etc.

EDIT2:

Discography would have Albums. This is inverted from what you have.

To summarize:

Discography
    List<Album> albums
    details

Album
    Band band;
    List<Track> tracks;
    details

Band
    List<Artist> members;
    details

Track
    List<Artist> collaborators;
    details

Artist
    details




where details is just other attributes specific to that class. There really seems to be no inheritance in this problem.

This post has been edited by MentalFloss: 28 October 2018 - 11:22 AM

Was This Post Helpful? 2
  • +
  • -

#9 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3599
  • View blog
  • Posts: 16,456
  • Joined: 20-September 08

Re: Question about heritage.

Posted 28 October 2018 - 04:13 PM

Quote

I think it's more like an Album has 1..n Artists because there are collaborations

Yes, there's that too ;)
Was This Post Helpful? 1
  • +
  • -

#10 robgeek   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 153
  • Joined: 15-January 13

Re: Question about heritage.

Posted 28 October 2018 - 06:08 PM

Thanks for yout help!

I just dont understand how could I implement that using interfaces like snoopy11 said.
Was This Post Helpful? 0
  • +
  • -

#11 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3599
  • View blog
  • Posts: 16,456
  • Joined: 20-September 08

Re: Question about heritage.

Posted 29 October 2018 - 06:38 AM

I wouldn't personally use interfaces. Interfaces are for when things do stuff. You have a situation where things are stuff mainly.

Have said that, you need to design your software on what you want it to do. For instance, it might not be necessary to model the relationship from Album to Artist so that would simplify your object relationships.

A piece of software is written to accomplish tasks. What are your tasks?
Was This Post Helpful? 2
  • +
  • -

#12 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11267
  • View blog
  • Posts: 19,308
  • Joined: 19-March 11

Re: Question about heritage.

Posted 29 October 2018 - 10:23 AM

*
POPULAR

View Postg00se, on 28 October 2018 - 03:29 AM, said:

Music seems to be over-abstract to no good effect. Just Song might do, or Track. Actually the latter is normally used of albums


Artist <- Album <- Track is a reasonable structure (an artist has [0..many] albums, an album has [0..many] tracks)
However, this assumes that a track is on an album, which might not make sense in today's world. People release tracks without albums, and forcing them to associate a track with an album might not accord with their intention.
A more subtle issue, which plagues most music players, is that a track can appear on multiple albums - for example, it shows up on the original release, on the single, on the EP, on the greatest hits collection. Also, a different track with the same name shows up on the live album and maybe on the fan bootleg, and a similar track shows up as a remix somewhere - and I'm sure we can come up with other cases, for example a different version of the same song appears on some other artist's album as a cover tune, or the track shows up in an anthology collection of multiple artists, etc., etc.

So we're into a data modeling problem, and it's probably worth spending a little while thinking carefully about exactly what parts of this problem matter to you, and which parts you're willing to get wrong.

For example, most music players are completely incapable of linking two different versions of the same song, which is understandable (it's not obvious how that would be executed) but also frustrating to the listener who wants to find, say, someone else's cover of a certain Bob Dylan song, knowing only that the song is Tombstone Blues. They generally can go by the title, but that's bothersome if another song exists with the same title. This gets you into partial-match problems, for example someone looking for "Let It Be" by the Beatles will likely find themselves having to filter out "Let It Be Me" by the Indigo Girls.
If this is a problem you care about, you have to build your data model to address it. If you decide not to care about this problem, it's best to make that an intentional decision rather than an accidental failure of foresight.
Was This Post Helpful? 6
  • +
  • -

#13 robgeek   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 153
  • Joined: 15-January 13

Re: Question about heritage.

Posted 30 October 2018 - 07:27 AM

People.
Thank you very much for your help. I have to say that you have extrapolated the problem a little. But ok you guys tried to help. The problem was as simple as the one I originally posted. We have to make a music player simulator. The app will not actually play a song, but we have to simulate that.

So we have:
SearchActivity: Search where you have to type something. Could be an artist, album or song.

ArtistActivity: Artist info visualization.
It shows some information about the artist: fans, number of albums, year they started to play and the list of all albums.

AlbumActivity: Album info visualization.
When you click on an album and then you see the name of the artist all tracks and some other infos: year, fans(here too I don't know why), number of tracks.

PlayerActivity: The player itself.
You see the artist, album, name of track, time, year(probably the year of the album)...

As you can see in player visualization I have to show informations not only about the song but the album and artist too. And the album visualization I have to show some artist information as well. Thats why I tried originally heritage design. Because when I send an Artist object through Intent to player activity by heritage I could know which album it belongs to and which artist. I cannot send more than one object.

I posted here because is a java design class problem not a android problem.

Here other sollution I thought:

Artist
	ArrayList<Album> albums
	//details

Album
	Artist artist
	ArrayList<Track> tracks
	//details

Track
	Album album
	//details


I just don't know if these "linked" references are a good solution or I should avoid things like that.
Was This Post Helpful? 0
  • +
  • -

#14 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1547
  • View blog
  • Posts: 4,925
  • Joined: 20-March 10

Re: Question about heritage.

Posted 30 October 2018 - 03:16 PM

Having looked a this in more detail,

perhaps designing it using classes within classes would be more beneficial.

Class List
Artist,
Track,
Album,
Catalog

You could add a Player class on to the end of that,

The Player Class would utilize a Catalog Class,

The Catalog would be little more than a collection of Albums.

An Album would contain an Artist class and a collection of Tracks.

It all hinges around the Album class anyway so one possible design of such a class could be,

class Album {

    private String AlbumTitle;
    private List<Track> Tracks;
    private final Artist artist;
    private int numberOfTracks;

    Album(String AlbumTitle, Artist artist, List<Track> Tracks, int numberOfTracks) {
        this.Tracks = new ArrayList<>();
        this.AlbumTitle = AlbumTitle;
        this.artist = artist;
        this.Tracks = Tracks;
        this.numberOfTracks = numberOfTracks;
    }

    Album() {
        this.Tracks = new ArrayList<>();
        this.artist = new Artist();
    }

    public String getName() {
        return this.AlbumTitle;
    }

    public String getArtist() {
        return this.artist.getName();
    }

    public void setArtistName(String Name) {
        this.artist.setName(Name);
    }

    int searchForTrackName(String TrackName, int Index) {
        while (Index < numberOfTracks) {
            if (this.Tracks.get(Index).getName().toLowerCase().contains(TrackName.toLowerCase())) {
                return this.Tracks.get(Index).getAlbumTrackNumber();
            }
            Index++;
            if (Index == numberOfTracks) {
                break;
            }
        }

        return 0;
    }

    String getAlbumTrackName(int index) {
        return Tracks.get(index).getName();
    }

    void setAlbumTrackName(String Name, int index) {
        Track track = new Track(Name, index);
        track.setAlbumTrackNumber(index);
        Tracks.add(track);
    }

    void setAlbumName(String Name) {
        AlbumTitle = Name;
    }

    String getAlbumTrackNumber(int index) {
        return String.valueOf(Tracks.get(index).getAlbumTrackNumber());
    }

    int getNumberOfTracks() {
        return numberOfTracks;
    }

    void setNumberOfTracks(int number) {
        numberOfTracks = number;
    }

    String getArtistFans() {
        return String.valueOf(artist.getFans());
    }

    void setArtistFans(int numFans) {
        artist.setFans(numFans);
    }
}



Adding functions in the Catalog class to shadow the Album class would be beneficial.

This post has been edited by macosxnerd101: 30 October 2018 - 06:47 PM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

#15 robgeek   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 153
  • Joined: 15-January 13

Re: Question about heritage.

Posted 31 October 2018 - 06:55 PM

Ok. Thanks for your suggestion snoopy11. I'll try your way.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1