如何打印多个对象值

时间:2015-10-15 12:59:45

标签: java object

我需要做什么才能获得System.out.println(part2);显示id,name,stocklevel,unitprice,part0 stocklevel和part1 stocklevel?我只是起床直到第0和第1部分,并且他们的两个版本都没有打印出来。

主类

public class TestAssembledPart {    
    public static void main(String args[]) {
        // Constructing two Part objects (the base parts)
        Part part0 = new Part("p101", "Crank", 218, 12.20);        
        Part part1 = new Part("p102", "Pedal", 320, 14.30);        

        // Constructing AssembledPart object assembled from p101 & p102        
        AssembledPart part2 = new AssembledPart("p183", "Crank & Pedal",80,3.50,part0,part1);        

        System.out.println(part2); //<--------HERE

        // replenishing stock by 100 items        
        part2.replenish(100);   

        // Supplying the maximum possible assembled parts - combination         
        // of existing parts and base parts available for assembly         
        System.out.println("Supplying max number of (assembled) part");        

        int totalAvail = part2.getStockLevel() + part2.getAvailForAssembly();   
        double cost = part2.supply(totalAvail);       

        if ( cost > 0)           
            System.out.println(part2.getID() + " Total cost for "  
                        + totalAvail + " items = " + cost);        
        else            
            System.out.println(part2.getID()  + " Insufficient parts to supply " + totalAvail);  

        // printing the stock level for assembled and base parts        
        System.out.println(part0.getID() + " (base part) Available qty="  + part0.getStockLevel());        
        System.out.println(part1.getID() + " (base part) Available qty="  + part1.getStockLevel());       
        System.out.println(part2.getID() + " (assembled) Available qty="  + part2.getStockLevel());  
        // Attempting to supply another 10 items (bound to fail)        
        System.out.println("Trying to supply another 10 parts");   

        int qty = 10;        
        cost = part2.supply(qty);        

        if ( cost > 0)           
            System.out.println(part2.getID() + " Total cost for " + qty  + " items = " + cost);        
        else        System.out.println(part2.getID()  + " Insufficient parts to supply " + qty);  
    } 
}  

AssembledPart

public class AssembledPart extends Part {

    private Part basica;
    private Part basicb;
    private int assembledstocklevel;

    public AssembledPart(String id, String name, int stocklevel, double unitprice, 
    Part part0, Part part1) {

        super(id, name, stocklevel, unitprice);

        this.basica = part0;
        this.basicb = part1;
    }

    public void replenish(int qty){
        super.replenish(qty);
        //super.stocklevel = super.stocklevel + qty;
    }

    public void setAvailForAssembly(int Part){
        assembledstocklevel = assembledstocklevel;
    }

    public int getAvailForAssembly(){
        return assembledstocklevel;
    }

    public String toAssembledString() {
        return super.toString() + " | " + basica.getName() + " | " + basicb.getName();
    }   

    public static String toAssembledString(Collection<AssembledPart> aparts {
        String s = "";
        for (AssembledPart apart: aparts){
            s += apart.toAssembledString() + "\n";
        }
        return s;
    }
}

部分

public class Part {

    private String id;
    private String name;
    protected int stocklevel;
    private double unitprice;
    private int qty = 6000;

    public Part(String id, String name, int stocklevel, double unitprice){
        this.id = id;
        this.name = name;
        this.stocklevel = stocklevel;
        this.unitprice = unitprice;
    }


    String partsAvailable()
    {
        //String newLine = System.getProperty("line.separator");
        return (id + "\t" + name + "\t    " + stocklevel + "\t\t   " + unitprice);
    }


    public String getID() {
        return id;
    }

    public void setID(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getStockLevel(){
        return stocklevel - qty;
    }

    public void setStockLevel(int stocklevel){
        this.stocklevel = stocklevel;
    }

    public double getUnitPrice(){
        return unitprice;
    }

    public void setUnitPrice(double unitprice){
        this.unitprice = unitprice;
    }

    public void replenish(int qty){
        this.stocklevel = stocklevel + qty;
    }

    public double supply(int qty){
        return unitprice * qty;
    }

    public String toString() {
        return id + " | " + name + " | " + stocklevel + " | " + unitprice;
    }

    public static String toString(Collection<Part> parts){
        String s = "";
        for (Part part: parts){
            s += part + "\n";
        }
        return s;
    }
}

object names not appearing

2 个答案:

答案 0 :(得分:1)

如果您致电System.out.println(Object),则会通过调用String.valueOf(Object)进行调用,最终会调用Object#toString或任何班级的String toString()覆盖版本。< / p>

那么为了获得结果你需要做的就是覆盖AssembledPart这样的方法

@Override
public String toString() {
    return super.toString() + " | " + basicb.getStockLevel() + " | " + basica.getStockLevel();
}

答案 1 :(得分:0)

AssembledPart 未实现 toString 方法,因此使用 Part.toString()方法:
 id + " | " + name + " | " + stocklevel + " | " + unitprice
这正是你的输出; - )。