Program structure, how to relate these two classes

0

On the one hand I have the Subject class:

public class Assignatura{
    private int codi;
    private Assignatura seguent;

    public Assignatura(int c){
        codi=c;
        seguent=null;
    }

As you can see, the following attribute will point to another subject, in order to have a dynamic structure. The same goes for the Course class:

public class Curs {
    private int codi;
    private Curs seguent;       

    public Curs(int c, llista_assignatura_curs ll){
        codi=c;
        seguent=null;
    }

My goal now is that a course has a list of subjects, for this I created the class 'llista_asignatura_curs':

public class llista_assignatura_curs {
private Assignatura primera;


public llista_assignatura_curs(){
    primera=null;
}

This class has methods to add and remove subjects from the list. The problem is that now. I need to know how to link a list to a course. Would it be adding an attribute to the class class of the type 'llista_assignatura_curs' the solution? Or is there another better way? Thanks

PS: this would be my proposal, but I think it can be done better

public class Curs {
    private int codi;
    private Curs seguent;
    private llista_assignatura_curs llista;

    public Curs(int c, llista_assignatura_curs ll){
        codi=c;
        seguent=null;
        llista= ll;
    }

Thanks for the help

    
asked by pituofevil 10.12.2017 в 20:03
source

2 answers

0

If I have not understood correctly, what you need is that the Course class contains a list of subjects. You can do it in two ways:

If you need the class llista_assignatura_curs , you already do it well, but you will have to create a list of Subjects because right now you are only saving one subject per list:

public class llista_assignatura_curs {
    private ArrayList<Assignatura> lista = new ArrayList();

If not, you can also directly create in the class Course, the list of subjects, and in the class Course have the methods to add, delete, modify subjects:

public class Curs {
    private ArrayList<Assignatura> lista = new ArrayList();
    
answered by 10.12.2017 в 20:47
0

The solution to your problem is to add an attribute of type subject in course, like this:

public class Curs {
    private int codi;
    private Curs seguent;       
    private Assignatura asignatura;
    public Curs(int c, llista_assignatura_curs ll){
        codi=c;
        seguent=null;
        asignatura = null;
    }
}

You should bear in mind that you are simulating the operation of a list of lists and because you are handling reflexive relationships, using recursion will make your job much easier.

    
answered by 11.12.2017 в 06:23