打印出最昂贵的船只及其来自阵列的信息

时间:2015-11-16 02:26:14

标签: java inheritance arraylist

我正在制作一个有超级(船)和两个子类(SailBoat,Powerboat)的船只计划,我必须打印出所有的船只信息和价格,以及最昂贵的船只和它的信息。这是我遇到麻烦的部分,因为我不完全确定如何去做。这是我到目前为止所拥有的......

船类:

public class Boat {
    String color;
    int length;

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

    public Boat(String col, int leng) {
        color = col;
        length = leng;
    }

    public boolean setColor(String col) {
        if ("white".equals(col) || "red".equals(col) || "blue".equals(col) || "yellow".equals(col)) {
            col = color;
            return true;
        } else {
            System.out.println("Error: can only be white, red, blue or yellow");
            return false;
        }

    }

    public String getColor() {
        return color;
    }

    public boolean setLength(int leng) {

        if (leng < 20 || leng > 50) {
            leng = length;
            System.out.println("Sail Boats can only be between 20 and 50 feet, inclusively.");
            return false;

        } else {
            return true;

        }
    }

    public int getLength() {
        return length;
    }

    public String toString() {
        String string;
        string = String.format("Color = " + color + " Length = " + length);
        return string;
    }

    public int calcPrice() {
        int price;
        price = 5000 + length;
        return price;
    }
}

PowerBoat Subclass

import java.text.NumberFormat;

public class PowerBoat extends Boat {
    int engineSize;

    public PowerBoat() {
        super();
        engineSize = 5;
    }

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

    public boolean setEngineSize(int esize) {
        if (esize < 5 || esize > 350) {
            System.out.println(
                    "Error: That engine is too powerful.  The engine size must be between 1 and 350, inclusively");
            esize = engineSize;
            return false;
        } else {
            return true;
        }

    }

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

    public String toString() {
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        nf.setMinimumFractionDigits(2);
        nf.setMaximumFractionDigits(2);
        return super.toString() + " Engine Size = " + engineSize + " Price = " + nf.format(calcPrice());
    }
}

SailBoat子类

import java.text.NumberFormat;

public class SailBoat extends Boat {

    int numSails;

    public SailBoat() {
        numSails = 0;
    }

    public SailBoat(String col, int leng, int numsail) {
        color = col;
        length = leng;
        numSails = numsail;
    }

    public boolean setNumSails(int nsails) {
        if (nsails < 1 || nsails > 4) {
            nsails = numSails;
            return false;
        } else {
            return true;
        }
    } // end setNumSails

    public int getNumSails() {
        return numSails;
    }

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

    public String toString() {
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        nf.setMinimumFractionDigits(2);
        nf.setMaximumFractionDigits(2);
        return super.toString() + "Color: " + color + " Length: " + length + " Number Sails = " + numSails + " Cost = "
                + nf.format(calcPrice());
    }

    public int getTotalCost() {
        int totalCost = 0;
        totalCost += calcPrice();
        return totalCost;
    }
}

库存类(测试人员)

import java.util.ArrayList;

public class Inventory {

    public static void main(String[] args) {

        // boat objects
        Boat pb1 = new PowerBoat("blue", 22, 60);
        Boat sb1 = new SailBoat("white", 20, 1);
        Boat sb2 = new SailBoat("red", 42, 3);
        Boat pb2 = new PowerBoat("yellow", 35, 80);
        Boat pb3 = new PowerBoat("red", 50, 120);
        Boat sb3 = new SailBoat("blue", 33, 2);
        Boat pb4 = new PowerBoat("white", 20, 10);

        ArrayList<Boat> AL = new ArrayList<Boat>();
        // add boat objects to arraylist
        AL.add(pb1);
        AL.add(sb1);
        AL.add(sb2);
        AL.add(pb2);
        AL.add(pb3);
        AL.add(sb3);
        AL.add(pb4);

        // print all boat objects
        System.out.println("Print all boats");
        for (Boat anyBoat : AL) {
            System.out.println(anyBoat.toString());
        }

        int max = 0;
        int totalcost = 0;
        Boat mostExpensiveBoat = null;
        for (Boat anyBoat : AL) {
            if (anyBoat instanceof SailBoat) {
                totalcost += anyBoat.calcPrice();
                if (anyBoat.calcPrice() > max) {
                    max = anyBoat.calcPrice();
                    mostExpensiveBoat = anyBoat;
                }
            }
        }
    }
}

我真的很困惑如何完成这个程序,我打算在打印完所有船信息后得到的结果是这个..

Total price of all boats is $ 170,500.00
Most Expensive Boat: Color = red  Length = 42  Number Sails = 3 Cost = $ 48,000.00

任何帮助将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:2)

您应该纠正一些设计缺陷:

  1. 您的Boat课程应为interfaceabstract。你不能拥有一艘不是动力艇或帆船的船,所以你不应该实例化它。
  2. 您的实例变量应为private
  3. 制作需要由Boat的子类定义的方法摘要(例如calcPrice)。
  4. 如果你能够使用Java 8,那么有一个很好的方法来获得最昂贵的船。以下代码将打印最昂贵的船(使用Boat.toString),如果有的话。

    allBoats.stream()
        .max(Comparator.comparingInt(Boat::calcPrince))
        .ifPresent(System.out::println);
    

    这避免了必须编写手动迭代列表比较价格的代码。它还应对空列表的情况(这意味着没有最大值)。否则,您需要在打印前初始化为null并与null进行比较。

答案 1 :(得分:0)

你的for循环应如下所示:

for (Boat anyBoat : AL) {
    totalcost += anyBoat.calcPrice();
    if (anyBoat.calcPrice() > max) {
        max = anyBoat.calcPrice();
        mostExpensiveBoat = anyBoat;
    }
}

如果它是一个帆船并不重要,你只想打印最昂贵的信息,这样你就可以删除条件的实例。之后:

NumberFormat nf = NumberFormat.getCurrencyInstance();
    nf.setMinimumFractionDigits(2);
    nf.setMaximumFractionDigits(2);

System.out.println("Total price of all boats is " +   nf.format(totalcost));
System.out.println("Most expensive boat: " + mostExpensiveBoat.toString());

应该可以工作,因为你已经覆盖了toString()方法。

还有一件事:在你的SailBoat toString()方法中,你正在做:

return super.toString() + "Color: " + color + " Length: " + length + " Number Sails = " + numSails + " Cost = "
            + nf.format(calcPrice());

当你调用super.toString()时,你打印的颜色和长度是两次;只需致电

return super.toString() + " Number Sails = " + numSails + " Cost = " + nf.format(calcPrice());