Results 1 to 5 of 5
  1. #1
    johnhelen is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default Question for a list of objects

    Hello all

    I would like to have your help

    I have a class written by another member. This class has two constructors.

    Java Code:
    public MySelections (Plan plan, List badPlans) {  //First
    			this.plan = plan;
    			this.badPlans = badPlans;
    		}
    		
    public MySelections (List<Plan> goodPlans, List<Plan> badPlans) { // Second
    			this.goodPlans = goodPlans;
    			this.badPlans = badPlans;
    		}
    Now I have a create a "First" object from a "Second" object.
    Only the lowest price for the Plan of goodPlans list in the Second object will be the Plan for the first object. Other plans in the goodPlan list will be added into badPlan list.

    What I did is:

    Java Code:
    private  MySelections reset(MySelections orig){
    			List<Plan> goodPlanList = orig.getGoodPlans();
    			List<Plan> badPlanList = orig.getBadPlans();
    			double smallest = Double.MAX_VALUE;			 
    			Plan bestPlan = null ;
    			for (Plan plan : goodPlanList){
    				double cost  = plan.getPrice(); 
    				 
    				if (cost <= smallest){
    					smallest = cost;
    					bestPlan = plan;				
    				}
    			}
    			goodPlanList.remove(bestPlan);
    			badPlanList.addAll(badPlanList);
    			return new MySelections (bestPlan, badPlanList);
    then I call function above

    Java Code:
    MySelection selection = new MySelection (....);
    selection = reset(selection);

    Could you please tell me if I can optimise the code above for better structure

    Many thanks
    john

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by johnhelen View Post
    Hello all

    I would like to have your help

    I have a class written by another member. This class has two constructors.

    Java Code:
    public MySelections (Plan plan, List badPlans) {  //First
    			this.plan = plan;
    			this.badPlans = badPlans;
    		}
    		
    public MySelections (List<Plan> goodPlans, List<Plan> badPlans) { // Second
    			this.goodPlans = goodPlans;
    			this.badPlans = badPlans;
    		}
    Now I have a create a "First" object from a "Second" object.
    Only the lowest price for the Plan of goodPlans list in the Second object will be the Plan for the first object. Other plans in the goodPlan list will be added into badPlan list.

    What I did is:

    Java Code:
    private  MySelections reset(MySelections orig){
    			List<Plan> goodPlanList = orig.getGoodPlans();
    			List<Plan> badPlanList = orig.getBadPlans();
    			double smallest = Double.MAX_VALUE;			 
    			Plan bestPlan = null ;
    			for (Plan plan : goodPlanList){
    				double cost  = plan.getPrice(); 
    				 
    				if (cost <= smallest){
    					smallest = cost;
    					bestPlan = plan;				
    				}
    			}
    			goodPlanList.remove(bestPlan);
    			badPlanList.addAll(badPlanList);
    			return new MySelections (bestPlan, badPlanList);
    then I call function above

    Java Code:
    MySelection selection = new MySelection (....);
    selection = reset(selection);

    Could you please tell me if I can optimise the code above for better structure

    Many thanks
    john
    Your program structure is fine but I think the second to last line is incorrect; according to your own remark "Other plans in the goodPlan list will be added into badPlan list"; shouldn't it be:

    Java Code:
    badPlanList.addAll(goodPlanList);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    johnhelen is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default Tahnks

    Thank JosAH for pointing out the error

    My concern now is the "Second" object is still the same or it was changed after function call "reset" ???

    I need the object "Second" (orig) unchanged after creating the object "First".

    Thanks

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by johnhelen View Post
    My concern now is the "Second" object is still the same or it was changed after function call "reset" ???

    I need the object "Second" (orig) unchanged after creating the object "First".
    Make a copy of those 'bad' and 'good' Lists first before you change them and create a new object given those modified copies.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    johnhelen is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Need Help in Removing Objects from list
    By pramod_r20 in forum New To Java
    Replies: 2
    Last Post: 12-10-2010, 12:19 PM
  2. add objects to list
    By javaMike in forum Advanced Java
    Replies: 1
    Last Post: 11-17-2009, 10:11 PM
  3. List of objects
    By bubbless in forum New To Java
    Replies: 0
    Last Post: 08-01-2009, 04:21 PM
  4. List of objects
    By rekha in forum New To Java
    Replies: 6
    Last Post: 03-20-2009, 11:39 AM
  5. Getting objects from a list
    By markyoung1984 in forum New To Java
    Replies: 4
    Last Post: 03-13-2008, 10:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •