This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
/** * Constructor for objects of class Club */ public Club() { // Initialise any fields here ...
memberships = new ArrayList<Membership>(); members = new ArrayList<Membership>(); }
and method
public ArrayList <Membership> purge(int month, int year) { ArrayList <Membership> members = new ArrayList<Membership>(); if ((month > 0) && (month < 13)) {
If you don't go out on the limb, you'll never get the whole tree
pascal betz
Ranch Hand
Joined: Jun 19, 2001
Posts: 547
posted
0
you just described what works, not where the problem lies (Exceptions ? unexpected result ?), how you call the code and so on.
Perhaps the problem has anything to do with the local variable "members" which also exists as a property of the Club class ? And since you pass in month and year in the purge() method i assume you want to check for both
??
also you should use code tags around any code you post. This will make it more readable.
Mike Stevens
Greenhorn
Joined: Oct 28, 2006
Posts: 12
posted
0
Thanks for looking in on me pascal. There were no errors it just was not behaving the way it was Intended. What do you mean by you should use code tags around any code you post.
You have helped me fix one of my problems by pointing out the local var, thanks. I new this was there, I was just a little mixxed-up (better now).
I do still have another problem though and thats returning the collection members now. I could create a local var to do this like I had but would much rather return the collection
Originally posted by Mike Stevens: Thanks for looking in on me pascal. There were no errors it just was not behaving the way it was Intended. What do you mean by you should use code tags around any code you post.
About the code tags: When you put source code in your post, write it between [ code ] and [/ code ] tags (without the spaces, ofcourse) - then the forum software will format and indent it nicely. See this topic from the JavaRanch FAQ: Use Code Tags
can you post your new version (including the code tags, as Jesper described) and explain again what you want to do and whats happening isntead ?
pascal
Mike Stevens
Greenhorn
Joined: Oct 28, 2006
Posts: 12
posted
0
Thanks again for takin' a boo pascal. So the method is not complete yet but easy to finish the validation and some other thing it needs. I have it doing what I want it to do,
just wondering is if I can return the data from members.add(monthMem);?
rather than the local var?
purged.add(monthMem);
I have to use the method sig.
----------------------------------------------------------- Class and constructor
[ November 08, 2006: Message edited by: Mike Stevens ] [ November 09, 2006: Message edited by: Mike Stevens ]
pascal betz
Ranch Hand
Joined: Jun 19, 2001
Posts: 547
posted
0
you can return whatever you want... as long as it fits the method signature (btw: i would recomend to use java.util.List instead of java.util.ArrayList as the return type. it is considered good practice to code against interfaces rather than implementations - if you are allowed to change this)
I amo not sure i follow what you want to do: - you have a List "memberships". does this include all the members (valid date/invalid date) ? - you have a List "members". is this a subset of "membership" including just the valid members (those which are not purged) ? - in your purge method you drop the members depending on some date criteria and you want to return the purged collection ?
? did i get it right so far ?
If you need to "recalucalate" the purged List everytime (e.g. because once you purge for December 2006 and once you purge for January 2006) then you should not make the "members" List a instance variable. Rather it should be a method local variable. (in this case the "members" List is only valid when accessed trough the purge() method - it's content is dependent on the last purge() method call).
Also in your implementation if you call purge() twice, the same member might be in the "members" List twice (members.add() is called twice with the same member)
do i make any sense ? have a nice day.
pascal
Mike Stevens
Greenhorn
Joined: Oct 28, 2006
Posts: 12
posted
0
Hi again pascal, I pretty much have to stick to what is posted above. Other than what I thought I would have to use
But with what you mentioned about what can be returned (thanks) and the error I received about incompatable types when I tried to return members it became clear that I need to change
All works now
This is from a lesson in a book. I was in an intro course for Java and OOP but had to drop out due to medical reasons so now I am left with no support for this book but really want to complete it.
The book is asking me to Remove all members from a given month and return them stored in a separate collection. It is the seperate collection that I wanted to return and access in another method
There is a little support for the book, but the answers are not avalable so when your stuck or if your right or wrong you have know way of knowing because you have nothing to compare to. Easy to get off track even when things are working right.
Thanks for looking in, all is as it should be now. I hope!