使用来自其 getter 方法的另一个 类 值
Using another classes values from its getter method
我花了一段时间尝试不同的事情来尝试让这个家庭作业正确地工作,但我想不通,这是我认为是在盯着我看的最后一部分。当我输入名字和姓氏并按添加帐户然后确认它应该将一个帐户添加到数组列表然后当我按帐户数量时它应该显示总共有多少个帐户,但它一直显示 0 .
基本帐户列表
import java.util.*;
public class BasicAccountList
{
private ArrayList < BasicAccount> accounts;
/**
* Create a BasicAccount.
*/
public BasicAccountList()
{
accounts = new ArrayList < BasicAccount>();
}
/**
* Add an account to this account list.
* @param account the accountobject to be added
*/
public void addAccount(BasicAccount account)
{
accounts.add(account);
}
/**
* Return the number of accounts currently held.
*
* @return the number of accounts
*/
public int getNumberOfAccounts()
{
return accounts.size();
}
}
基本账户
public class BasicAccount
{
private Name name;
private String accountNumber;
/**
* Constructor for objects of class Account.
* The number of pointsHeld should should be set to
* the supplied value.
*
* @param fName The Account Holder's first name
* @param lName The Account Holder's last name
* @param acctNumber The account number
*/
public BasicAccount(String fName, String lName, String acctNumber)
{
name = new Name (fName, lName);
accountNumber = acctNumber;
}
// accessors
/**
* Get the Account Holder's first name
*
* @return the Account Holder's first name
*/
public String getFirstName()
{
return name.getFirst();
}
/**
* Get the Account Holder's last name
*
* @return the Account Holder's last name
*/
public String getLastName()
{
return name.getLast();
}
/**
* Get the Account Holder's account Number
*
* @return the Account Holder's account number
*/
public String getAccountNumber()
{
return accountNumber;
}
public void printAccountDetails()
{
System.out.println( toString());
}
/**
* Return details of an account as a formated string
*
* @return the account details of a particular account
*/
public String toString()
{
String output = accountNumber + " ";
output = output + name.toString() + "\n";
return output;
}
// mutators
/**
* Change the first name
*
* @param fName the new first name
*
*/
public void setFirstName(String fName)
{
name.setFirst (fName);
}
/**
* Change the last name
*
* @param lName the new last name
*
*/
public void setLastName(String lName)
{
name.setLast(lName);
}
} // end Account class
GUI中的相关代码class
/**
* Write a description of class HW4GUI here.
*
* @author (your name)
* @version (a version number or a 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);
}
}
您在 actionPerformed
方法中创建了另一个 BasicAccountList
。这意味着,每次单击按钮时,都会生成一个新的 BasicAccountList
并在此列表上执行所有操作,而不是 HW4GUI
持有的列表。
与其在 class 实现 JFrame
中实现 ActionListener
,不如在单独的 class 中实现(可能在匿名 class,但任何 class 都可以)。
现在您可以有两个单独的 classes 实现 ActionListener
,因此可以有两个单独的 actionPerformed
实现,每个按钮一个。
将这些 ActionListener
附加到相应的按钮上,您就可以开始了。
注意:帐户列表应该是框架的成员,因此您可以在 ActionListener
之间共享它。
祝你好运。
我花了一段时间尝试不同的事情来尝试让这个家庭作业正确地工作,但我想不通,这是我认为是在盯着我看的最后一部分。当我输入名字和姓氏并按添加帐户然后确认它应该将一个帐户添加到数组列表然后当我按帐户数量时它应该显示总共有多少个帐户,但它一直显示 0 .
基本帐户列表
import java.util.*;
public class BasicAccountList
{
private ArrayList < BasicAccount> accounts;
/**
* Create a BasicAccount.
*/
public BasicAccountList()
{
accounts = new ArrayList < BasicAccount>();
}
/**
* Add an account to this account list.
* @param account the accountobject to be added
*/
public void addAccount(BasicAccount account)
{
accounts.add(account);
}
/**
* Return the number of accounts currently held.
*
* @return the number of accounts
*/
public int getNumberOfAccounts()
{
return accounts.size();
}
}
基本账户
public class BasicAccount
{
private Name name;
private String accountNumber;
/**
* Constructor for objects of class Account.
* The number of pointsHeld should should be set to
* the supplied value.
*
* @param fName The Account Holder's first name
* @param lName The Account Holder's last name
* @param acctNumber The account number
*/
public BasicAccount(String fName, String lName, String acctNumber)
{
name = new Name (fName, lName);
accountNumber = acctNumber;
}
// accessors
/**
* Get the Account Holder's first name
*
* @return the Account Holder's first name
*/
public String getFirstName()
{
return name.getFirst();
}
/**
* Get the Account Holder's last name
*
* @return the Account Holder's last name
*/
public String getLastName()
{
return name.getLast();
}
/**
* Get the Account Holder's account Number
*
* @return the Account Holder's account number
*/
public String getAccountNumber()
{
return accountNumber;
}
public void printAccountDetails()
{
System.out.println( toString());
}
/**
* Return details of an account as a formated string
*
* @return the account details of a particular account
*/
public String toString()
{
String output = accountNumber + " ";
output = output + name.toString() + "\n";
return output;
}
// mutators
/**
* Change the first name
*
* @param fName the new first name
*
*/
public void setFirstName(String fName)
{
name.setFirst (fName);
}
/**
* Change the last name
*
* @param lName the new last name
*
*/
public void setLastName(String lName)
{
name.setLast(lName);
}
} // end Account class
GUI中的相关代码class
/**
* Write a description of class HW4GUI here.
*
* @author (your name)
* @version (a version number or a 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);
}
}
您在 actionPerformed
方法中创建了另一个 BasicAccountList
。这意味着,每次单击按钮时,都会生成一个新的 BasicAccountList
并在此列表上执行所有操作,而不是 HW4GUI
持有的列表。
与其在 class 实现 JFrame
中实现 ActionListener
,不如在单独的 class 中实现(可能在匿名 class,但任何 class 都可以)。
现在您可以有两个单独的 classes 实现 ActionListener
,因此可以有两个单独的 actionPerformed
实现,每个按钮一个。
将这些 ActionListener
附加到相应的按钮上,您就可以开始了。
注意:帐户列表应该是框架的成员,因此您可以在 ActionListener
之间共享它。
祝你好运。