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 中获取任何内容(除了只在一行代码中一遍又一遍地打印),但测试类本身中没有变量。这是我正在使用的所有三个类的代码。希望我能更好地为你们理解如何获取和使用数组中的任何内容,例如计算、最高成本、低成本、最便宜等。
Boat 是继承(超类) SailBoat 和 PowerBoat 扩展了 Boat(子类) Inventory(测试类)
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();
}
}
帆船类
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;
}
}
动力艇类
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());
}
}
}