Java calculation with vectors question

acole1

Golden Member
Sep 28, 2005
1,543
0
0
I have a Sale object that has a Vector in it.

That Vector contains Line objects.

Each line object has a line number, an Item object, a quantity, and a subtotal.


My problem is that I can't figure out how to calculate the Total Price for the Sale.

I have a method that walks through the Vector and prints out the toString of each Line object, and I think it would be something similar to that, but I'm not sure. (Attached is the code for this.)

I would think all you have to do is go to each Line object in the Vector and ask it what it's subtotal is, then do a += to totalPrice or something.

Do you know the syntax/code for this?
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Well you have to sum up the subtotals, right? And then probably calculate the tax and shipping, etc. I'm not sure why you think calling toString on Line will accomplish that. At best all you'll get is a list of the textual representations of the individual fields.

You need to iterate through the vector, take the value of the subtotal of each line, and add it to an accumulator variable, call it total. When you reach the end of the list you can then operate on the total variable to calculate order-level costs.

Edit: oh, I see you may be suggesting the toString method as a template. Yes, it's the same general approach. But replace the call to toString() with the block that reads the Line's subtotal and adds it to the total variable.
 

acole1

Golden Member
Sep 28, 2005
1,543
0
0
Originally posted by: Markbnj
Well you have to sum up the subtotals, right? And then probably calculate the tax and shipping, etc. I'm not sure why you think calling toString on Line will accomplish that. At best all you'll get is a list of the textual representations of the individual fields.

You need to iterate through the vector, take the value of the subtotal of each line, and add it to an accumulator variable, call it total. When you reach the end of the list you can then operate on the total variable to calculate order-level costs.

Edit: oh, I see you may be suggesting the toString method as a template. Yes, it's the same general approach. But replace the call to toString() with the block that reads the Line's subtotal and adds it to the total variable.

See, thats the thing I am missing. I don't know how to iterate through the vector and call the getSubtotal method.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Your Vector is storing objects of type Object, not of type Line. So you either need to obtain a Vector<Line> or you need to cast the items in your Vector<Object> to Line. You can use Reflection API to determine if your items in List<Object> can be cast to type Line if you choose the latter method otherwise it's possible to get a runtime exception for an invalid cast.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Originally posted by: Crusty
Your Vector is storing objects of type Object, not of type Line. So you either need to obtain a Vector<Line> or you need to cast the items in your Vector<Object> to Line. You can use Reflection API to determine if your items in List<Object> can be cast to type Line if you choose the latter method otherwise it's possible to get a runtime exception for an invalid cast.

Good point! Missed that. You need to have the list returning the right type as well. Once you have a valid reference to a Line object the IDE will help you figure out what method or property you need.
 

acole1

Golden Member
Sep 28, 2005
1,543
0
0
Originally posted by: Crusty
Your Vector is storing objects of type Object, not of type Line. So you either need to obtain a Vector<Line> or you need to cast the items in your Vector<Object> to Line. You can use Reflection API to determine if your items in List<Object> can be cast to type Line if you choose the latter method otherwise it's possible to get a runtime exception for an invalid cast.

I just did a replace all on Vector<Object> and made it Vector<Line>, and everything compiles and runs just fine.

So now, how do I address a particular Line object and get it's Subtotal, then do the same for every line?

Are there any code examples floating around the net?

Sorry, I'm just a Java novice. Thanks for the help!


Edit:
When I use the following code, it compiles, but when I run it then I get a "Array index out of range: 1.... etc etc" error.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
You are mixing up techniques for iterating the Vector. The iterator will keep track of your position in the Vector for you, so there's no need to use Vector.get(int index) to retrieve your Line object. You would replace your Line lineObject = aVector.get(counter); with Line lineObject = (Line)iter.Next();

Also, it's bad programming practice to declare a variable inside of a loop. You should declare lineObject outside of the loop :)
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Originally posted by: Crusty
Also, it's bad programming practice to declare a variable inside of a loop. You should declare lineObject outside of the loop :)

I've always heard the opposite through numerous code reviews with senior developers that it's good practice to limit the scope of variables as tight as you can.

Usually with Java there are no performance implications doing this, although it is common for some to believe otherwise.
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Originally posted by: tfinch2
Originally posted by: Crusty
Also, it's bad programming practice to declare a variable inside of a loop. You should declare lineObject outside of the loop :)

I've always heard the opposite through numerous code reviews with senior developers that it's good practice to limit the scope of variables as tight as you can.

Usually with Java there are no performance implications doing this, although it is common for some to believe otherwise.

I agree that variables need to have the tightest scope possible, but I've seen far too many mistakes by people because they keep on reinitializing their counter variable inside of their loop ;).

In the OP's case, it most likely doesn't matter, especially since it's java :)
 

acole1

Golden Member
Sep 28, 2005
1,543
0
0
Originally posted by: Crusty
You are mixing up techniques for iterating the Vector. The iterator will keep track of your position in the Vector for you, so there's no need to use Vector.get(int index) to retrieve your Line object. You would replace your Line lineObject = aVector.get(counter); with Line lineObject = (Line)iter.Next();

Also, it's bad programming practice to declare a variable inside of a loop. You should declare lineObject outside of the loop :)

That worked perfectly! (after I made the N on Next lowercase)

It compiles and runs just how it should.

Thanks for all the help, and tips, guys!
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Originally posted by: acole1
Originally posted by: Crusty
You are mixing up techniques for iterating the Vector. The iterator will keep track of your position in the Vector for you, so there's no need to use Vector.get(int index) to retrieve your Line object. You would replace your Line lineObject = aVector.get(counter); with Line lineObject = (Line)iter.Next();

Also, it's bad programming practice to declare a variable inside of a loop. You should declare lineObject outside of the loop :)

That worked perfectly! (after I made the N on Next lowercase)

It compiles and runs just how it should.

Thanks for all the help, and tips, guys!

Ah sorry about that, I haven't written anything in Java in over a year now, C# is fresh in my mind :p
 

spamsk8r

Golden Member
Jul 11, 2001
1,787
0
76
If you're using Java 5 you can do this is a much cleaner fashion.

public void printTotalPrice(Vector<Line> aVector) {
double total = 0;
for (Line line : aVector) {
total += line.getSubtotal();
}
System.out.println(total);
}

Vector supports the List interface, so foreach iteration works on it. To be honest, most people nowadays use ArrayList instead of Vector (because Vector includes synchronization overhead) so you might want to consider that as well.