Java 标识索引号。 */ 公共无效列表文件(int索引) { System.out.print(candidateList.get(index.getName()); System.out.println(“+candidateList.get(index.getParty()); } /** *此方法将使用户能够删除候选项。 */ 公共void removeFile(int索引) { 如果(索引>=0&&index

Java 标识索引号。 */ 公共无效列表文件(int索引) { System.out.print(candidateList.get(index.getName()); System.out.println(“+candidateList.get(index.getParty()); } /** *此方法将使用户能够删除候选项。 */ 公共void removeFile(int索引) { 如果(索引>=0&&index,java,methods,arraylist,bluej,Java,Methods,Arraylist,Bluej,} 在候选类中,我刚刚添加了上面提到的getName()和getParty()方法 关于这一xD中的大量工作,首先:永远不会调用setCandidateList-应该从构造函数中调用它。供将来参考:如果它不是setter/getter模式中的setter,请不要将其称为setXX()(希望您稍后了解这一点,以及何时使用和何时不使用:)。第二:看起来你不应该存储候选人的名字,而是实例化Candidates并将其放入支持列表private ArrayList candidateList。因此,重新编

}

在候选类中,我刚刚添加了上面提到的
getName()
getParty()
方法


关于这一xD中的大量工作,首先:永远不会调用setCandidateList-应该从构造函数中调用它。供将来参考:如果它不是setter/getter模式中的setter,请不要将其称为
setXX()
(希望您稍后了解这一点,以及何时使用和何时不使用:)。第二:看起来你不应该存储候选人的名字,而是实例化
Candidate
s并将其放入支持列表
private ArrayList candidateList。因此,重新编写构造函数以接受分隔符上的
candidateList
拆分,然后构造
Candidate
s
private ArrayList<candidate> candidateList;

/**
 * The following constructor will establish the Candidate List
 */
public VotingMachine()
{
    candidateList = new ArrayList<String>();
}
public void addNewCandidate(String name, char partySymbol)
{
    candidate candid = new candidate(name, partySymbol);// this will call the candidate constructor
    candidateList.add(candid);//add that object in ArrayList


}    
public void printCandidateInfo()
{
    for (int index=0; index < candidateList.size(); index++)
    {
        System.out.println(candidateList.get(index));
    }
}
public String getName()
{
    return name;

}

/**
 * Method to retrieve the Candidate's party for the caller.
 * 
 * @return
 */
public char getParty()
{
    return party;

}
public void listFile(int index)
{
      if(index >= 0 && index < candidateList.size()){
       String filename = candidateList.get(index);
       System.out.println(filename);
   }
import java.util.ArrayList;

/**
* These are the fields for the Voting Machine Class.
*/

public class VotingMachine
{
private ArrayList<Candidate> candidateList;

/**
 * The following constructor will establish the Candidate List
 */
public VotingMachine()
{
    candidateList = new ArrayList<>();
}

/**
 * This method will store the Candidates for the Candidate List   
 */
public void addNewCandidate(String name, char partySymbol)
{
    Candidate candid = new Candidate(name, partySymbol);// this will call the candidate constructor
    candidateList.add(candid);//add that object in ArrayList
}    

/**
 * This method will display the entire Candidate List.
 */
public void printCandidateInfo()
{
    for (int index=0; index < candidateList.size(); index++)
    {
        System.out.print(candidateList.get(index).getName());
        System.out.println("  " + candidateList.get(index).getParty());
    }
}

/**
 * Method to the number of Candidates in the CandidateList Arraylist.
 */
public int getNumberofFiles()
{
    return candidateList.size();       
}

/**
* Method to select one candidate by first providing an index number.
*/
public void listFile(int index)
{
   System.out.print(candidateList.get(index).getName());
   System.out.println("  " + candidateList.get(index).getParty());
}

/**
 * This method will enable a user to remove a candidate.
 */
public void removeFile(int index)
{
    if(index >= 0 && index < candidateList.size()){
        candidateList.remove(index);
    }
}