Ardurum

eurovision 2/2

Feb 2nd, 2022 (edited)
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.02 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.util.Scanner;
  5.  
  6. /**
  7. * Classe pais que conté el nom, la puntuació i una taula dels vots que
  8. * guarda els punts que dona i a qui se'ls dona.
  9. *
  10. * @author copaPiston
  11. */
  12. class Pais {
  13.  
  14. String nom;
  15. int puntuacio;
  16. int vots[][] = new int[10][2];
  17. }
  18.  
  19. /**
  20. * Genera l'objecte pais, i crida les funcions perque el programa s'inicii.
  21. *
  22. * @author copaPiston
  23. */
  24. public class Main{
  25.  
  26. public static void main(String[] args) {
  27. int opc;
  28. final int L = 26;
  29. Pais[] pais = new Pais[L];
  30.  
  31. for (int i = 0; i < pais.length; i++) {
  32. pais[i] = new Pais();
  33. }
  34. generacioPunts(pais);
  35. do {
  36. opc = menu();
  37. opcions(opc, pais);
  38.  
  39. } while (opc != 0);
  40. System.out.println("Adeu!!");
  41. }
  42.  
  43. /**
  44. * Llegim els paisos.
  45. *
  46. * @param pais generam els paissos que participaran en aquesta votació.
  47. */
  48. public static void carregarPaisos(Pais[] pais) {
  49. int i=0;
  50. File fitxer = new File ("paisos.txt");
  51. try{
  52. Scanner entradaF = new Scanner(fitxer);
  53. if(fitxer.exists()){
  54. while(entradaF.hasNext() && i<26){
  55. String nom= entradaF.next();
  56. System.out.println(nom);
  57. pais[i].nom=nom;
  58. i++;
  59. }
  60. entradaF.close();
  61. }else{
  62. System.out.println("Aquest fitxer no existeix");
  63. }
  64. }catch(IOException ex){
  65. System.out.println("Hi ha errors");
  66. }
  67.  
  68. }
  69.  
  70.  
  71. public static void edicioAny(Pais[] pais){
  72. Scanner entrada = new Scanner(System.in);
  73. char opcio;
  74. int any;
  75.  
  76. System.out.print("A quin any correspon aquesta votació: ");
  77. any=entrada.nextInt();
  78. File eurovisio = new File ("eurovisio"+any+".txt");
  79.  
  80. System.out.print("Vols desar aquesta votació: ");
  81. opcio=entrada.next().charAt(0);
  82. opcio=Character.toLowerCase(opcio);
  83.  
  84. if(opcio=='s'){
  85. if (eurovisio.exists()) {
  86. System.out.println("El fitxer ja existeix");
  87. } else {
  88. try {
  89. PrintWriter sortida = new PrintWriter(eurovisio);
  90. for (int i = 0; i < pais.length; i++) {
  91. sortida.print(pais[i].nom+" "+pais[i].puntuacio);
  92. for (int j = 0; j < 10; j++) {
  93. sortida.print(" "+pais[i].vots[j][0]);
  94. }
  95. for (int j = 0; j < 10; j++) {
  96. sortida.print(" "+pais[i].vots[j][1]);
  97. }
  98. sortida.println();
  99.  
  100. }
  101. sortida.close();
  102. } catch (IOException ex) {
  103. System.out.println("Hi ha hagut algun error amb el fitxer");
  104. }
  105. }
  106. }
  107. }
  108.  
  109.  
  110. public static void consultarVictorias(){
  111. Scanner entrada = new Scanner(System.in);
  112. int victorias=0;
  113. String nompais;
  114. System.out.println("De quin pais vols saber les victories que té?");
  115. nompais = entrada.nextLine();
  116.  
  117. File edicions = new File ("edicions.txt");
  118. try{
  119. Scanner entradaF = new Scanner(edicions);
  120. if(edicions.exists()){
  121. while(entradaF.hasNext()){
  122. int edicio= entradaF.nextInt();
  123. File eurovisio = new File ("eurovisio"+edicio+".txt");
  124.  
  125. Scanner entradaE = new Scanner(eurovisio);
  126. if (eurovisio.exists()) {
  127. String nom = entradaE.next();
  128. System.out.println("El pais " + nom + " va guanyar la edicio de l'any" + edicio);
  129. if (nompais.equals(nom)) {
  130. victorias++;
  131. }
  132.  
  133. entradaE.close();
  134. } else {
  135. System.out.println("No hi ha concurs per a l'any "+edicio);
  136. }
  137.  
  138.  
  139. }
  140. entradaF.close();
  141. System.out.println("El pais "+nompais+" ha guanyat "+victorias+" vegades");
  142. }else{
  143. System.out.println("Aquest fitxer no existeix");
  144. }
  145. }catch(IOException ex){
  146. System.out.println("Hi ha errors");
  147. }
  148.  
  149.  
  150.  
  151. }
  152.  
  153. /**
  154. * Mostra per pantalla el menu, demana i després retorna l'opció.
  155. *
  156. * @return retorna l'opció escollida.
  157. */
  158. public static int menu() {
  159. Scanner entrada = new Scanner(System.in);
  160. int opc = 100;
  161.  
  162. do {
  163. System.out.print("\n MENU\n____________\n1. Simular votació\n"
  164. + "2. Consultar victories d'un pais\n3. A qui ha votat\n4. Qui l'ha votat\n0. Sortir\nOpcio: ");
  165. opc = entrada.nextInt();
  166. } while (opc < 0 || opc > 5);
  167.  
  168. return opc;
  169. }
  170.  
  171.  
  172. /**
  173. * Comprova si es la primera edició del concurs en aquesta execució del programa
  174. *
  175. * @param pais classe pais, amb el nom, la puntuació rebuda, vots donats i paisos a qui se'ls dona.
  176. * @return retorna un valor 1 o 0 per comprovar si aquesta és la primera edició o si no.
  177. */
  178. public static int primeraEdicio(Pais[] pais) {
  179. int primer = 1;
  180. for (int i = 0; i < pais.length; i++) {
  181. if (pais[i].puntuacio != 0) {
  182. primer = 0;
  183. }
  184. }
  185. return primer;
  186. }
  187.  
  188. /**
  189. * Segons l'opció escollida anem a un mètode diferent.
  190. *
  191. * @param opc reb l'opció escollida, segons la qual actuarà d'una manera distinta per cada opció.
  192. * @param pais
  193. */
  194. public static void opcions(int opc, Pais[] pais) {
  195. Scanner entrada = new Scanner(System.in);
  196. int primer, opcPrimer;
  197.  
  198. switch (opc) {
  199. case 1:
  200. primer = primeraEdicio(pais);
  201. if (primer == 1) {
  202. carregarPaisos(pais);
  203. comencarVots(pais);
  204. edicioAny(pais);
  205. } else {
  206. System.out.print("Vols iniciar la nova votació amb els mateixos països, diferents o sortir? [1=Mateixos 2=Diferents");
  207. opcPrimer = entrada.nextInt();
  208. if (opcPrimer == 2) {
  209. carregarPaisos(pais);
  210. comencarVots(pais);
  211. edicioAny(pais);
  212. } else if (opcPrimer == 1) {
  213. comencarVots(pais);
  214. edicioAny(pais);
  215. }
  216. }
  217. break;
  218. case 2:
  219. consultarVictorias();
  220. break;
  221. case 3:
  222. aQuiVota(pais);
  223. break;
  224. case 4:
  225. quiLhaVotat(pais);
  226. break;
  227. case 0:
  228. break;
  229. default:
  230. System.out.println("Opcio incorrecta torna a provar!");
  231. }
  232. }
  233.  
  234. /**
  235. *assigna els punts que poden donar els paisos a una filera d'una taula
  236. *
  237. * @param pais classe pais, amb el nom, la puntuació rebuda, vots donats i paisos a qui se'ls dona.
  238. */
  239. public static void generacioPunts(Pais[] pais) {
  240. for (int i = 0; i < pais.length; i++) {
  241. pais[i].vots[0][1] = 1;
  242. pais[i].vots[1][1] = 2;
  243. pais[i].vots[2][1] = 3;
  244. pais[i].vots[3][1] = 4;
  245. pais[i].vots[4][1] = 5;
  246. pais[i].vots[5][1] = 6;
  247. pais[i].vots[6][1] = 7;
  248. pais[i].vots[7][1] = 8;
  249. pais[i].vots[8][1] = 10;
  250. pais[i].vots[9][1] = 12;
  251. }
  252. }
  253.  
  254. /**
  255. * Comença la votació
  256. *
  257. * @param pais classe pais, amb el nom, la puntuació rebuda, vots donats i paisos a qui se'ls dona.
  258. */
  259. public static void comencarVots(Pais[] pais) {
  260.  
  261. for (int i = 0; i < pais.length; i++) {
  262. pais[i].puntuacio = 0;
  263. }
  264. for (int i = 0; i < pais.length; i++) {
  265. for (int j = 0; j < 10; j++) {
  266. boolean comprovat = false;
  267.  
  268. pais[i].vots[j][0] = (int) (Math.random() * 26);
  269.  
  270. for (int l = 0; l < j && j > 0; l++) {
  271. if (pais[i].vots[j][0] == pais[i].vots[l][0] || pais[i].vots[j][0] == i) {
  272. j--;
  273. comprovat = true;
  274. }
  275. }
  276. if (comprovat == false) {
  277. pais[pais[i].vots[j][0]].puntuacio += pais[i].vots[j][1];
  278. }
  279. }
  280. mostrarClassificacio(pais);
  281. }
  282. }
  283.  
  284. /**
  285. * Ordena la classificació de paisos de més votat a menys votat, amb possibilitat d'empats.
  286. *
  287. * @param pais classe pais, amb el nom, la puntuació rebuda, vots donats i paisos a qui se'ls dona.
  288. * @return retorna un auxiliar per poder detectar posteriorment si n'hi han empats.
  289. */
  290. public static int[][] ordenarClassificacio(Pais[] pais) {
  291. String auxNom;
  292. int auxPunts;
  293. int auxIndice;
  294. int auxVots[][] = new int[pais.length][2];
  295.  
  296. for (int i = 0; i < pais.length; i++) {
  297. auxVots[i][0] = i;
  298. for (int j = 0; j < pais.length; j++) {
  299. auxVots[j][1] = pais[j].puntuacio;
  300. }
  301. }
  302.  
  303. for (int i = 0; i < auxVots.length; i++) {
  304. for (int j = i + 1; j < auxVots.length; j++) {
  305. if (auxVots[i][1] < auxVots[j][1]) {
  306. auxIndice = auxVots[i][0];
  307. auxPunts = auxVots[i][1];
  308. auxVots[i][0] = auxVots[j][0];
  309. auxVots[i][1] = auxVots[j][1];
  310. auxVots[j][0] = auxIndice;
  311. auxVots[j][1] = auxPunts;
  312. }
  313. }
  314. }
  315. return auxVots;
  316. }
  317.  
  318. /**
  319. * Comprova si n'hi ha algun empat, i en cas de que hi hagi mira segons les normes qui té més vots 12, i després 10.
  320. *
  321. * @param auxVots auxiliar per comprovar si n'hi han empats.
  322. * @param pais classe pais, amb el nom, la puntuació rebuda, vots donats i paisos a qui se'ls dona.
  323. */
  324. public static void mirarEmpats(int auxVots[][],Pais[]pais) {
  325. int mesvotat1 = 0, mesvotat2 = 0, mesdotze1 = 0, mesdotze2 = 0, mesdeu1 = 0, mesdeu2 = 0;
  326. int auxPunts;
  327. int auxIndice;
  328.  
  329. for (int i = 0; i < auxVots.length - 1; i++) {
  330. if (auxVots[i][1] == auxVots[i + 1][1]) {
  331. for (int j = 0; j < pais.length - 1; j++) {
  332. for (int k = 0; k < pais[j].vots.length - 1; k++) {
  333. if (pais[j].vots[k][0] == auxVots[i][1]) {
  334. mesvotat1++;
  335. } else if (pais[j].vots[k + 1][0] == auxVots[i + 1][1]) {
  336. mesvotat2++;
  337. }
  338. if (pais[j].vots[k][0] == 12) {
  339. mesdotze1++;
  340. } else if (pais[j].vots[k + 1][0] == 12) {
  341. mesdotze2++;
  342. }
  343. if (pais[j].vots[k][0] == 10) {
  344. mesdeu1++;
  345. } else if (pais[j].vots[k + 1][0] == 10) {
  346. mesdeu2++;
  347. }
  348. }
  349. }
  350. }
  351.  
  352. if (mesvotat2 > mesvotat1) {
  353. desempats(i,auxVots);
  354. } else if (mesvotat1 == mesvotat2) {
  355. if (mesdotze2 > mesdotze1) {
  356. desempats(i,auxVots);
  357. } else if (mesdotze1 == mesdotze2) {
  358. if (mesdeu2 > mesdeu1) {
  359. desempats(i,auxVots);
  360. } else if (mesdeu1 == mesdeu2) {
  361.  
  362. }
  363. }
  364. }
  365. }
  366. }
  367.  
  368. /**
  369. *Ordena segons les normes, els paissos empatats.
  370. *
  371. * @param i reb l'índex per canviar l'ordre dels empatats.
  372. * @param auxVots reb l'auxiliar per canviar l'ordre dels empatats.
  373. */
  374. public static void desempats(int i, int auxVots[][]){
  375. int auxIndice, auxPunts;
  376. auxIndice = auxVots[i][0];
  377. auxPunts = auxVots[i][1];
  378. auxVots[i][0] = auxVots[i + 1][0];
  379. auxVots[i][1] = auxVots[i + 1][1];
  380. auxVots[i + 1][0] = auxIndice;
  381. auxVots[i + 1][1] = auxPunts;
  382. }
  383.  
  384. /**
  385. * Mostra la classificació ordenada i sense empats.
  386. *
  387. * @param pais classe pais, amb el nom, la puntuació rebuda, vots donats i paisos a qui se'ls dona.
  388. */
  389. public static void mostrarClassificacio(Pais[] pais) {
  390. int auxVots[][] = ordenarClassificacio(pais);
  391. mirarEmpats(auxVots,pais);
  392. System.out.println("Puntuació:");
  393. for (int i = 0; i < 12; i++) {
  394.  
  395. System.out.printf("├────┬───────────────────────────────────┬────────┬────┬───────────────────────────────────┤\n"
  396. + "│ %3d│%-18S %3d │ │ %3d│%-18S %3d │ \n"
  397. + "├────┴───────────────────────────────────┴────────┴────┴───────────────────────────────────┤\n"
  398. + "\033[30m│ \033[41m \033[30m │\033[30m\n", i + 1, pais[auxVots[i][0]].nom, auxVots[i][1], i + 13, pais[auxVots[i + 13][0]].nom, auxVots[i + 13][1]);
  399. if (i + 14 == pais.length - 1) {
  400. System.out.printf("├────┬───────────────────────────────────┬────────┬────┬───────────────────────────────────┤\n"
  401. + "│ │ │ │ %3d│%-18S %3d │ \n"
  402. + "└────┴───────────────────────────────────┴────────┴────┴───────────────────────────────────┘\n", 25, pais[auxVots[i + 14][0]].nom, auxVots[i + 14][1]);
  403. }
  404. }
  405. }
  406.  
  407. /**
  408. * Llegeix per teclat un pais, per després buscarlo i mostrar a qui vota.
  409. *
  410. * @param pais classe pais, amb el nom, la puntuació rebuda, vots donats i paisos a qui se'ls dona.
  411. */
  412. public static void aQuiVota(Pais[] pais){
  413. Scanner entrada = new Scanner(System.in);
  414. String buscarpais;
  415. System.out.printf("\nIntrodueix el nom del pais que vols buscar: ");
  416. buscarpais=entrada.nextLine();
  417. for (int i = 0; i < pais.length; i++) {
  418. if(buscarpais.equalsIgnoreCase(pais[i].nom)){
  419. System.out.printf("\nEl pais %s amb %d punts ha votat a:",pais[i].nom,pais[i].puntuacio);
  420. for (int j = 0; j < pais[i].vots.length; j++) {
  421. System.out.printf("\n%d punts per a %s",pais[i].vots[j][1],pais[pais[i].vots[j][0]].nom);
  422.  
  423. }
  424. i=pais.length;
  425. }
  426. }
  427. }
  428.  
  429. /**
  430. * Llegeix per teclat un pais, per després burcarlo i mostrar qui l'ha votat.
  431. *
  432. * @param pais classe pais, amb el nom, la puntuació rebuda, vots donats i paisos a qui se'ls dona.
  433. */
  434. public static void quiLhaVotat(Pais[] pais){
  435. Scanner entrada = new Scanner(System.in);
  436. String buscarpais;
  437. System.out.printf("\nIntrodueix el nom del pais que vols buscar: ");
  438. buscarpais=entrada.nextLine();
  439. for (int i = 0; i < pais.length; i++) {
  440. for (int j = 0; j < pais[i].vots.length; j++) {
  441. if(buscarpais.equalsIgnoreCase(pais[pais[i].vots[j][0]].nom)){
  442. System.out.printf("\nEl pais %s l'ha votat amb %d punts",pais[i].nom,pais[i].vots[j][1]);
  443. }
  444. }
  445. }
  446. }
  447. }
Add Comment
Please, Sign In to add comment