CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2008
    Posts
    1

    Creating an ArrayList as an attribute

    Hello All,

    Maybe it's time I started utilizing forums to help with my programs. I was assigned to create a program which connects to a database, access two tables, take information from the tables and store them as attributes of 2 objects. The only part that is hard for me is adding an ArrayList as an attribute. I am accessing a loan table and a payment table. Everything from the loan table is taken and assigned to the loan object and the payment table to payment object. The thing is every "loan" has payments. He wants us to add an ArrayList called "ArrayList<Payment>" to the loan class as an attribute of the loan object. I am having trouble creating an instance of this in my loan class... can anyone help? This assignment was already graded I just want to know how to do this for future reference (probably on my final exam as well). Thank you.

    he's the loan class
    Code:
    import java.util.ArrayList;
    
    public class Loan {
    	
    	private String loanID; //id no
    	private String startDate; //date
    	private double annualRate; //loan annual rate
    	private double loanAmt; //loan amount due
    	private int noYears; //years on loan
    	private ArrayList<Payment> payments; //is this correct?
    	
    	public Loan (String l, String s, double ar, double la, int ny, ArrayList<Payment> p)
    	{ loanID = l;
    		startDate = s;
    		annualRate = ar;
    		loanAmt = la;
    		noYears = ny;
    		ArrayList<Payment> payments = new ArrayList<Payment>(); //this doesn't work
    		
    		
    		/*public Loan ()
    		{ this.loanID= loanID;
    			this.startDate = startDate;
    			this.annualRate= annualRate;
    			this.loanAmt = loanAmt;
    			this.noYears = noYears;
    			ArrayList<Payment> = ArrayList<Payment>();
    		//ArrayList <Payment> payments = new ArrayList <Payment>();
    		*/
    	}//end loan constructor
    	
    	
    	public void setloanID (String l)
    	{ 
    	loanID = l;
    		
    	}//end setloanID
    	
    	public String getloanID ()
    	{
    		return loanID;
    	}//end getloanID
    	
    	
    	public void setstartDate (String s)
    	{ 
    	startDate = s;
    		
    	}//end setstartDate
    	
    	public String getstartDate ()
    	{
    		return startDate;
    	}//end getstartDate
    	
    	public void setannualRate (double ar)
    	{
    		annualRate = ar;
    	}
    	public double getannualRate (double ar)
    	{
    		
    		return annualRate;
    	}//end getstartDate
    	public void setloanAmt (double la)
    {
    	loanAmt = la;
    }
    	public double getloanAmt (double la)
    	{
    		
    		return loanAmt;
    	}//end getstartDate
    	public void setnoYears (int ny)
    	{
    		noYears = ny;
    	}
    	public int getnoYears (int ny)
    	{
    		
    		return noYears;
    	}//end getstartDate
    	public ArrayList <Payment> getPayments ()
    	{
    		return payments;
    	}//end getPayments
    	
    }//end loan

  2. #2
    Join Date
    Apr 2007
    Posts
    442

    Re: Creating an ArrayList as an attribute

    In your constructor you are creating a new instance of ArrayList that survives only as long as the method is alive. Instead, you have the ArrayList in the parameters, and the instance variable... simply do this:

    this.payments=p;

    Or you could check if parameter p is null, in which case declare payments as a new ArrayList

    this.payments=new ArrayList<Payment>()

    That way you avoid having a possible null list.

    For some odd reason, you are tumbling on and on from one declaration of an ArrayList to another. Regardless that the variable names equal, they are not the same object, when declared anew (as in ArrayList<Payment> payments=new ArrayList<Payment>(), does not reference your instance variable where as payments=new ArrayList<Payment>() does). There is no namespace collision, because they are not declared within the same brackets. Also, there is a declaration with no variable name what so ever. You can experiment this, by eg. changing the name of the parameter ArrayList also to payments.

    Much adviced that you consult the class material, and/or Sun Java tutorials on how variable life span goes.
    Last edited by Londbrok; November 24th, 2008 at 02:29 AM.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured