简体   繁体   中英

How to automatically add an object to a list of another class?

So I'm currently working on a project for my Java class.

Here is my main:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class Main {

    public static void main(String args[]){
        Entrepot Entrepot1=new Entrepot(3);
        Rangee Rangee1=new Rangee(2);
        Rangee Rangee2=new Rangee(3);
        Rangee Rangee3=new Rangee(3);
        /*for(int i=0;i<Entrepot.getNbRangee();i++) {
            Entrepot.addRangee(Rangee.getID()==i);
        }*/
        for(int i=0; i<Entrepot.getNbRangee(); i++) {
            Entrepot.addRangee(Rangee.getID());
        }
        Meuble meuble = new Meuble("Table",new Lot[]{new Lot(new Vis(200,10),1),new Lot(new Planche(1000,500),3)},"Salon",3);
        meuble.afficherMeuble();
        System.out.println(Rangee1.getID());
        System.out.println(Rangee2.getID());
        System.out.println(Rangee3.getID());
        System.out.println(Entrepot.getNbRangee());
        System.out.println(Entrepot.getListeRangee());

    }
}

Everytime the user creates an Object "Rangee" I want it to be automatically added in the list listeRangee inside the Entrepot class.

As you can see on my Main, I tried a for loop where it adds each Rangee that has the ID of i, that didn't work out.

I'm quite new at this so I'd really appreciate it if you can help me !

Thanks !

Rangee.java

import java.util.ArrayList;
import java.util.List;

public class Rangee {
    private static int count=0;
    private static int ID;
    private int longueur;
    private int largeur=1;
    private int hauteur=1;
    private int volume= longueur*largeur*hauteur;
    private ArrayList<Lot> listeLot= new ArrayList<Lot>();
    
    public Rangee(int longueur) {
        this.longueur=longueur;
        if(ID<Entrepot.getNbRangee()) {
        ID=count++;
        }
        else {
            System.out.println("NON");
        }

    }

    public static int getID() {
        return ID;
    }

    public void setID(int iD) {
        if(ID>Entrepot.getNbRangee()) {
            System.out.println("Le nombre maximum de rangee est de "+ Entrepot.getNbRangee());
        }
        ID = iD;
    }

Entrepot.java

import java.util.ArrayList;
import java.util.List;

public class Entrepot {
    private static int nbRangee;
    static ArrayList<Rangee> listeRangee= new ArrayList<Rangee>(nbRangee);
    
    public Entrepot(int nbRangee) {
        this.nbRangee=nbRangee;
    }
    public static boolean addRangee( Rangee newRangee ) {
              listeRangee.add(newRangee);
              return true;
      }
    
    
    public static int getNbRangee() {
        return nbRangee;
    }
    public void setNbRangee(int nbRangee) {
        this.nbRangee = nbRangee;
    }
    public static ArrayList<Rangee> getListeRangee() {

        return listeRangee;
    }
    public void setListeRangee(ArrayList<Rangee> listeRangee) {
        this.listeRangee = listeRangee;
    }

}

Make Rangee 's constructor private and expose a static factory method to build a new instance of Rangee which will automatically add it to Entrepot .

public Rangee {
  private Rangee(int longueur) {
    this.longueur=longueur;
    if(ID<Entrepot.getNbRangee()) {
      ID=count++;
    } else {
      System.out.println("NON");
    }
  }

  public static Rangee createRangee(int longueur) {
    Rangee rangee = new Rangee(longueur);
    Entrepot.listeRangee.add(rangee);
    return rangee;
  }

You can even move that check if ID is lower than Entrepot.getNbRangee() inside the createRangee method and return null/throw error if that happens instead of printing a message.

Then in your main method:

Rangee rangee1 = Rangee.createRangee(2); // was Rangee Rangee1=new Rangee(2);
Rangee rangee2 = Rangee.createRangee(3); // was Rangee Rangee2=new Rangee(3);
Rangee rangee3 = Rangee.createRangee(3); // was Rangee Rangee3=new Rangee(3);

Everything else stays the same.

Note that createRangee can be created in Entrepot instead of Rangee , but Rangee 's constructor will have to be accessible only to Entrepot . This means: 1/ making the constructor with default access(no public/private) and put both classes in the same package (other classes in the same package can still create Rangee instances) or 2/ move Rangee class inside Entrepot as static class and keep the constructor private. With all that said, here is how I'd implement it in Entrepot with package-private Rangee constructor:

class Entrepot {
    private int nbRangee;
    private List<Rangee> listeRangee = new ArrayList<>(nbRangee);

    public Entrepot(int nbRangee) {
        this.nbRangee=nbRangee;
    }

    public int getNbRangee() {
        return nbRangee;
    }
    public void setNbRangee(int nbRangee) {
        this.nbRangee = nbRangee;
    }
    public List<Rangee> getListeRangee() {
        return listeRangee;
    }
    public void setListeRangee(ArrayList<Rangee> listeRangee) {
        this.listeRangee = listeRangee;
    }

    public Rangee createRangee(int longueur) {
        if (listeRangee.size() < nbRangee) {
            Rangee rangee = new Rangee(longueur);
            listeRangee.add(rangee);
            return rangee;
        } else {
            System.out.println("NON");
            return null;
        }
    }
}

Note that I have removed a few statics here and there, since they make more sense now. Note that you could set the rangee 's id to listeRangee.size() inside createRangee . Your previous implementation updated a static variable and would have printed 3 for all your 3 rangees getId()`, which I suspect it isn't what you want.

Main method would be:

class Main {
    public static void main(String[] args) {
        Entrepot entrepot = new Entrepot(3);
        Rangee rangee1 = entrepot.createRangee(2);
        Rangee rangee2 = entrepot.createRangee(2);
        Rangee rangee3 = entrepot.createRangee(3);
    }
}

The easiest way I imagine to do this is to use the constructor of Rangee indirectly as a class method of Entrepot.

Inside the Entrepot class you can add this (you could do it with parameters if you need to):

        public void createRangee(){
            Rangee aux = new Rangee();
            listeRangee.add(newRangee);
        }

Then in your main you wouldn't have to call the constructor, but instead this method:

Entrepot ntrepot1=new Entrepot(3);
entrepot1.createRangee(); // The number of times you need

As for the id of each you could use this solution:

private static AtomicInteger ID_GENERATOR = new AtomicInteger(1000);

Rangee aux = new Rangee(ID_GENERATOR.getAndIncrement());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM