从由不同对象组成的列表中打印toString信息

时间:2018-06-27 14:03:59

标签: java oop

我在应对该问题方面存在一些问题。     我有一个 Boat 类,其中包含一个 toString()和getter和setter。      PowerBoat 类可扩展功能并从其超类中覆盖 toString()方法。      SailBoat 类,该类扩展了功能并从其超类覆盖 toString()方法。     在测试类中,我在Boat类型的ArrayList中添加了不同的PowerBoat,SailBoats。     我需要找到最昂贵的船,并打印有关该船的 toString()信息。

public class Boat {
    String color;
    int length;

    public Boat(){
        color = "white";
        length = 20;
    }

    public Boat(String color, int length){
        setColor(color);
        setLength(length);
    }

    public String getColor() {
        return color;
    }

    public boolean setColor(String color) {
        switch (color){
            case "white" : this.color = color;
            case "red" : this.color = color;
            case "blue" : this.color = color;
            case "yellow" : this.color = color;
            return true;
        }
        return false;
    }

    public int getLength() {
        return length;
    }

    public boolean setLength(int length) {
        if(length >= 20 && length <= 50) {
            this.length = length;
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        return "Boat{" +
                "color='" + color + '\'' +
                ", length=" + length +
                '}';
    }
}

public class PowerBoat extends Boat {
    int engineSize;

    public PowerBoat(){
        super();
        setEngineSize(5);
    }

    public PowerBoat(String color, int length, int engineSize){
        super(color, length);
        setEngineSize(engineSize);
    }

    public boolean setEngineSize(int engineSize){
        if(engineSize >= 5 && engineSize <= 350){
            this.engineSize = engineSize;
            return true;
        }
        return false;
    }

    public int getEngineSize() {
        return engineSize;
    }

    public int calcPrice(){
        return 5000 + length + 300 + engineSize * 20;
    }

    @Override
    public String toString() {
        return super.toString() +
                "engineSize= " + engineSize +
                '}' + " Price " + calcPrice();
    }
}


    public class SailBoat extends Boat {
        int numSails = 0;

        public SailBoat(){
            numSails = 1;
        }

        public SailBoat(String color, int length, int numSails){
            super(color, length);
            setNumSails(numSails);
        }

        public int getNumSails() {
            return numSails;
        }

        public boolean setNumSails(int numSails) {
            if(numSails >= 1 && numSails <= 4){
                this.numSails = numSails;
                return true;
            }
            return false;
        }

        public int calcPrice(){
            return length * 1000 + numSails * 2000;
        }

        @Override
        public String toString() {
            return super.toString() +
                    "numSails= " + numSails +
                    '}' + " price " + calcPrice();
        }
    }

    public class Inventory {
        public static void main(String[] args){
            ArrayList<Boat> list = new ArrayList();
            Boat powerBoat = new PowerBoat("blue", 46, 60);
            Boat powerBoat1 = new PowerBoat("yellow", 42, 55);
            Boat sailBoat = new SailBoat("white", 32, 1);
            Boat sailBoat1 = new SailBoat("red", 24, 2);
            list.add(powerBoat);
            list.add(powerBoat1);
            list.add(sailBoat);
            list.add(sailBoat1);

            int sumSailBoat = 0;
            int sumPowerBoat = 0;
            int largest = Integer.MIN_VALUE;

            for(Boat b : list){
                if (b instanceof SailBoat){
                    sumSailBoat+= ((SailBoat) b).calcPrice();
                    if (((SailBoat) b).calcPrice() > largest){
                        if(b instanceof SailBoat) {
                            largest = ((SailBoat) b).calcPrice();
                        }
                    }
                }
                if (b instanceof PowerBoat){
                    sumPowerBoat+= ((PowerBoat) b).calcPrice();
                    if(((PowerBoat) b).calcPrice() > largest){
                        if(b instanceof PowerBoat){
                            largest = ((PowerBoat) b).calcPrice();
                        }
                    }
                }
            }

            int totalSum = sumSailBoat + sumPowerBoat;
            System.out.println("Total price of sail boats is " + sumSailBoat);
            System.out.println("Total price of sail boats is " + sumPowerBoat);
            System.out.println("Total price of sail and power boats is " + totalSum);
            System.out.println("Most expensive boat is " + largest);
        }
    }

我设法找到了最高价,但是如何打印有关该船的toString信息?

5 个答案:

答案 0 :(得分:0)

如果知道类型,可以使用:

System.out.println("My PowerBoat " + ((PowerBoat) b));

System.out.println("My SailBoat " + ((SailBoat) b));

答案 1 :(得分:0)

如果已经找到对象,则需要打印该对象并将其存储在变量中,然后只需在其上调用toString。

Boat b = //whichever one you found here from the arraylist

System.out.println(b.toString())

由于Java中的类使用动态绑定,因此即使您的引用是超类,方法的内容也应相同。

答案 2 :(得分:0)

[teach-me]您似乎对OO设计和Java方法调用有一些误解。请与您的老师交谈或阅读一些OO / Java书籍。从Java中的abstractfinal的概念开始-应该使您知道如何调用方法以及如何使用继承。

话虽如此,你需要做两件事。

(1)要回答您的问题,除了int largest外,还请保留Boat mostExpensiveBoat,并在更新largest的同时对其进行更新。然后在末尾打印mostExpensiveBoat.toString()

(2)您需要使calcPrice()为Boat的抽象方法。然后,当您调用b.calcPrice()时,将“神奇地”调用与对象b所引用类型匹配的方法。无需检查instanceof或进行投射。

答案 3 :(得分:0)

最好在Boat接口中加入 calcPrice()

abstract class Boat {
    String color;
    int length;

    public Boat(){
        color = "white";
        length = 20;
    }

    public Boat(String color, int length){
        setColor(color);
        setLength(length);
    }

    public String getColor() {
        return color;
    }

    public boolean setColor(String color) {
        switch (color){
            case "white" : this.color = color;
            case "red" : this.color = color;
            case "blue" : this.color = color;
            case "yellow" : this.color = color;
            return true;
        }
        return false;
    }

    public int getLength() {
        return length;
    }

    public boolean setLength(int length) {
        if(length >= 20 && length <= 50) {
            this.length = length;
            return true;
        }
        return false;
    }

    public abstract int calcPrice();

    @Override
    public String toString() {
        return "Boat{" +
                "color='" + color + '\'' +
                ", length=" + length +
                '}';
    }
}

然后您可以通过以下方式进行搜索:

    int sumSailBoat = 0;
    int sumPowerBoat = 0;
    int largest = Integer.MIN_VALUE;
    Boat boatWithLargestPrice = null;

    for (Boat b : list) {
        int boatPrice = b.calcPrice();
        if (boatPrice > largest) {
            largest = boatPrice;
            boatWithLargestPrice = b;
        }

        if (b instanceof SailBoat) {
            sumSailBoat += boatPrice;
        } else {
            sumPowerBoat += boatPrice;
        }       
    }

    int totalSum = sumSailBoat + sumPowerBoat;
    System.out.println("Total price of sail boats is " + sumSailBoat);
    System.out.println("Total price of power boats is " + sumPowerBoat);
    System.out.println("Total price of sail and power boats is " + totalSum);
    System.out.println("Most expensive boat is " + boatWithLargestPrice + " with price " + largest);

答案 4 :(得分:0)

通过使您的Boatabstract,您可以要求所有Boat来实现calcPrice。然后,您可以将所有Boat都相同。

public abstract class Boat {
    final String color;
    final int length;

    public Boat(String color, int length) {
        this.color = color;
        this.length = length;
    }

    public abstract int calcPrice();

    @Override
    public String toString() {
        return "Boat{" +
                "color='" + color + '\'' +
                ", length=" + length +
                '}';
    }
}

public class PowerBoat extends Boat {
    final int engineSize;

    public PowerBoat(String color, int length, int engineSize) {
        super(color, length);
        this.engineSize = engineSize;
    }

    @Override
    public int calcPrice() {
        return 5000 + length + 300 + engineSize * 20;
    }

    @Override
    public String toString() {
        return super.toString() +
                "engineSize= " + engineSize +
                '}' + " Price " + calcPrice();
    }
}

public class SailBoat extends Boat {
    final int numSails;

    public SailBoat(String color, int length, int numSails) {
        super(color, length);
        this.numSails = numSails;
    }

    @Override
    public int calcPrice() {
        return length * 1000 + numSails * 2000;
    }

    @Override
    public String toString() {
        return super.toString() +
                "numSails= " + numSails +
                '}' + " price " + calcPrice();
    }
}

public void test() {
    ArrayList<Boat> list = new ArrayList();
    list.add(new PowerBoat("blue", 46, 60));
    list.add(new PowerBoat("yellow", 42, 55));
    list.add(new SailBoat("white", 32, 1));
    list.add(new SailBoat("red", 24, 2));

    Boat mostExpensiveBoat = null;
    int totalSum = 0;

    for (Boat boat : list) {
        totalSum += boat.calcPrice();
        if(mostExpensiveBoat == null || boat.calcPrice() > mostExpensiveBoat.calcPrice() ) {
            mostExpensiveBoat = boat;
        }
    }

    System.out.println("Total price of sail and power boats is " + totalSum);
    System.out.println("Most expensive boat is " + mostExpensiveBoat);
}