The form consist of three textfields and two buttons(add and show buttons). User enters the candidate first name and last name and the respective number of votes received in textfields.On clicking the add button the candidate object is created and the candidate object is added to the arraylist object. On clicking the show button gives the actual size of the arraylist which is not happening.
Candidates.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author hp
*/
//import java.io.*;
public class Candidates {
String firstname;
String lastname;
int votesreceived;
public Candidates(String fname,String lname,int votes)
{
this.firstname=fname;
this.lastname=lname;
this.votesreceived=votes;
}
public String getFirstName()
{
return firstname;
}
public String getLastName()
{
return lastname;
}
public int getVotesReceived()
{
return votesreceived;
}
}
VotesForm.java
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
String fname=firstname.getText();
String lname=lastname.getText();
int votes=Integer.parseInt(votesreceived.getText());
//System.out.println("Checking the voting system");
System.out.println("Creating the candidate object");
CandidateList canlist=new CandidateList();
canlist.addCandidates(fname,lname,votes);
firstname.setText("");
lastname.setText("");
votesreceived.setText("");
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
CandidateList canshow=new CandidateList();
//canshow.showCandidates();
//canshow.test();
canshow.showCandidates();
}
CandidateList.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author hp
*/
import java.util.*;
import java.util.ArrayList.*;
public class CandidateList {
//instance variables
//private List canlist;
ArrayList canlist;
public CandidateList()
{
// canlist=new LinkedList();
canlist=new ArrayList();
}
//adding the candidates in the list
public void addCandidates(String fname,String lname,int votes)
{
Candidates can=new Candidates(fname,lname,votes);
canlist.add(can);
System.out.println("candidates are added to the arraylist");
System.out.println(canlist.size());
}
public void showCandidates()
{
int i=canlist.size();
//gives the size of the arraylist
System.out.println(i);
// for(int i=0;i=canlist.size();i++)
// {
//}
//System.out.println(canlist);
}
}