mercredi 23 septembre 2020

compare between two arrays of objects- java

I have method in class which create instace of Locker object, and add it to an array. When I try to test the method and check the array I get, I failed, beacuse it created another instance of object. This is the method

public class SpaceShip {
  private final String shipName;
    private final int[] IDs;
    private final Locker[] LockersLst;
    private final Item[][] constrainsLst;
    private LongTermStorage longTerm=new LongTermStorage();
    private final Map<Integer, Boolean> map_of_Id_lockers= new HashMap<>();

    public class SpaceShip {
            private LongTermStorage longTerm=new LongTermStorage();
           public SpaceShip(String name, int[] crewIDs, int numOfLockers, Item[]
[] constrains) {
        shipName=name;
        IDs=crewIDs;
        constrainsLst=constrains;
        LockersLst=new Locker[numOfLockers];
    for (int id : IDs) {
        map_of_Id_lockers.put(id, false);

     public int createLocker(int crewID, int capacity) {
        for(int id: IDs){
            if(id==crewID &&!map_of_Id_lockers.get(crewID)){

                /** check capacity valid*/

                if (capacity>=0){

                    /** check empty locker*/
                    for (int i=0; i<LockersLst.length; i++){
                        if (LockersLst[i] != null) {
                            continue;
                        }
                        LockersLst[i]=new Locker(longTerm, capacity, constrainsLst);
                        map_of_Id_lockers.replace(crewID, true);
                        return 0;
                    }

and this is the test I tried:

 public void testGetLockers() {
    /** full capacity*/
    LongTermStorage longterm=new LongTermStorage();
    SpaceShip spaceship1=new SpaceShip("space1", crewIDs1,3, constrains);
    Locker locker1=new Locker(longterm,200,constrains);
    Locker locker2=new Locker(longterm,0,constrains);
    Locker locker3=new Locker(longterm,200,constrains);

    Locker[] lockerLst1 = new Locker[]{locker1, locker2, locker3};
    spaceship1.createLocker(1,200);
    spaceship1.createLocker(2, 0);
    spaceship1.createLocker(3, 200);
    Assert.assertArrayEquals( lockerLst1, spaceship1.getLockers());

what is the best ways to test it?

Aucun commentaire:

Enregistrer un commentaire