0

Q: How do I keep track of which ArrayList element belongs to which element in a JTable for when I later have to put it all together into one single ArrayList?

DETAILED:

I have an interface Media, and 3 classes implementing it: Book, Movie, Album. Album has an ArrayList of Songs(also a class I made, obviously).

I made a JTable as per usual with Object[][] where I can put in Strings, ints and such for the time being and the user can add rows, remove rows, edit the cells, and then later I planned on going through the data and making each thing specified in the JTable and putting them into an ArrayList called ListOne of type Media so I can use ObjectOutputStream and save the List(yes, this is a demand in the assignment, I would've kept it simple personally).

But the problem is Songs are an ArrayList in the class Album, and so I made another JFrame with a JTable where one can similarly fill that one out, and then I thought I would store all of the songs the user provides into a temporary Album-object. But where do I go from here? I can't keep track of them like this if the user deletes the corresponding Album in the first JTable.

If I store these Albums in an ArrayList called ListTwo, and if the user decides to delete no.6 in ListOne, and no.6 happens to be an Album, I have to know which one to delete in ListTwo now and make sure the others' ID or key isn't affected as a result because I will need to pair them properly later. I need to know which Album's songs in ListTwo to transfer to which of the Albums in ListOne so I can save it later.

So how would I go about keeping track of this and connecting them later?

Preliminary code:

Media interface class:

public interface Media {
    void setAuthor(String author);
    String getAuthor();
}

Book class:

public class Book implements Media {

    private String author;

    Book(){
        this.author = "";

    }

    @Override
    public void setAuthor(String author) {
        this.author = author;
    }
    @Override
    public String getAuthor() {
        return this.author;
    }

}

CD(Album) class:

import java.util.ArrayList;

public class CD implements Media {

    public class Song {
        private int seconds;
        private String songName;
        private String artist;

        Song(String name, int seconds, String artist){
            this.songName = name;
            this.seconds = seconds;
            this.artist = artist;
        }

        public String getName(){
            return this.songName;
        }
        public void setName(String name){
            this.songName = name;
        }
    }

    private ArrayList<Song> songs;

    private String artist;

    CD(){
        songs = new ArrayList<Song>();
        this.artist = "";
    }

    public void addSong(String name, int seconds, String artist){
        this.songs.add(new Song(name, seconds, artist));
    }
    public void removeSong(int position){
        this.songs.remove(position);
    }

    @Override
    public void setAuthor(String author){
        this.artist = author;
    }

    @Override
    public String getAuthor(){
        return this.artist;
    }
}

Movie class:

public class Movie implements Media {

    private String producer;

    Movie(){
        this.producer = "";
    }

    @Override
    public void setAuthor(String author){
        this.producer = author;
    }
    @Override
    public String getAuthor(){
        return this.producer;
    }
}

TABLE: http://pastebin.com/ETJGuQhD

Jack Of Blades
  • 495
  • 7
  • 16
  • Welcome to Stack Overflow! We are a question-and-answer site, not a coders-for-hire service. Please narrow your question down to a specific problem that would be on-topic for this site. See: [Why is "Can someone help me?" not an actual question?](http://meta.stackoverflow.com/q/284236) and [How to ask a good question when I'm not sure what I'm looking for?](https://meta.stackoverflow.com/questions/262527/how-to-ask-a-good-question-when-im-not-sure-what-im-looking-for) – Joe C Mar 12 '17 at 10:15
  • I asked how to keep track of two arraylists simultaneously. I don't know how to word it better than this. – Jack Of Blades Mar 12 '17 at 10:17
  • Don't describe your code. Post it. – JB Nizet Mar 12 '17 at 10:33
  • Don't post your code. Post a [MCVE] – c0der Mar 12 '17 at 10:37
  • I'm working on it, it'll take a while. – Jack Of Blades Mar 12 '17 at 10:41
  • I put some up so you can sort of see the idea behind it, I don't have time to do the entire example code in detail and if I cut more it won't get everything in that needs to show. When edit album is clicked, another frame opens where the table should be where you add songs. Then it's supposed to behave in the same way, and save the songs, which are stored in an Album-object and that is supposed to be mapped to the selected Album-object in the first frame's table so that I can later save it to a Media-ArrayList. – Jack Of Blades Mar 12 '17 at 11:12
  • 1
    This seems like a rather broad design problem; you might get some insight from this [Q&A](http://stackoverflow.com/q/31909941/230513). – trashgod Mar 12 '17 at 12:14

1 Answers1

0

My solution was to make 3 Add-buttons instead of one - one for each type of object that can be added to the final ArrayList. Then I made three separate add-functions, one for each button, and in each I not only add a row with a specific type already chosen, I also add an object of that type directly to the final ArrayList. Then I use the table.getRowSelection() as part of the createTable-function to pick out which one in the ArrayList to alter when I finish the list of Songs. After which the songs are directly stored in the Album object pointed out.

This way I can be sure that everything is in the right place, even though it is kinda clunky. But I'd rather finish on time than not finish on time.

Jack Of Blades
  • 495
  • 7
  • 16