我想修改我的倉庫類中的toString方法,這樣它會從我的主類返回如何修改一個ArrayList的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是鞋類的擴展類。
鞋類是一種採用String風格,雙倍大小和String SKU的類。它的toString方法返回鞋信息,如:
Dress Shoe - Loafer (size 10-1/2), SKU 1234-13
斌是一類是發生在字符串名稱和創建內容的ArrayList,它的toString方法返回斌數量和內容,如:
[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;
}
}
編輯加入鞋類類
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;
}
}
可以顯示Bin類的代碼嗎? – Eran
對於toString,可以按照需要的順序添加所需的位。沒有什麼魔法可以做到這一點。 –
@Eran,我確定我已經添加了Bin類。 – Oninez