aspose file tools*
The moose likes Beginning Java and the fly likes Printing a total cost from collection of job Objects in Arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Printing a total cost from collection of job Objects in Arraylist" Watch "Printing a total cost from collection of job Objects in Arraylist" New topic
Author

Printing a total cost from collection of job Objects in Arraylist

Iain Linton
Greenhorn

Joined: Jan 24, 2006
Posts: 21
Hello all as of previous posts here are the class�s which I am using.

The Job Class stores jobs with these parameters

int ref, int nCopy, boolean doubleSided, int nPages, String staffMember, Time atime, Date adate, Date rdate

The Daylog Class stores the jobs in an ArrayList when a job is completed it is passed to the Completed ArrayList

When printing the information to the terminal window I have to display the total cost of all the completed jobs, in the Job class I have the method getCost how can I use this in the Daylog Class to calculate the total cost of the completed jobs and print it to the terminal window.

Any help would be great !
[ January 29, 2006: Message edited by: Iain Linton ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I have a few suggestions that will help encourage people here to asnwer your questions:

1) Please do not post so much code when you ask a question. Few of us are going to look at every line. You should only post the code that is relavant to your question.

2) You should not start a new thread for a question that is related to previous posts you have made. In other words, you should continue the conversation as a reply to a thread you have already started unless you are asking a completely unrelated question.

3) Ask a specific question. In other words, show us what you have tried so far and explain why your attempts haven't worked out. The more details that you can give, the better.

If you follow these suggestions, you are more likely to get the help you want.

With that said, I have already given you a suggestion in another thread for some steps you can take to figure out how to calculate the total. If you missed them, please go back and reread that thread.

Layne
[ January 28, 2006: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Iain Linton
Greenhorn

Joined: Jan 24, 2006
Posts: 21
I managed to do it it was a case of =+ thankyou for all your help and advice, now I just have to work out big jobs.

Would it be

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296


That should work just fine (assuming you have a print() method that will do something relevant). However remember an if statement doesn't always have to have a complemetary else statement. Therefore you could just write this as:

[ January 29, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Iain Linton
Greenhorn

Joined: Jan 24, 2006
Posts: 21
Thank you now I have to work out the averageCost (mean)using a double.

Any ideas

I thought I would do this:

----------------------------------------------
This does compile but when run get

arithmeticException.

cannot / 0

no help available "Good old BlueJ"

Thank you

[ January 29, 2006: Message edited by: Iain Linton ]
[ January 29, 2006: Message edited by: Iain Linton ]
Craig Tyler
Ranch Hand

Joined: Jan 15, 2006
Posts: 52
I'm surprised the compiler let that through since the return type is a void and you're trying to return something. Doing a quick search through your code turns up totalCost being set to 0, but never to anything else. I imagine that is your problem unless you have code that sets totalCost to something. Further, size() will return an integer and you are trying to divide with another integer, so you are going to get integer division (ie, 7/3 = 2) instead of floating point division.

One added note. You don't need to specifically import java.util.Iterator and jave.util.ArrayList since you are also importing java.util.*.


Final Note: I really didn't want to wade through all your code, so if I'm missing something, just skip what I said.
[ January 29, 2006: Message edited by: Craig Tyler ]
Iain Linton
Greenhorn

Joined: Jan 24, 2006
Posts: 21
now within the code I have a method which does this:



So when the job is passed to the completedList it also passes its cost to the totalCost field.

then when I have say 4 jobs in the completedList and the totalCost has been updated I would then need to get the totalCost and then divide it by number of jobs in the completedList.

Done this different ways but keeps returning 0.0

I don't understand
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I agree with Craig. If youre sure that the totalCost and completedList variables conatain what you think they contain, integer division may be your problem. The workaround for that is using an explicit cast for at least one of your factors.


Note: casting the entire result to a double after the division is performed won't do it i.e.
Craig Tyler
Ranch Hand

Joined: Jan 15, 2006
Posts: 52
In addition with what Garrett said about explicitly casting one of the operands to use floating point division, your actual division is the wrong way around. To get an average, you need to divide the totalCost by the size of the completedList.
Iain Linton
Greenhorn

Joined: Jan 24, 2006
Posts: 21
Thank you so much you have made my day (well night)

If I have any more problems as Arnold said "I'll be back"
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Craig Tyler:
In addition with what Garrett said about explicitly casting one of the operands to use floating point division, your actual division is the wrong way around. To get an average, you need to divide the totalCost by the size of the completedList.

Good catch Craig, I completely missed the logic error there.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing a total cost from collection of job Objects in Arraylist