如何从ArrayList中的对象中添加所有值的总成本?

时间:2013-03-21 01:11:20

标签: java inheritance arraylist foreach super

Print all boats
Color= blue Length= 22 Engine size= 60 Price= $12,800.00
Color= white Length= 18 Num of Sails= 1 Price= $20,000.00
Color= red Length= 42 Num of Sails= 3 Price= $48,000.00
Color= yellow Length= 35 Engine size= 80 Price= $17,100.00
Color= red Length= 50 Engine size= 120 Price= $22,400.00
Color= blue Length= 33 Num of Sails= 2 Price= $37,000.00
Color= white Length= 14 Engine size= 10 Price= $9,400.00

Total price of all boats is $166,700.00. 

如何在ArrayList中获得totalCost?

Most Expensive Boat: Color= red Length= 42 Num of Sails= Cost= $48,000.00 

如何让ArrayList搜索成本最高的船?

我还不太了解如何在ArrayList中获取任何内容(除了只在一行代码中反复打印),但测试类本身不存在变量。这是我正在使用的所有三个类的代码。希望我能更好地理解你们如何获得和使用数组中的任何东西,如计算,最高成本,最低成本,最便宜等等。

船是继承(超类) SailBoat和PowerBoat扩展了Boat(子类) 库存(测试类)

public class Boat
{
//attributes
String color; //holds boat color
int lengthBoat; //contains boat length

//default constructor
public Boat()
{
    color = "white";
    lengthBoat = 0;
}
//parameterized constructor
public Boat(String _color, int _lengthBoat)
{
    setColor(_color);
    setLengthBoat(_lengthBoat);
}
//mutator method for color
public boolean setColor(String _color)
{
    if(_color == "white" || _color == "red" || _color == "yellow" || _color == "blue")
    {
        color = _color;
        return true;
    }
    else 
    {
        System.out.println("ERROR - The color must be one of these following choices, \"white\", \"red\", \"yellow\",  or \"blue\".");
        return false;
    }
}
//accessor method for color
public String getColor()
{
    return color;
}
//mutator method for lengthBoat
public boolean setLengthBoat(int _lengthBoat)
{
    if(_lengthBoat >= 0 && _lengthBoat <= 50)
    {
        lengthBoat = _lengthBoat;
        return true;
    }
    else
    {
        System.out.println("ERROR - The length must be between 0 and 50 inclusively.");
        return false;
    }
}
//accessor method for lengthBoat
public int getLengthBoat()
{
    return lengthBoat;
}
//toString method for printing results
public String toString()
{
    return "Color= " + getColor() + " Length= " + getLengthBoat();
}
}

SailBoat类

import java.text.NumberFormat;

 public class SailBoat extends Boat
 {
//attribute
int numOfSails; //holds the number of Sails

//default constructor
public SailBoat()
{
    numOfSails = 1;
}
//parameterized constructor
public SailBoat(String _color, int _lengthBoat, int _numOfSails)
{
    super(_color, _lengthBoat);//taken from the parent and inherited to be recognized in this class's constructor
    setNumOfSails(_numOfSails);
}

//mutator method for numOfSails
public boolean setNumOfSails(int _numOfSails)
{
    if(_numOfSails >= 1 && _numOfSails <= 4)
    {
        numOfSails = _numOfSails;
        return true;
    }
    else 
    {
        System.out.println("ERROR - The number of sails must be between 1 and 4 inclusively.");
        return false;
    }
}
//accessor method for numOfSails
public int getNumOfSails()
{
    return numOfSails;
}
//calculates the price of the SailBoat as follows:
public int calcPrice()
{
    return lengthBoat * 1000 + getNumOfSails() * 2000;
}
//toString method
public String toString()
{
    NumberFormat nf = NumberFormat.getCurrencyInstance();//use to make the numbers as currency with $ automatically added
    nf.setMinimumFractionDigits(2);//decimal is moved over 2 places
    nf.setMaximumFractionDigits(2);//decimal is moved over 2 places
    return super.toString() + " Num of Sails= " + getNumOfSails() + " Price= " + nf.format(calcPrice());
}
//get the total Cost of all boats printed in arraylist
public int getTotalCost()
{
    int totalCost = 0;
    totalCost += calcPrice();//as calcPrice() is printed, it is therefore added/updated to totalCost
    return totalCost;
}
}

PowerBoat类

import java.text.NumberFormat;

public class PowerBoat extends Boat
{
//attributes
int sizeOfEngine; //holds the engine size

//default constructor
public PowerBoat()
{
    sizeOfEngine = 5;
}
//parameterized constructor
public PowerBoat(String _color, int _lengthBoat, int _sizeOfEngine)
{
    super(_color, _lengthBoat);
    setSizeOfEngine(_sizeOfEngine);
}
//mutator method for sizeOfEngine
public boolean setSizeOfEngine(int _sizeOfEngine)
{
    if(_sizeOfEngine >= 1 && _sizeOfEngine <= 350)
    {
        sizeOfEngine = _sizeOfEngine;
        return true;
    }
    else
    {
        System.out.println("ERROR - The size of Engine must be between 1 to 350 inclusively.");
        return false;
    }
}
//accessor for sizeOfEngine
public int getSizeOfEngine()
{
    return sizeOfEngine;
}
//caculates the price of the PowerBoat as follows:
public int calcPrice()
{
    return 5000 + lengthBoat * 300 + getSizeOfEngine() * 20;
}
//toString method for printing the results
public String toString()
{
    NumberFormat nf = NumberFormat.getCurrencyInstance(); //formats all the numbers a currency
    nf.setMinimumFractionDigits(2); //sets the decimal place as a currency
    nf.setMaximumFractionDigits(2); //sets the decimal place as a currency
    return super.toString() + " Engine size= " + getSizeOfEngine() + " Price= " + nf.format(calcPrice());
}
//get the total Cost of all boats printed in arraylist
public int getTotalCost()
{
    int totalCost = 0;
    totalCost += calcPrice();//as calcPrice() is printed, it is therefore added/updated to totalCost
    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", 18, 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", 14, 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());
        //System.out.println("Total price of  all boats is " + anyBoat.getTotalCost());
     }
  }
 }

2 个答案:

答案 0 :(得分:1)

int sumCost = 0;
for(Boat b : AL)
{
  sumCost += b.calcPrice();
}
return sumCost;

答案 1 :(得分:0)

“如何在ArrayList中获得totalCost?”

“我如何让ArrayList搜索成本最高的船?” 回答:

 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;
        }
    }
 }     
 // if not null print out most expensive boat here

注意:如果您想要为可能具有相同最高价格的船只提供服务,请使用阵列列表来存储最昂贵的船只。