fork download
  1. //package votation;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5.  
  6. /*******************************************
  7.  * Completez le programme à partir d'ici.
  8.  *******************************************/
  9.  
  10. class Postulant{
  11. private String nom;
  12. private int electeurs;
  13.  
  14. public Postulant(String leNom){nom=leNom;electeurs=0;}
  15. public void elect(){electeurs+=1;}
  16. public void init(){electeurs=0;}
  17. public int getVotes(){return electeurs;}
  18. public String getNom(){return nom;}
  19. }
  20. //===============
  21. class Scrutin{
  22. private ArrayList<Postulant> postulants;
  23. private int maxVotants;
  24. private int votants;
  25. private int date;
  26. private ArrayList<Vote> votes;
  27.  
  28. public Scrutin(ArrayList<Postulant> cand, int max, int jour){
  29. //postulants=new ArrayList<Postulant>();
  30. postulants=cand;
  31. maxVotants=max;
  32. date=jour;
  33. votants=0;
  34. votes=new ArrayList<Vote>();
  35. }
  36. public void calculerVotants(){
  37. this.votants=0;
  38. for(Postulant p: postulants){
  39. this.votants+=p.getVotes();
  40. }
  41. }
  42. public void init(){
  43. for(Postulant p: postulants){
  44. p.init();
  45. }
  46. }
  47. public void resultats(){
  48. this.calculerVotants();
  49. if (this.votants<=0){
  50. System.out.println("Scrutin annule, pas de votants");
  51. return;
  52. }
  53.  
  54. double taux=(this.votants*100.0)/this.maxVotants;
  55. System.out.format("Taux de participation -> %.1f pour cent\n", taux);
  56. System.out.println("Nombre effectif de votants -> "+this.votants);
  57. System.out.println("Le chef choisi est -> "+this.gagnant().getNom());
  58. System.out.println("\nRepartition des electeurs ");
  59. for(Postulant p: postulants){
  60. System.out.format(p.getNom()+" -> %.1f pour cent des electeurs\n",(100.0*p.getVotes())/this.votants);
  61. }
  62. System.out.println();
  63. }
  64. /*public String gagnant(){
  65. Postulant cand=postulants.get(0);
  66. for(int i=1;i<postulants.size();i++){
  67. if(postulants.get(i).getVotes()>=cand.getVotes()){
  68. cand=postulants.get(i);
  69. }
  70. }
  71. return cand.getNom();
  72. }*/
  73. public Postulant gagnant(){
  74. /*Postulant cand=postulants.get(0);
  75. for(int i=1;i<postulants.size();i++){
  76. if(postulants.get(i).getVotes()>=cand.getVotes()){
  77. cand=postulants.get(i);
  78. }
  79. }*/
  80. Postulant cand=postulants.get(0);
  81. for(Postulant p:postulants){
  82. if(p.getVotes()>=cand.getVotes()){
  83. cand=p;
  84. }
  85. }
  86. return cand;
  87. }
  88. public void compterVotes(){
  89. //int n=0;
  90. for(Vote v:votes){
  91. System.out.println(v);
  92. if(!v.estInvalide()){
  93. //buscar el candidato
  94. /*for(Postulant p:postulants){
  95. if(v.getPostulant().equals(p)){
  96. p.elect();
  97. }
  98. }*/
  99. v.getPostulant().elect();
  100. }
  101.  
  102. }
  103. }
  104. private boolean isPair(int n){
  105. return n%2==0;
  106. //return (n==0 || (n%2==0));
  107. }
  108. public void simuler(double taux, int jourVote){
  109. //this.votants=(int)(this.maxVotants*taux);
  110. int papeletas=(int)(this.maxVotants*taux);
  111. for(int i=0;i<=papeletas;i++){
  112. int candNum=Utils.randomInt(postulants.size());
  113. boolean pair=isPair(i);
  114. switch (i%3) {
  115. case 0:
  116. //Boletín electrónico a candNum
  117. votes.add(new BulletinElectronique(postulants.get(candNum),jourVote,this.date));
  118. break;
  119. case 1:
  120. //Boletín papel a candNum+ si pair pas signé
  121. if(pair){
  122. votes.add(new BulletinPapier(postulants.get(candNum),jourVote,this.date,false));
  123. }//else{
  124. // votes.add(new BulletinPapier(postulants.get(candNum),jourVote,this.date,true));
  125. //}
  126. break;
  127. case 3:
  128. //Boletín correo a candNum+ si pair signé
  129. if(pair){
  130. votes.add(new BulletinCourrier(postulants.get(candNum),jourVote,this.date,false));
  131. }//else{
  132. // votes.add(new BulletinCourrier(postulants.get(candNum),jourVote,this.date,true));
  133. //}
  134. //votes.add(new BulletinPapier(postulants.get(candNum),jourVote,this.date,pair));
  135. break;
  136. default:
  137.  
  138. break;
  139. }
  140. }
  141. }
  142. }
  143. //===============
  144. interface CheckBulletin{
  145. public abstract boolean checkDate();
  146. }
  147. //===============
  148. abstract class Vote{
  149. private Postulant postulant;
  150. private int date;
  151. private int limite;
  152.  
  153. public Vote(Postulant p, int jour, int lim){
  154. postulant=p;
  155. date=jour;
  156. limite=lim;
  157. }
  158. public abstract boolean estInvalide();
  159. public Postulant getPostulant(){return postulant;}
  160. public int getDate(){return date;}
  161. public int getDateLimite(){return limite;}
  162. public String toString(){
  163. String s="pour "+this.postulant.getNom()+" -> ";
  164. if(this.estInvalide()){
  165. s+="invalide";
  166. }else{
  167. s+="valide";
  168. }
  169.  
  170. return s;
  171. }
  172. }
  173. //=========================
  174. class BulletinPapier extends Vote{
  175. private boolean signe;
  176. public BulletinPapier(Postulant p, int jour, int lim, boolean avecSignature){
  177. super(p, jour, lim);
  178. signe=avecSignature;
  179. }
  180. public BulletinPapier(Postulant p, int jour, int lim){
  181. super(p, jour, lim);
  182. signe=false;
  183. }
  184. public boolean estInvalide(){
  185. //if(signe){return true;}else{return false;}
  186. return !signe;
  187. }
  188. public String toString(){
  189. return "vote par bulletin papier "+super.toString();
  190. }
  191.  
  192. }
  193. //=========================
  194. class BulletinElectronique extends Vote implements CheckBulletin{
  195. public BulletinElectronique(Postulant p, int jour, int lim){
  196. super(p, jour, lim);
  197. }
  198. public boolean estInvalide(){
  199.  
  200. return !this.checkDate();
  201. }
  202. public String toString(){
  203. return "vote electronique "+super.toString();
  204. }
  205. public boolean checkDate() {
  206. //return (this.getDate()>=(this.getDateLimite()-2));
  207. return ((this.getDateLimite()-2)>=this.getDate());
  208. }
  209. }
  210. //=========================
  211. class BulletinCourrier extends BulletinPapier implements CheckBulletin{
  212. public BulletinCourrier(Postulant p, int jour, int lim, boolean avecSignature){
  213. super(p, jour, lim, avecSignature);
  214. }
  215. public boolean estInvalide(){
  216. //System.out.println(super.estInvalide()+" et "+!this.checkDate());
  217. return super.estInvalide() || (!this.checkDate());
  218. }
  219. public String toString(){
  220. return "envoi par courrier d'un "+super.toString();
  221. }
  222. public boolean checkDate() {
  223. //return (this.getDate()>=this.getDateLimite());
  224. return (this.getDateLimite()>=this.getDate());
  225. }
  226. }
  227. /*******************************************
  228.  * Ne pas modifier les parties fournies
  229.  * pour pr'eserver les fonctionnalit'es et
  230.  * le jeu de test fourni.
  231.  * Votre programme sera test'e avec d'autres
  232.  * donn'ees.
  233.  *******************************************/
  234.  
  235. class Utils {
  236.  
  237. private static final Random RANDOM = new Random();
  238.  
  239. // NE PAS UTILISER CETTE METHODE DANS LES PARTIES A COMPLETER
  240. public static void setSeed(long seed) {
  241. RANDOM.setSeed(seed);
  242. }
  243.  
  244. // génère un entier entre 0 et max (max non compris)
  245. public static int randomInt(int max) {
  246. return RANDOM.nextInt(max);
  247. }
  248. }
  249.  
  250. /**
  251.  * Classe pour tester la simulation
  252.  */
  253.  
  254. class Votation {
  255.  
  256. public static void main(String args[]) {
  257. // TEST 1
  258. System.out.println("Test partie I:");
  259. System.out.println("--------------");
  260.  
  261. ArrayList<Postulant> postulants = new ArrayList<Postulant>();
  262. postulants.add(new Postulant("Tarek Oxlama"));
  263. postulants.add(new Postulant("Nicolai Tarcozi"));
  264. postulants.add(new Postulant("Vlad Imirboutine"));
  265. postulants.add(new Postulant("Angel Anerckjel"));
  266.  
  267. postulants.get(0).elect();
  268. postulants.get(0).elect();
  269.  
  270. postulants.get(1).elect();
  271. postulants.get(1).elect();
  272. postulants.get(1).elect();
  273.  
  274. postulants.get(2).elect();
  275.  
  276. postulants.get(3).elect();
  277. postulants.get(3).elect();
  278. postulants.get(3).elect();
  279. postulants.get(3).elect();
  280.  
  281. // 30 -> nombre maximal de votants
  282. // 15 jour du scrutin
  283. Scrutin scrutin = new Scrutin(postulants, 30, 15);
  284. scrutin.calculerVotants();
  285. scrutin.resultats();
  286.  
  287. // FIN TEST 1
  288.  
  289. // TEST 2
  290. System.out.println("Test partie II:");
  291. System.out.println("---------------");
  292.  
  293. scrutin = new Scrutin(postulants, 30, 15);
  294. scrutin.init();
  295. // tous les bulletins passent le check de la date
  296. // les parametres de simuler sont dans l'ordre:
  297. // le pourcentage de votants et le jour du vote
  298. scrutin.simuler(0.75, 12);
  299. scrutin.compterVotes();
  300. //scrutin.calculerVotants();
  301. scrutin.resultats();
  302.  
  303. scrutin = new Scrutin(postulants, 30, 15);
  304. scrutin.init();
  305. // seuls les bulletins papier non courrier passent
  306. scrutin.simuler(0.75, 15);
  307. scrutin.compterVotes();
  308. scrutin.resultats();
  309.  
  310. scrutin = new Scrutin(postulants, 30, 15);
  311. scrutin.init();
  312. // les bulletins electroniques ne passent pas
  313. scrutin.simuler(0.75, 15);
  314. scrutin.compterVotes();
  315. scrutin.resultats();
  316. //FIN TEST 2
  317.  
  318. scrutin = new Scrutin(postulants, 120, 23);
  319. scrutin.init();
  320. scrutin.simuler(0.50, 21);
  321. scrutin.compterVotes();
  322. //System.out.println(scrutin.gagnant().getNom());
  323. scrutin.resultats();
  324.  
  325. }
  326. }
  327.  
Success #stdin #stdout 0.08s 380544KB
stdin
Standard input is empty
stdout
Test partie I:
--------------
Taux de participation -> 33.3 pour cent
Nombre effectif de votants -> 10
Le chef choisi est -> Angel Anerckjel

Repartition des electeurs 
Tarek Oxlama -> 20.0 pour cent des electeurs
Nicolai Tarcozi -> 30.0 pour cent des electeurs
Vlad Imirboutine -> 10.0 pour cent des electeurs
Angel Anerckjel -> 40.0 pour cent des electeurs

Test partie II:
---------------
vote electronique pour Vlad Imirboutine -> valide
vote electronique pour Angel Anerckjel -> valide
vote par bulletin papier pour Angel Anerckjel -> invalide
vote electronique pour Tarek Oxlama -> valide
vote electronique pour Vlad Imirboutine -> valide
vote par bulletin papier pour Angel Anerckjel -> invalide
vote electronique pour Nicolai Tarcozi -> valide
vote electronique pour Vlad Imirboutine -> valide
vote par bulletin papier pour Nicolai Tarcozi -> invalide
vote electronique pour Tarek Oxlama -> valide
vote electronique pour Vlad Imirboutine -> valide
vote par bulletin papier pour Nicolai Tarcozi -> invalide
Taux de participation -> 26.7 pour cent
Nombre effectif de votants -> 8
Le chef choisi est -> Vlad Imirboutine

Repartition des electeurs 
Tarek Oxlama -> 25.0 pour cent des electeurs
Nicolai Tarcozi -> 12.5 pour cent des electeurs
Vlad Imirboutine -> 50.0 pour cent des electeurs
Angel Anerckjel -> 12.5 pour cent des electeurs

vote electronique pour Nicolai Tarcozi -> invalide
vote electronique pour Angel Anerckjel -> invalide
vote par bulletin papier pour Nicolai Tarcozi -> invalide
vote electronique pour Nicolai Tarcozi -> invalide
vote electronique pour Angel Anerckjel -> invalide
vote par bulletin papier pour Angel Anerckjel -> invalide
vote electronique pour Vlad Imirboutine -> invalide
vote electronique pour Angel Anerckjel -> invalide
vote par bulletin papier pour Tarek Oxlama -> invalide
vote electronique pour Nicolai Tarcozi -> invalide
vote electronique pour Angel Anerckjel -> invalide
vote par bulletin papier pour Vlad Imirboutine -> invalide
Scrutin annule, pas de votants
vote electronique pour Angel Anerckjel -> invalide
vote electronique pour Angel Anerckjel -> invalide
vote par bulletin papier pour Angel Anerckjel -> invalide
vote electronique pour Angel Anerckjel -> invalide
vote electronique pour Tarek Oxlama -> invalide
vote par bulletin papier pour Vlad Imirboutine -> invalide
vote electronique pour Tarek Oxlama -> invalide
vote electronique pour Angel Anerckjel -> invalide
vote par bulletin papier pour Vlad Imirboutine -> invalide
vote electronique pour Angel Anerckjel -> invalide
vote electronique pour Tarek Oxlama -> invalide
vote par bulletin papier pour Angel Anerckjel -> invalide
Scrutin annule, pas de votants
vote electronique pour Vlad Imirboutine -> valide
vote electronique pour Vlad Imirboutine -> valide
vote par bulletin papier pour Angel Anerckjel -> invalide
vote electronique pour Angel Anerckjel -> valide
vote electronique pour Nicolai Tarcozi -> valide
vote par bulletin papier pour Nicolai Tarcozi -> invalide
vote electronique pour Nicolai Tarcozi -> valide
vote electronique pour Angel Anerckjel -> valide
vote par bulletin papier pour Angel Anerckjel -> invalide
vote electronique pour Tarek Oxlama -> valide
vote electronique pour Angel Anerckjel -> valide
vote par bulletin papier pour Nicolai Tarcozi -> invalide
vote electronique pour Tarek Oxlama -> valide
vote electronique pour Angel Anerckjel -> valide
vote par bulletin papier pour Angel Anerckjel -> invalide
vote electronique pour Tarek Oxlama -> valide
vote electronique pour Angel Anerckjel -> valide
vote par bulletin papier pour Nicolai Tarcozi -> invalide
vote electronique pour Nicolai Tarcozi -> valide
vote electronique pour Vlad Imirboutine -> valide
vote par bulletin papier pour Angel Anerckjel -> invalide
vote electronique pour Tarek Oxlama -> valide
vote electronique pour Tarek Oxlama -> valide
vote par bulletin papier pour Angel Anerckjel -> invalide
vote electronique pour Angel Anerckjel -> valide
vote electronique pour Vlad Imirboutine -> valide
vote par bulletin papier pour Tarek Oxlama -> invalide
vote electronique pour Angel Anerckjel -> valide
vote electronique pour Tarek Oxlama -> valide
vote par bulletin papier pour Angel Anerckjel -> invalide
vote electronique pour Angel Anerckjel -> valide
Taux de participation -> 17.5 pour cent
Nombre effectif de votants -> 21
Le chef choisi est -> Angel Anerckjel

Repartition des electeurs 
Tarek Oxlama -> 28.6 pour cent des electeurs
Nicolai Tarcozi -> 14.3 pour cent des electeurs
Vlad Imirboutine -> 19.0 pour cent des electeurs
Angel Anerckjel -> 38.1 pour cent des electeurs