如何修改ArrayList的toString方法

时间:2014-12-29 20:16:53

标签: java arraylist tostring

我正在尝试修改Warehouse类中的toString方法,以便它返回

Bin B0:
Dress Shoe - Loafer (size 10-1/2), SKU 1234-13: 5
Boot - Riding (size 8), SKU 1234-5: 5
Bin B1:
Bin B2:
Dress Shoe - Wing-tip (size 10), SKU 1234-3: 10
Bin B3:
Casual Shoe - Sandal (size 9-1/2), SKU 1234-0: 8
Bin B4:

来自我的主要课程

public static void main( String[] args )
{
  Warehouse w = new Warehouse( 10 );
  Footwear d0 = new DressShoe( "Loafer", 10.5, "1234-13" );
  Footwear b = new Boot( "Riding", 8, "1234-5" );
  Footwear c = new CasualShoe( "Sandal", 9.5, "1234-0" );
  Footwear d1 = new DressShoe( "Wing-tip", 10, "1234-3" );
  w.stockWarehouse( d0, 5, "B0" );
  w.stockWarehouse( b, 5, "B0" );
  w.stockWarehouse( c, 8, "B3" );
  w.stockWarehouse( d1, 10, "B2" );
  System.out.println( w.toString() );
}

stockWarehouse方法是“隐藏”而未给出。 DressShoe,Boot,CasualShoe和DressShoe是鞋类的延伸类。

Footwear是一个采用String样式,双倍大小和String SKU的类。它的toString方法返回鞋子信息,例如:

Dress Shoe - Loafer (size 10-1/2), SKU 1234-13

Bin是一个接受String名称并创建内容ArrayList的类,其toString方法返回Bin编号和内容,如:

[SKU 1234-13: 5, SKU 1234-13: 5]

这是我的仓库类。

public class Warehouse {
    private int myBinMax;
    private ArrayList<Footwear> myCatalog;
    private ArrayList<Bin> myBins;

    public Warehouse (int binMax)
    {
        myBinMax = binMax;
        myCatalog = new ArrayList<Footwear>();
        myBins = new ArrayList<Bin>(5);
        for(int i=0; i<5; i++)
        {
            addBin();
        }
    }

    public void addBin() 
    { 
       myBins.add( new Bin( "B" + myBins.size() ) );
    }

    @override
    public String toString()
    {
      String b0 = myBins.get(0).getName() + "\n" + 
      myCatalog.get(0).toString() + ", " +   myBins.get(0).getContents().get(1) + "\n";

    int b0size = myBins.get(0).getContents().size();

    return b0size + b0;
    }       
}

EDIT 增加鞋类

public class Footwear

{

private String myStyle;
private double mySize;
private String mySKU; 

public Footwear ( String style, double size, String SKU)

{
    myStyle = style;
    mySize = size;
    mySKU = SKU;
}


 public String getStyle()

 {
     return myStyle;
 }


 public double getSize()

 {
     return mySize;
 }


 public String getSKU()

 {
     return mySKU;
 }


 public String getType()

 {
     return "Unspecified";
 }

 public String printSize()

 {
     int x = (int)(getSize());
     if (getSize() % x == 0.5)
     return getStyle() + " (size " + x + "-1/2)";  

     else
     return getStyle() + " (size " + x + ")";

 }

 public String toString()

 {
      int x = (int)(getSize());
      String ShoeInfo = "";
      if (getSize() % x == 0.5)
      ShoeInfo = getStyle() + " (size " + x + "\u00bd" + ")";
      else
      ShoeInfo= getStyle() + " (size " + x + ")";    

     if (!(getType().equals("Unspecified")))
     return getType() + " - " + ShoeInfo;

     else
     return ShoeInfo;

 }

}

2 个答案:

答案 0 :(得分:0)

我会使用Warehouse类中的StringBuilder类来解决这个问题。此类允许进行字符串操作,例如追加,插入和删除。

这是PSUEDO-CODE,可以让你走上正确的轨道

StringBuilder output = new StringBuilder();
output.append(myBins.get(x).toString());
ouput.replace(0, output.length, "/n");
int colon = output.charAt(':');
output.insert(colon, "/n");
output.insert(colon +1, catOut);

你可能需要调整它,但这应该给你一个想法

答案 1 :(得分:0)

这是您的解决方案 新的仓库toString方法

@Override
    public String toString()
    {
      String output = "";
      for(Bin b : myBins)
      {
          System.out.print(b.toString());
          if(!b.getContents().isEmpty())
          {
            for(BinItem bi : b.getContents())
            {
                System.out.println(findSKU(bi.getSKU()) + ", " + bi.toString());
            }
          }
      }
      return " ";
    }   
    private String findSKU(String SKU)
    {
        for(Footwear f : myCatalog)
        {
            if(SKU.equals(f.getSKU()))
                return f.toString();
        }
        return "";
    }

新的Bin toString方法

@Override
public String toString()
{
    String s = "Bin " + myName + ":\n";
    return s;    
}        

要完成此操作,您需要将另一个变量添加到名为String shoeType的鞋类中,并将鞋类的子类设置为相应类型的IE Dress Shoe,Casual Shoe

新鞋类toString方法

@Override
    public String toString()
    {
        int x = (int)(getSize());
        String ShoeInfo = "";
        if (getSize() % x == 0.5)
            ShoeInfo = shoeType + " - " + myStyle + " (size " + x + "\u00bd" + ")";
        else
            ShoeInfo= shoeType + " - " + myStyle + " (size " + x + ")";    
         if (!(getType().equals("Unspecified")))
            return getType() + " - " + ShoeInfo;
         else
            return ShoeInfo;
    }

以上更改的示例输出。

Bin B0:
Dress Shoe - Loafer (size 10½), SKU 1234-13: 5
Boot - Riding (size 8), SKU 1234-5: 5
Bin B1:
Bin B2:
Dress Shoe - Wing-tip (size 10), SKU 1234-3: 10
Bin B3:
Casual Shoe - Sandal (size 9½), SKU 1234-0: 8
Bin B4:
Bin B5:
Bin B6:
Bin B7:
Bin B8:
Bin B9: