0

I am trying to get a value from with in an ArrayList. I have two classes, the main and a Car class. here is the code:

  public class CarOrders {
     public static void main (String [] args){
        Car toyota= new Car("Toyota", "$10000", "300"+ "2003");
        Car nissan= new Car("Nissan", "$22000", "300"+ "2011");
        Car ford= new Car("Ford", "$15000", "350"+ "2010");

        ArrayList<Car> cars = new ArrayList<Car>();
        cars.add(toyota);        
        cars.add(nissan);
        cars.add(ford);
     }

public static void processCar(ArrayList<Car> cars){
       int totalAmount=0;
       for (int i=0; i<cars.size(); i++){
          cars.get(i).computeCars ();
         totalAmount+= ?? 
      // in need to add the computed values of totalprice from the Car class?

       }
      System.out.println (totalAmount);

    }


}    
class Car {
     public Car (String name, int price, int, tax, int year)
     {
       constructor.......
     }

     public void computeCars ()
     {
      int  totalprice= price+tax;
      System.out.println (name + "\t" +totalprice+"\t"+year );
     }

}

How would i be able to calculate totalAmount in the processCar() method where totalAmount=totalAmount+totalPrice from the computCar() method in the Car Class?

1
  • Make computeCars() return totalprice. Thus totalAmount += cars.get(i).computeCars (); Commented Oct 31, 2011 at 8:32

4 Answers 4

1

Just return price+tax; from computeCars() :

 public int computeCars ()
 {
  return price+tax;
 }

then :

    public static void processCar(ArrayList<Car> cars){
       int totalAmount=0;
       for (int i=0; i<cars.size(); i++){
         totalAmount+= cars.get(i).computeCars(); 
       }
      System.out.println (totalAmount);
    }
Sign up to request clarification or add additional context in comments.

Comments

1

I'd suggest you change the signature of your computCar() method

public int computeCar() { ... }

and return the totalPrice as value from this method call. Thus you can use it in your method processCar().

Comments

0
public int  computeCars ()
     {
      return price+tax;

     }


public static void processCar(ArrayList<Car> cars){
       int totalAmount=0;
       for (int i=0; i<cars.size(); i++){

         totalAmount=+ cars.get(i).computeCars ();
      // in need to add the computed values of totalprice from the Car class?

       }
      System.out.println (totalAmount);

    }

Comments

-1

You need to make some changes in you Car class, it should be like this:

class Car {
 //i'm adding only 2 properties you can add all the properties
 public int price;
 public int tax;
 public Car (String name, int price, int tax, int year)
 {
   //here you should add these 2 lines
   this.price=price;
   this.tax=tax;
 }

and you function will be:

  public static void processCar(ArrayList<Car> cars){
   int totalAmount=0;
   for (int i=0; i<cars.size(); i++){
      cars.get(i).computeCars ();
     totalAmount=+ cars.get(i).price;
  // in need to add the computed values of totalprice from the Car class?

   }
  System.out.println (totalAmount);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.