2

I am trying to construct a method that will allow me to find the total cost of a number of items in an arraylist. Currently I have an arraylist full of "items." In the item class the objects have a price and a quantity found by getQuantity and getPrice. I'm trying to find the total cost of all the items in the arraylist but keep getting 5 errors. Can anyone help thanks,

EDIT: Posted the errors

 public double getTotalCost()
         {

            double total;
            for(int i = 0; i < cart.size; i++) // size has private access in ArrayList
            {
                total = ((cart.get(i)).getQuantity) * ((cart.get(i)).getPrice) + total; // cannot find symbol // illegal start of type// cannot find symbol // illegal start of type
            }


            return total;
         }   
    }
5
  • 2
    why no closing braces for getQuantity() and getPrice() etc., Commented Jan 18, 2012 at 0:29
  • Why are you not posting the errors messages? We're terrible at reading minds you know. Commented Jan 18, 2012 at 0:30
  • also, using the += operator would make this much more readable. plus, the "total" variable isn't initialized with 0. not to mention that a "for-each" over the ArrayList would be more preferable... Commented Jan 18, 2012 at 0:30
  • Re: "I [...] keep getting 5 errors": When asking about error-messages, you should always post them. (In this case I think we can guess what they are, but you should always post them, anyway.) Commented Jan 18, 2012 at 0:31
  • Ok, I will definitively post them sorry Commented Jan 18, 2012 at 0:35

4 Answers 4

1

To call a method on a object, you always need to use parentheses. So not cart.size but cart.size(). If you do not, Java expects you are requesting a public variable for a object. size is a private variable in your List Object and therfore gives you a private access error

public double getTotalCost(){
  double total = 0;
  for(int i = 0; i < cart.size(); i++){
    total =  ( cart.get(i).getQuantity() * cart.get(i).getPrice() ) + total;
  }
  return total;
}   
Sign up to request clarification or add additional context in comments.

Comments

1

You're missing parenthesis, lots of them. Try this:

public double getTotalCost() {
    double total = 0;
    for (int i = 0; i < cart.size(); i++) {
        total = cart.get(i).getQuantity() * cart.get(i).getPrice() + total;
    }
    return total;
}

And I'm assuming that cart was declared using generics, something like this:

ArrayList<Item> cart = ...

Takeaway lesson: when you call a method in Java, it will always have parenthesis after the name, even if it doesn't receive any arguments.

2 Comments

total is a local variable, so is required to be explicitly initialized before being used.
My instance field is private ArrayList<Item> cart;
0

You can get the size of an ArrayList with the size() method - ArrayList's size field is private, but the method is public and can be used instead.

Another error likely comes from trying to access the Item class's getPrice and getQuantity fields rather than the getPrice() and getQuantity() methods - another quick fix, just remember the parenthesis!

Syntactically speaking, you can probably make things a bit easier on yourself using some different syntax in your for loop (though this may be beyond your class a bit):

double total = 0;
for (Item i : cart) {
    total += i.getQuantity() * i.getPrice();
}

Some other tips: your total field isn't initialized to anything (though it is in the example above). This may work fine with native types (ints, floats, doubles, etc), but it'll definitely cause compiler complaints later on with non-primitive types. If in doubt, initialize them to 0 for primitives or null.

Comments

0

Not sure why everyone is bypassing the obvious for loop construct to ease this a bit. Here I use CartEntry, replace that with whatever object is in your List that has the getQuantity() and getPrice() methods on it.

double total = 0;
for(CartEntry entry: cart){
  total += entry.getQuantity() * entry.getPrice();
}
return total;

While I'm at it. I'd suggest putting an extendedPrice() method on whatever the object type is in the cart, which does the quantity * price multiplication for you.

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.