So I wrote up a simple voting GUI, but I havent been able to figure out how to get it to display the current leader or how many votes they have. It displays the total number of votes correctly, however. Heres my code

import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * @author rhyspj
 *
 */
public class Election5 extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public class Candidate3 extends Candidate2 {
		
		
		private static final long serialVersionUID = 1L;
		public Candidate3(Color c, String name) {
	       super(c, name);
	    }
	    public int getVotes() {
	        return votes;
	    }
	    public String getName() {
			return name;	
	    }
	}
    private Vector<Candidate3> heroes;
    private JButton tote;

    public Election5() {
    	super();
        setVisible(true);
        setSize(600,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container me = getContentPane();
        heroes = new Vector<Candidate3>();
        heroes.add(new Candidate3(Color.red, "Spiderman"));
        heroes.add(new Candidate3(Color.black, "Batman"));
        heroes.add(new Candidate3(Color.magenta, "Thor"));
        heroes.add(new Candidate3(Color.blue, "Captain America"));
        heroes.add(new Candidate3(Color.green, "Ironman"));
        heroes.add(new Candidate3(Color.pink, "Superman"));

        tote = new JButton("Latest results appear (click me)");
        tote.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    tote.setText("Current leader is: " +getName() 
                    		+"with " +mostVotes() +" votes out of " +sumVotes());
                }
            });
        me.setLayout(new GridLayout(0,2));
        for (Candidate3 person : heroes) {
            me.add(person);
        }
        me.add(tote);
        validate();
    }
    public int mostVotes(){
    	int total = 0;
    	for (Candidate3 win : heroes) {
    		total = total + win.getVotes();
    	}
    	return total;
    }
    public int sumVotes() {
        int total = 0;
        for (Candidate3 hero : heroes) {
            total = total + hero.getVotes();
        }
        return total;
    }
    public static void main(String[] args) {
        new Election5();

}
}

I think it involves using an enhanced for loop to somehow get the candidate with most votes. But i dont know the code

ooo forgot heres the other class thats referenced

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;


@SuppressWarnings("serial")
public class Candidate2 extends JButton implements MouseListener {
	private Color color;
 	protected String name;
 	protected int votes;
	
	
	Candidate2(Color c, String name) {
		super();
		setSize(600,200);
		votes = 0;
		this.name = name;
        color = c;
        setForeground(color);
        setText(name+" has "+votes+" votes");
        addMouseListener(this);
	}
	
	public void registerVote() {
		votes++;
		setText(name+" has "+votes+" votes");
	}
	
	
	

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		registerVote();
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub	
	}
	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}

got it to work....

import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
/**
 * @author rhyspj
 *
 */
public class Election5 extends JFrame {
	/**
	 * 
	 */
	 Candidate3 spider,batman,thor,captain,superman,ironman;
	private static final long serialVersionUID = 1L;
	public class Candidate3 extends Candidate2 {
		
		
		private static final long serialVersionUID = 1L;
		public Candidate3(Color c, String name) {
	       super(c, name);
	    }
	    public int getVotes() {
	        return votes;
	    }
	    public String getName() {
			return name;	
	    }
	}
    private Vector<Candidate3> heroes;
    private JButton tote;

    public Election5() {
    	super();
        setVisible(true);
        setSize(600,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container me = getContentPane();
        heroes = new Vector<Candidate3>();

spider=new Candidate3(Color.red, "Spiderman");
     batman= new Candidate3(Color.black, "Batman");
   thor=new Candidate3(Color.magenta, "Thor");
   captain= new Candidate3(Color.blue, "Captain America");
   ironman=new Candidate3(Color.green, "Ironman");
   superman=new Candidate3(Color.pink, "Superman");

		heroes.add(spider);
        heroes.add(batman);
        heroes.add(thor);
        heroes.add(captain);
        heroes.add(ironman);
        heroes.add(superman);

        tote = new JButton("Latest results appear (click me)");
        tote.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    tote.setText("Current leader is: " +(getResult().getText()) 
                    		+"with " +(getResult().buttonvote) +" votes out of " +sumVotes());
                }
            });
        me.setLayout(new GridLayout(0,2));
        for (Candidate3 person : heroes) {
            me.add(person);
        }
        me.add(tote);
        validate();
    }
    public int mostVotes(){
    	int total = 0;
    	for (Candidate3 win : heroes) {
    		total = total + win.getVotes();
    	}
    	return total;
    }
    public int sumVotes() {
        int total = 0;
        for (Candidate3 hero : heroes) {
            total = total + hero.getVotes();
        }
        return total;
    }
	public Candidate2  getResult()
	{
int max=spider.buttonvote;
Candidate2 maxbutton=spider;
if(max<batman.buttonvote){max=batman.buttonvote;maxbutton=batman;}
 if(max<thor.buttonvote){max=thor.buttonvote;maxbutton=thor;}
 if(max<captain.buttonvote){max=captain.buttonvote;maxbutton=captain;}
 if(max<superman.buttonvote ){max=superman.buttonvote;maxbutton=superman;}
if(max<ironman.buttonvote){max=ironman.buttonvote;maxbutton=ironman;}

return maxbutton;
	}

    public static void main(String[] args) {
        new Election5();

}
}

Another class

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;


@SuppressWarnings("serial")
public class Candidate2 extends JButton implements MouseListener {
	private Color color;
 	protected String name;
 	protected int votes;
	int buttonvote=0;
	
	Candidate2(Color c, String name) {
		super();
		setSize(600,200);
		votes = 0;
		this.name = name;
        color = c;
        setForeground(color);
        setText(name+" has "+votes+" votes");
        addMouseListener(this);
	}
	
	public void registerVote() {
		votes++;
		buttonvote++;
		setText(name+" has "+votes+" votes");
	}
	

	

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		registerVote();
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub	
	}
	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}

I have just made it to work by adding some basic logic. Try yourself adding more logic like finding in case of tie(displaying tie between votes etc...)

Coding is not important.. understanding logic is important...
(Try to understand the logic..)

Edited 5 Years Ago by harinath_2007: code tag...

This article has been dead for over six months. Start a new discussion instead.