简体   繁体   中英

How do I add up the total cost for all values from the objects within ArrayList?

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. 

how do I get totalCost in ArrayList?

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

how do I get ArrayList to search for the boat with the highest cost?

I haven't quite really understand how to get anything in an ArrayList (besides only print over and over again in one line of code) yet without the variables being present in the test class itself. Here's the code for all three classes I'm using. Hopefully I can understand it much better for you guys on how to get and use anything in array such as calculations, highest cost, low cost, cheapest, etc..

Boat is the Inheritance (super class) SailBoat and PowerBoat extends Boat (subclasses) Inventory (test class)

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 class

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 class

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;
}
}

Inventory

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());
     }
  }
 }
int sumCost = 0;
for(Boat b : AL)
{
  sumCost += b.calcPrice();
}
return sumCost;

"how do I get totalCost in ArrayList?"
AND
"how do I get ArrayList to search for the boat with the highest cost?"
Answer:

 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

Note: if you want to cater for boats of potentially the same max price use an array list to store the most expensive boat/s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM