//package votation;
import java.util.ArrayList;
import java.util.Random;
/*******************************************
* Completez le programme à partir d'ici.
*******************************************/
class Postulant{
private int electeurs;
public Postulant
(String leNom
){nom
=leNom
;electeurs
=0;} public void elect(){electeurs+=1;}
public void init(){electeurs=0;}
public int getVotes(){return electeurs;}
public String getNom
(){return nom
;} }
//===============
class Scrutin{
private ArrayList<Postulant> postulants;
private int maxVotants;
private int votants;
private int date;
private ArrayList<Vote> votes;
public Scrutin(ArrayList<Postulant> cand, int max, int jour){
//postulants=new ArrayList<Postulant>();
postulants=cand;
maxVotants=max;
date=jour;
votants=0;
votes=new ArrayList<Vote>();
}
public void calculerVotants(){
this.votants=0;
for(Postulant p: postulants){
this.votants+=p.getVotes();
}
}
public void init(){
for(Postulant p: postulants){
p.init();
}
}
public void resultats(){
this.calculerVotants();
if (this.votants<=0){
System.
out.
println("Scrutin annule, pas de votants"); return;
}
double taux=(this.votants*100.0)/this.maxVotants;
System.
out.
format("Taux de participation -> %.1f pour cent\n", taux
); System.
out.
println("Nombre effectif de votants -> "+this.
votants); System.
out.
println("Le chef choisi est -> "+this.
gagnant().
getNom()); System.
out.
println("\nRepartition des electeurs "); for(Postulant p: postulants){
System.
out.
format(p.
getNom()+" -> %.1f pour cent des electeurs\n",
(100.0*p.
getVotes())/this.
votants); }
}
/*public String gagnant(){
Postulant cand=postulants.get(0);
for(int i=1;i<postulants.size();i++){
if(postulants.get(i).getVotes()>=cand.getVotes()){
cand=postulants.get(i);
}
}
return cand.getNom();
}*/
public Postulant gagnant(){
/*Postulant cand=postulants.get(0);
for(int i=1;i<postulants.size();i++){
if(postulants.get(i).getVotes()>=cand.getVotes()){
cand=postulants.get(i);
}
}*/
Postulant cand=postulants.get(0);
for(Postulant p:postulants){
if(p.getVotes()>=cand.getVotes()){
cand=p;
}
}
return cand;
}
public void compterVotes(){
//int n=0;
for(Vote v:votes){
if(!v.estInvalide()){
//buscar el candidato
/*for(Postulant p:postulants){
if(v.getPostulant().equals(p)){
p.elect();
}
}*/
v.getPostulant().elect();
}
}
}
private boolean isPair(int n){
return n%2==0;
//return (n==0 || (n%2==0));
}
public void simuler(double taux, int jourVote){
//this.votants=(int)(this.maxVotants*taux);
int papeletas=(int)(this.maxVotants*taux);
for(int i=0;i<=papeletas;i++){
int candNum=Utils.randomInt(postulants.size());
boolean pair=isPair(i);
switch (i%3) {
case 0:
//Boletín electrónico a candNum
votes.add(new BulletinElectronique(postulants.get(candNum),jourVote,this.date));
break;
case 1:
//Boletín papel a candNum+ si pair pas signé
if(pair){
votes.add(new BulletinPapier(postulants.get(candNum),jourVote,this.date,false));
}//else{
// votes.add(new BulletinPapier(postulants.get(candNum),jourVote,this.date,true));
//}
break;
case 3:
//Boletín correo a candNum+ si pair signé
if(pair){
votes.add(new BulletinCourrier(postulants.get(candNum),jourVote,this.date,false));
}//else{
// votes.add(new BulletinCourrier(postulants.get(candNum),jourVote,this.date,true));
//}
//votes.add(new BulletinPapier(postulants.get(candNum),jourVote,this.date,pair));
break;
default:
break;
}
}
}
}
//===============
interface CheckBulletin{
public abstract boolean checkDate();
}
//===============
abstract class Vote{
private Postulant postulant;
private int date;
private int limite;
public Vote(Postulant p, int jour, int lim){
postulant=p;
date=jour;
limite=lim;
}
public abstract boolean estInvalide();
public Postulant getPostulant(){return postulant;}
public int getDate(){return date;}
public int getDateLimite(){return limite;}
String s
="pour "+this.
postulant.
getNom()+" -> "; if(this.estInvalide()){
s+="invalide";
}else{
s+="valide";
}
return s;
}
}
//=========================
class BulletinPapier extends Vote{
private boolean signe;
public BulletinPapier(Postulant p, int jour, int lim, boolean avecSignature){
super(p, jour, lim);
signe=avecSignature;
}
public BulletinPapier(Postulant p, int jour, int lim){
super(p, jour, lim);
signe=false;
}
public boolean estInvalide(){
//if(signe){return true;}else{return false;}
return !signe;
}
return "vote par bulletin papier "+super.toString();
}
}
//=========================
class BulletinElectronique extends Vote implements CheckBulletin{
public BulletinElectronique(Postulant p, int jour, int lim){
super(p, jour, lim);
}
public boolean estInvalide(){
return !this.checkDate();
}
return "vote electronique "+super.toString();
}
public boolean checkDate() {
//return (this.getDate()>=(this.getDateLimite()-2));
return ((this.getDateLimite()-2)>=this.getDate());
}
}
//=========================
class BulletinCourrier extends BulletinPapier implements CheckBulletin{
public BulletinCourrier(Postulant p, int jour, int lim, boolean avecSignature){
super(p, jour, lim, avecSignature);
}
public boolean estInvalide(){
//System.out.println(super.estInvalide()+" et "+!this.checkDate());
return super.estInvalide() || (!this.checkDate());
}
return "envoi par courrier d'un "+super.toString();
}
public boolean checkDate() {
//return (this.getDate()>=this.getDateLimite());
return (this.getDateLimite()>=this.getDate());
}
}
/*******************************************
* Ne pas modifier les parties fournies
* pour pr'eserver les fonctionnalit'es et
* le jeu de test fourni.
* Votre programme sera test'e avec d'autres
* donn'ees.
*******************************************/
class Utils {
// NE PAS UTILISER CETTE METHODE DANS LES PARTIES A COMPLETER
public static void setSeed(long seed) {
RANDOM.setSeed(seed);
}
// génère un entier entre 0 et max (max non compris)
public static int randomInt(int max) {
return RANDOM.nextInt(max);
}
}
/**
* Classe pour tester la simulation
*/
class Votation {
public static void main
(String args
[]) { // TEST 1
System.
out.
println("Test partie I:"); System.
out.
println("--------------");
ArrayList<Postulant> postulants = new ArrayList<Postulant>();
postulants.add(new Postulant("Tarek Oxlama"));
postulants.add(new Postulant("Nicolai Tarcozi"));
postulants.add(new Postulant("Vlad Imirboutine"));
postulants.add(new Postulant("Angel Anerckjel"));
postulants.get(0).elect();
postulants.get(0).elect();
postulants.get(1).elect();
postulants.get(1).elect();
postulants.get(1).elect();
postulants.get(2).elect();
postulants.get(3).elect();
postulants.get(3).elect();
postulants.get(3).elect();
postulants.get(3).elect();
// 30 -> nombre maximal de votants
// 15 jour du scrutin
Scrutin scrutin = new Scrutin(postulants, 30, 15);
scrutin.calculerVotants();
scrutin.resultats();
// FIN TEST 1
// TEST 2
System.
out.
println("Test partie II:"); System.
out.
println("---------------");
scrutin = new Scrutin(postulants, 30, 15);
scrutin.init();
// tous les bulletins passent le check de la date
// les parametres de simuler sont dans l'ordre:
// le pourcentage de votants et le jour du vote
scrutin.simuler(0.75, 12);
scrutin.compterVotes();
//scrutin.calculerVotants();
scrutin.resultats();
scrutin = new Scrutin(postulants, 30, 15);
scrutin.init();
// seuls les bulletins papier non courrier passent
scrutin.simuler(0.75, 15);
scrutin.compterVotes();
scrutin.resultats();
scrutin = new Scrutin(postulants, 30, 15);
scrutin.init();
// les bulletins electroniques ne passent pas
scrutin.simuler(0.75, 15);
scrutin.compterVotes();
scrutin.resultats();
//FIN TEST 2
scrutin = new Scrutin(postulants, 120, 23);
scrutin.init();
scrutin.simuler(0.50, 21);
scrutin.compterVotes();
//System.out.println(scrutin.gagnant().getNom());
scrutin.resultats();
}
}