java - Using another classes values from its getter method -
i've spent while trying different things try homework assignment work correctly can't figure out , it's last part presume staring me in face. when enter first name , last name , press add account , confirm should add account arraylist , when press no. of accounts should show me how many accounts there in total, keeps showing 0.
basicaccountlist
import java.util.*; public class basicaccountlist { private arraylist < basicaccount> accounts; /** * create basicaccount. */ public basicaccountlist() { accounts = new arraylist < basicaccount>(); } /** * add account account list. * @param account accountobject added */ public void addaccount(basicaccount account) { accounts.add(account); } /** * return number of accounts held. * * @return number of accounts */ public int getnumberofaccounts() { return accounts.size(); } }
basicaccount
public class basicaccount { private name name; private string accountnumber; /** * constructor objects of class account. * number of pointsheld should should set * supplied value. * * @param fname account holder's first name * @param lname account holder's last name * @param acctnumber account number */ public basicaccount(string fname, string lname, string acctnumber) { name = new name (fname, lname); accountnumber = acctnumber; } // accessors /** * account holder's first name * * @return account holder's first name */ public string getfirstname() { return name.getfirst(); } /** * account holder's last name * * @return account holder's last name */ public string getlastname() { return name.getlast(); } /** * account holder's account number * * @return account holder's account number */ public string getaccountnumber() { return accountnumber; } public void printaccountdetails() { system.out.println( tostring()); } /** * return details of account formated string * * @return account details of particular account */ public string tostring() { string output = accountnumber + " "; output = output + name.tostring() + "\n"; return output; } // mutators /** * change first name * * @param fname new first name * */ public void setfirstname(string fname) { name.setfirst (fname); } /** * change last name * * @param lname new last name * */ public void setlastname(string lname) { name.setlast(lname); } } // end account class
relevant code in gui class
/** * write description of class hw4gui here. * * @author (your name) * @version (a version number or date) */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class hw4gui extends jframe implements actionlistener { private basicaccountlist accounts; private jpanel buttonpanel; private jbutton jbtadd; private jbutton jbtnumber; private jbutton jbtquit; private jlabel jlbacctno; private jlabel jlbfname; private jlabel jlblname; private jtextfield jtfacctno; private jtextfield jtffname; private jtextfield jtflname; private int nextacctno; private jpanel textpanel; public hw4gui () { makeframe(); showframe(); nextacctno = 1001; } public void actionperformed(actionevent ae) { basicaccountlist accountlist = new basicaccountlist (); string item = ae.getactioncommand(); string firstnametext = jtffname.gettext(); string lastnametext = jtflname.gettext(); string finalaccountnumber = jtfacctno.gettext(); if(item.equals("no. of accounts")) { jbtadd.setenabled(false); jbtnumber.settext ("clear"); jlbacctno.settext("no. of accounts:"); //accounts.getnumberofaccounts(); basicaccount newaccount = new basicaccount(firstnametext, lastnametext, finalaccountnumber); string accounttotal = integer.tostring (accountlist.getnumberofaccounts()); jtfacctno.settext (accounttotal); } }
you create basicaccountlist
inside actionperformed
method. means, every time click button, generate new basicaccountlist
, perform operations on list, not 1 held hw4gui
.