Java - necesito reducir codigo. ayuda porfavor.

 
Vista:

necesito reducir codigo. ayuda porfavor.

Publicado por claudio arrigorriaga (2 intervenciones) el 09/07/2012 20:18:12
este programa simula una eleccion municipal y trabaja con colas. tengo que reducir codigo trabajando con vectores pero me a costado demaciado hacerlo. porfavor ayudenme.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class candidato{
	String nombre;
	int votos,hombres,mujeres;
	float pvotos,phombres,pmujeres;
	candidato(String n, int v, int h, int m, float p, float ph, float pm){
	    nombre=n;
		votos=v;
		hombres=h;
		mujeres=m;
		pvotos=p;
		phombres=ph;
		pmujeres=pm;
	}
	public void imprimir(){
		System.out.println(nombre+" Votos: "+votos+" = "+pvotos+"% del total de los votos\n Votos de Hombres: "+hombres+"="+phombres+"% de los votos de hombres \n Votos de Mujeres: "+mujeres+"="+pmujeres+"% de los votos de mujeres \n\n\n");
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Leer
{
  public static String dato()
  {
    String dato = "";
    try
    {
      // Definir un flujo de caracteres de entrada: flujoE
      InputStreamReader Ir = new InputStreamReader(System.in);
      BufferedReader BR = new BufferedReader(Ir);
      // Leer. La entrada finaliza al pulsar la tecla Entrar
      dato = BR.readLine();
    }
    catch(IOException e)
    {
      System.err.println("Error: " + e.getMessage());
    }
    return dato; // devolver el dato tecleado
  }
 
  public static int datoInt()
  {
    try
    {
      return Integer.parseInt(dato());
    }
    catch(NumberFormatException e)
    {
      return 0;
    }
  }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.io.*;
import java.text.*;
public class Menu{
 
 
    public static int Menu() {
        int op=0;
        System.out.print(" Menu\n");
 
        System.out.print(" 1. Ingresar votos\n");
        System.out.print(" 2. Mostrar\n");
        System.out.print(" 3.Salir\n\n");
        System.out.print("Ingrese una opcion : \n");
        op=Leer.datoInt();
        return op;
    }
 
    public static void main(String[] args) {
        Metodos cola=new Metodos();
        int opc = 0,total = 0, voto = 0, votos=0,suma=0,i,thombres=0,tmujeres=0;
        int zn1=0, hzn1=0,mzn1=0;
        int zz2=0,hzz2=0,mzz2=0;
        int aa3=0,haa3=0,maa3=0;
        int ss4=0,hss4=0,mss4=0;
        int dd5=0 ,hdd5=0,mdd5=0;
        int nulos=0,hnulos=0,mnulos=0;
        float pzn1,pzz2,paa3,pss4,pdd5,pnulos;
        float phzn1,phzz2,phaa3,phss4,phdd5,phnulos;
        float pmzn1,pmzz2,pmaa3,pmss4,pmdd5,pmnulos;
       String aa="candidato 1", bb="Candidato 2",cc="Candidato 3",dd="Candidato 4",ee="Candidato 5",nu="Nulos";
 
        while(opc<1 || opc>3) {
            opc = Menu();
            switch(opc) {
 
                case 1: System.out.print("Numero de votos a generar : \n");
                        total=Leer.datoInt();
 
 
                            for(i=0;i<total;i++){
                            voto=(int)(Math.random()*6);
 
 
                           if(voto==1){
                           	zn1++;
                           	votos=(int)(Math.random()*2);
                            if(votos==0)
                           	 hzn1++;
                           	if(votos==1)
                           	 mzn1++;
 
                           }
 
 
                           	if(voto==2){
                           		zz2++;
                           		votos=(int)(Math.random()*2);
                                if(votos==0)
                             	 hzz2++;
                             	if(votos==1)
                           	     mzz2++;
 
                              }
 
 
                           		if(voto==3){
                           			aa3++;
                           			votos=(int)(Math.random()*2);
                                    if(votos==0)
                           	          haa3++;
                           	        if(votos==1)
                           	          maa3++;
                                 }
 
 
                           			if(voto==4){
                           				ss4++;
                           				votos=(int)(Math.random()*2);
                                        if(votos==0)
                                    	 hss4++;
                                    	if(votos==1)
                                    	 mss4++;
                                    }
 
 
                           				if(voto==5){
                           					dd5++;
                           					votos=(int)(Math.random()*2);
                                            if(votos==0)
                           	                 hdd5++;
                                        	if(votos==1)
                           	                 mdd5++;
                                            }
 
 
                           					if(voto==0){
                           						nulos++;
                           						votos=(int)(Math.random()*2);
                                                if(votos==0)
                           	                    hnulos++;
                           	                    if(votos==1)
                           	                    mnulos++;
                                             }
                       }
                       thombres=hzn1+hzz2+haa3+hss4+hdd5+hnulos;
                       tmujeres=mzn1+mzz2+maa3+mss4+mdd5+mnulos;
                       pzn1=zn1*100/total; pzz2=zz2*100/total; paa3=aa3*100/total;
                       pss4=ss4*100/total; pdd5=dd5*100/total; pnulos=nulos*100/total;
                       phzn1=hzn1*100/thombres; phzz2=hzz2*100/thombres; paa3=haa3*100/thombres;
                       phss4=hss4*100/thombres; phdd5=hdd5*100/thombres; phnulos=hnulos*100/thombres;
                       pmzn1=mzn1*100/tmujeres; pmzz2=mzz2*100/tmujeres; pmaa3=maa3*100/tmujeres;
                       pmss4=mss4*100/tmujeres; pmdd5=mdd5*100/tmujeres; pmnulos=mnulos*100/tmujeres;
 
 
                        candidato a=new candidato(aa,zn1,hzn1,mzn1,pzn1,phzn1,pmzn1); cola.Insertar(a);
                        candidato t=new candidato(aa,zn1,hzn1,mzn1,pzn1,phzn1,pmzn1); cola.Insertar(t);
                        candidato e=new candidato(bb,zz2,hzz2,mzz2,pzz2,phzz2,pmzz2); cola.Insertar(e);
                        candidato m=new candidato(cc,aa3,haa3,maa3,paa3,paa3,pmaa3); cola.Insertar(m);
                        candidato p=new candidato(dd,ss4,hss4,mss4,pss4,phss4,pmss4); cola.Insertar(p);
                        candidato o=new candidato(ee,dd5,hdd5,mdd5,pdd5,phdd5,pmdd5); cola.Insertar(o);
                        candidato r=new candidato(nu,nulos,hnulos,mnulos,pnulos,phnulos,pmnulos); cola.Insertar(r);
                        opc = 0;
                        break;
                case 2: cola.Mostrar();
                        opc = 0;
                        break;
                case 3: break;
            }
        }
    }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class Metodos {
    public Nodo L;
    public Nodo L1;
 
    public boolean Vacia() {
        if( L == null ){ return true; }
        else { return false; }
    }
 
    public void Insertar(candidato x){
        Nodo p=new Nodo(x);
 
        if(this.Vacia()) {
            L = p;
        }else {
            p.siguiente=L;
            L=p;
        }
    }
 
    public void Mostrar(){
        if(this.Vacia()) {
            System.out.println("\n\nLista es vacia\n\n");
        } else {
            L1 = L;
            int i=0;
            System.out.println("Contenido de la Cola");
            while(L1.siguiente != null ){
            	L1.imprimir_nodo();
                L1 = L1.siguiente;
                i++;
            }
 
        }
    }
 
 
    public void Eliminar() {
        if(this.Vacia()) {
            System.out.println("\n\nLista es vacia\n\n");
        } else {
            System.out.print("\n\nSe ha Eliminado valor : ");
            System.out.print(L.info+"\n\n");
            L=L.siguiente;
        }
    }
 
    public void Vaciar(){
        System.out.print("Lista esta siendo vaciada\n");
        L=null;
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder