How to modify add of ArrayList

社会主义新天地 提交于 2020-01-06 14:44:12

问题


I'm trying to modify an "add" method such that it checks if a BinItem's SKU are the same. If it's unique, I will add it. If it has similar SKU as others, it will remove the BinItem, create a new one (of the same SKU) but sum up the Quantities.

I tried my best but couldn't get my head around it... here's my effort. I tried making a nested for loop so it goes through each one, check if they're the same and delete if they are. It ended up deleting everything.

public class Bin
{
   private String myName;
   private ArrayList<BinItem> myContents; 

   public Bin ( String name )  
   {  
      myName = name; 
      myContents = new ArrayList <BinItem>();  
   }

   public ArrayList <BinItem> getContents()  
   {
      return myContents; 
   }

   public String getName() 
   { 
      return myName;  
   }


  // Define the remove and totalQuantity methods and redefine the add method here

   public void remove (int b)
   {
      myContents.remove(b);
   }

   public int totalQuantity()
   {
      int x = 0;
      for (int i = 0; i< myContents.size(); i++)
      {
          x += myContents.get(i).getQuantity();
      }
      return x;
   }

   public void add (BinItem b)
   {
     for (int i = 0; i < myContents.size(); i++)
     {
         for (int x = 0; x < myContents.size(); x++)
         {
             if (!(myContents.get(i).getSKU().equals(myContents.get(x).getSKU())))
             myContents.add(b); 
             else 
             myContents.remove(b);
         }
     } 
   }

   public String toString() 
   {
      String s = "Bin " + myName + ":\n";
      for ( BinItem b : myContents ) 
          s += b + "\n"; 
      return s;
   }
}

This is the BinItem class

public class BinItem
{
   private String mySKU; 
   private int myQuantity;

   public BinItem ( String sku, int quantity)   
   {   
       mySKU = sku;   
       myQuantity = quantity;   
   }

   public String getSKU()   
   {    
       return mySKU;  
   }

   public int getQuantity()  
   {  
       return myQuantity;  
   }    

   public String toString()  
   {  
       return "SKU " + getSKU() + ": " + getQuantity();  
   }
}

And here's the main class

public static void main( String[] args )
{
    Bin bin = new Bin( "A" );
    bin.add( new BinItem( "1234-0", 500 ) );
    bin.add( new BinItem( "1234-1", 25 ) );
    bin.add( new BinItem( "1234-0", 243 ) );
    bin.add( new BinItem( "1234-2", 7720 ) );
    bin.add( new BinItem( "1234-0", 871 ) );
    System.out.println( bin );
}

I'm trying to make it so that it will return

Bin A:
SKU 1234-1: 25
SKU 1234-2: 7720
SKU 1234-0: 1614

回答1:


If all you want to do is check if the list contains a BinItem with a certain sku, you can override the equals method in side of your BinItem class.

public override boolean equals(Object obj){
    if(!(obj instanceof Bin)) return false;
    Bin b = (Bin) obj;
    return b.getSKU().equals( getSKU());
}

You will want to check if obj is a Bin object otherwise you will receive an error if the passed obj isn't of type Bin. Then in your add method for the Bin class you would check if the list contains the object by using myContents.contains( newObject)
If the list contains the bin item already then it will return true. at this point you can get the quantity of the item that is already in the list and then add to it the quantity of the new object. There's no need to remove the item and create a new one, if all you are changing is the quantity.
Just add a addQuantity method and pass in an int; Like so:

public void addQuantity(int num){
    myQuantity += num;
}

To get the object that is alrady in the list you can use indexOf(obj) to get the index of it and then retrieve the object using get and then just increment the quantity by the given amount.
In fact you don't even need to use .contains(), since if the object doesn't exist in the list it will return -1 which you can check against like so:

int index = myContents.indexOf( b );
if(index == -1){
    //Handle adding the item to the list.
}else {
    BinItem bi = myContents.get(index);
    bi.addQuantity( b.getQuantity() );
}

Hope this helped you out.




回答2:


In your BinItem class you can override equals() method

public boolean equals(Object obj) {
    BinItem b = (BinItem) obj ;
    return b.getSKU() == obj.getSKU(); 
}

and then check for the list containing your item using public boolean contains(Object o)

 public void add (BinItem b)
 {

    if (!myContents.contains(b))
      myContents.add(b);
    else { //Edit : Based on Anthony Forloney's comment
      // You should handle the quantity sum here based on your logic
      myContents.remove(b);
      myContents.add(b);
    }
 }


来源:https://stackoverflow.com/questions/27680834/how-to-modify-add-of-arraylist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!