Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So uhm, this program allows you to vote to 3 person and has 20 voters. However my teacher want's this reduced by using loops. So here's the program:

   import java.util.Scanner;
   public class Vote
   {
 String a, b, c, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20;
int va, vb, vc, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, r16, r17, r18, r19, r20, r21, r22, r23, r24, r25, r26, r27, r28, r29, r30, r31;
int r32, r33, r34, r35, r36, r37, r38, r39, r40, r41, r42, r43, r44, r45, r46, r47, r48, r49, r50, r51, r52, r53, r54, r55, r56, r57, r58, r59, r60; 

public void main_menu(){
    String n;
    Scanner menu = new Scanner(System.in);  
    System.out.println("1. Vote");
    System.out.println("2. Credits");
    System.out.println("3. Exit");
    n = menu.next();
    if (n.equals("1")){
        voting();
    }
    else if (n.equals("2")){
        System.out.print("Continue");
    }
    else if (n.equals("3")){
        exit();
    }
    else{
        System.out.print("Please input Numbers 1, 2, 3,");
    }
}

public void exit(){
    System.exit(0);
}

public void voting(){
    Scanner Nominees = new Scanner(System.in);
    System.out.print("Enter 1st Nominee: ");
    a = Nominees.nextLine();
    System.out.print("Enter 2nd Nominee: ");
    b = Nominees.nextLine();
    System.out.print("Enter 3rd Nominee: ");
    c = Nominees.nextLine();
    System.out.println("Nominees are: " + (a) +", " + (b)+ ", " + (c));
    confirm();
}

public void confirm(){
    String s;
    Scanner in = new Scanner(System.in);
    System.out.println("Confirm Nomimees ? A.Yes or B.No");
    s = in.next();
    if (s.equals("A")){
        y();
    }
    else if (s.equals("B")){
         voting();
    }
    else{
        System.out.println("Please Enter A or B ");
        confirm();
    }
    in.close();
}

public void y(){
    v1(); 
}

public void v1(){
    Scanner usrinp1 = new Scanner(System.in);
    System.out.print("Enter 1st Voters Name: ");
    v1 = usrinp1.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in1 = new Scanner(System.in);
    String c1;
    c1 = in1.next();
    if (c1.equals("1")){
        r1++;
        va++;
        v2();
    }
    else if (c1.equals("2")){
        r2++;
        vb++;
        v2();
    }
    else if (c1.equals("3")){
        r3++;
        vc++;
        v2();
    }
    else{            
        System.out.println("Please enter numbers 1, 2, & 3");
        v1();
    }
    in1.close();
   }

public void v2(){
    Scanner usrinp2 = new Scanner(System.in);
    System.out.print("Enter 2nd Voters Name: ");
    v2 = usrinp2.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in2 = new Scanner(System.in);
    String c2;
    c2 = in2.next();
    if (c2.equals("1")){
        r4++;
        va++;
        v3();
    }
    else if (c2.equals("2")){
        r5++;
        vb++;
        v3();
    }
    else if (c2.equals("3")){
        r6++;
        vc++;
        v3();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v2();
    }
    in2.close();    
}

public void v3(){
    Scanner usrinp3 = new Scanner(System.in);
    System.out.print("Enter 3rd Voters Name: ");
    v3 = usrinp3.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in3 = new Scanner(System.in);
    String c3;
    c3 = in3.next();
    if (c3.equals("1")){
        r7++;
        va++;
        v4();
    }
    else if (c3.equals("2")){
        r8++;
        vb++;
        v4();
    }
    else if (c3.equals("3")){
        r9++;
        vc++;
        v4();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v3();
    }
    in3.close();    
}

public void v4(){
    Scanner usrinp4 = new Scanner(System.in);
    System.out.print("Enter 4th Voters Name: ");
    v4 = usrinp4.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in4 = new Scanner(System.in);
    String c4;
    c4 = in4.next();
    if (c4.equals("1")){
        r10++;
        va++;
        v5();
    }
    else if (c4.equals("2")){
        r11++;
        vb++;
        v5();
    }
    else if (c4.equals("3")){
        r12++;
        vc++;
        v5();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v4();
    }
    in4.close();    
}

public void v5(){
    Scanner usrinp5 = new Scanner(System.in);
    System.out.print("Enter 5th Voters Name: ");
    v5 = usrinp5.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in5 = new Scanner(System.in);
    String c5;
    c5 = in5.next();
    if (c5.equals("1")){
        r13++;
        va++;
        v6();
    }
    else if (c5.equals("2")){
        r14++;
        vb++;
        v6();
    }
    else if (c5.equals("3")){
        r15++;
        vc++;
        v6();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v5();
    }
    in5.close();    
}

public void v6(){
    Scanner usrinp6 = new Scanner(System.in);
    System.out.print("Enter 6th Voters Name: ");
    v6 = usrinp6.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in6 = new Scanner(System.in);
    String c6;
    c6 = in6.next();
    if (c6.equals("1")){
        r16++;
        va++;
        v7();
    }
    else if (c6.equals("2")){
        r17++;
        vb++;
        v7();
    }
    else if (c6.equals("3")){
        r18++;
        vc++;
        v7();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v6();
    }
    in6.close();    
}

public void v7(){
    Scanner usrinp7 = new Scanner(System.in);
    System.out.print("Enter 7th Voters Name: ");
    v7 = usrinp7.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in7 = new Scanner(System.in);
    String c7;
    c7 = in7.next();
    if (c7.equals("1")){
        r19++;
        va++;
        v8();
    }
    else if (c7.equals("2")){
        r20++;
        vb++;
        v8();
    }
    else if (c7.equals("3")){
        r21++;
        vc++;
        v8();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v7();
    }
    in7.close();    
}

public void v8(){
    Scanner usrinp8 = new Scanner(System.in);
    System.out.print("Enter 8th Voters Name: ");
    v8 = usrinp8.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in8 = new Scanner(System.in);
    String c8;
    c8 = in8.next();
    if (c8.equals("1")){
        r22++;
        va++;
        v9();
    }
    else if (c8.equals("2")){
        r23++;
        vb++;
        v9();
    }
    else if (c8.equals("3")){
        r24++;
        vc++;
        v9();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v8();
    }
    in8.close();    
}

public void v9(){
    Scanner usrinp9 = new Scanner(System.in);
    System.out.print("Enter 9th Voters Name: ");
    v9 = usrinp9.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in9 = new Scanner(System.in);
    String c9;
    c9 = in9.next();
    if (c9.equals("1")){
        r25++;
        va++;
        v10();
    }
    else if (c9.equals("2")){
        r26++;
        vb++;
        v10();
    }
    else if (c9.equals("3")){
        r27++;
        vc++;
        v10();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v9();
    }
    in9.close();    
}

public void v10(){
    Scanner usrinp10 = new Scanner(System.in);
    System.out.print("Enter 10th Voters Name: ");
    v10 = usrinp10.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in10 = new Scanner(System.in);
    String c10;
    c10 = in10.next();
    if (c10.equals("1")){
        r28++;
        va++;
        v11();
    }
    else if (c10.equals("2")){
        r29++;
        vb++;
        v11();
    }
    else if (c10.equals("3")){
        r30++;
        vc++;
        v11();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v10();
    }
    in10.close();    
}

public void v11(){
    Scanner usrinp11 = new Scanner(System.in);
    System.out.print("Enter 11th Voters Name: ");
    v11 = usrinp11.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in11 = new Scanner(System.in);
    String c11;
    c11 = in11.next();
    if (c11.equals("1")){
        r31++;
        va++;
        v12();
    }
    else if (c11.equals("2")){
        r32++;
        vb++;
        v12();
    }
    else if (c11.equals("3")){
        r33++;
        vc++;
        v12();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v11();
    }
    in11.close();    
}

public void v12(){
    Scanner usrinp12 = new Scanner(System.in);
    System.out.print("Enter 12th Voters Name: ");
    v12 = usrinp12.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in12 = new Scanner(System.in);
    String c12;
    c12 = in12.next();
    if (c12.equals("1")){
        r34++;
        va++;
        v13();
    }
    else if (c12.equals("2")){
        r35++;
        vb++;
        v13();
    }
    else if (c12.equals("3")){
        r36++;
        vc++;
        v13();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v12();
    }
    in12.close();    
}

public void v13(){
    Scanner usrinp13 = new Scanner(System.in);
    System.out.print("Enter 13th Voters Name: ");
    v13 = usrinp13.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in13 = new Scanner(System.in);
    String c13;
    c13 = in13.next();
    if (c13.equals("1")){
        r37++;
        va++;
        v14();
    }
    else if (c13.equals("2")){
        r38++;
        vb++;
        v14();
    }
    else if (c13.equals("3")){
        r39++;
        vc++;
        v14();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v13();
    }
    in13.close();    
}

public void v14(){
    Scanner usrinp14 = new Scanner(System.in);
    System.out.print("Enter 14th Voters Name: ");
    v14 = usrinp14.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in14 = new Scanner(System.in);
    String c14;
    c14 = in14.next();
    if (c14.equals("1")){
        r40++;
        va++;
        v15();
    }
    else if (c14.equals("2")){
        r41++;
        vb++;
        v15();
    }
    else if (c14.equals("3")){
        r42++;
        vc++;
        v15();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v14();
    }
    in14.close();    
}

public void v15(){
    Scanner usrinp15 = new Scanner(System.in);
    System.out.print("Enter 15th Voters Name: ");
    v15 = usrinp15.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in15 = new Scanner(System.in);
    String c15;
    c15 = in15.next();
    if (c15.equals("1")){
        r43++;
        va++;
        v16();
    }
    else if (c15.equals("2")){
        r44++;
        vb++;
        v16();
    }
    else if (c15.equals("3")){
        r45++;
        vc++;
        v16();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v15();
    }
    in15.close();    
}

public void v16(){
    Scanner usrinp16 = new Scanner(System.in);
    System.out.print("Enter 16th Voters Name: ");
    v16 = usrinp16.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in16 = new Scanner(System.in);
    String c16;
    c16 = in16.next();
    if (c16.equals("1")){
        r46++;
        va++;
        v17();
    }
    else if (c16.equals("2")){
        r47++;
        vb++;
        v17();
    }
    else if (c16.equals("3")){
        r48++;
        vc++;
        v17();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v16();
    }
    in16.close();    
}

public void v17(){
    Scanner usrinp17 = new Scanner(System.in);
    System.out.print("Enter 17th Voters Name: ");
    v17 = usrinp17.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in17 = new Scanner(System.in);
    String c17;
    c17 = in17.next();
    if (c17.equals("1")){
        r49++;
        va++;
        v18();
    }
    else if (c17.equals("2")){
        r50++;
        vb++;
        v18();
    }
    else if (c17.equals("3")){
        r51++;
        vc++;
        v18();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v17();
    }
    in17.close();    
}

public void v18(){
    Scanner usrinp18 = new Scanner(System.in);
    System.out.print("Enter 18th Voters Name: ");
    v18 = usrinp18.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in18 = new Scanner(System.in);
    String c18;
    c18 = in18.next();
    if (c18.equals("1")){
        r52++;
        va++;
        v19();
    }
    else if (c18.equals("2")){
        r53++;
        vb++;
        v19();
    }
    else if (c18.equals("3")){
        r54++;
        vc++;
        v19();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v18();
    }
    in18.close();    
}

public void v19(){
    Scanner usrinp19 = new Scanner(System.in);
    System.out.print("Enter 19th Voters Name: ");
    v19 = usrinp19.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in19 = new Scanner(System.in);
    String c19;
    c19 = in19.next();
    if (c19.equals("1")){
        r55++;
        va++;
        v20();
    }
    else if (c19.equals("2")){
        r56++;
        vb++;
        v20();
    }
    else if (c19.equals("3")){
        r57++;
        vc++;
        v20();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v19();
    }
    in19
    .close();    
}

public void v20(){
    Scanner usrinp20 = new Scanner(System.in);
    System.out.print("Enter 20th Voters Name: ");
    v20 = usrinp20.nextLine();
    System.out.println("Choose your Vote: " + "1." + (a) + " 2." + (b) + " 3." + (c));
    Scanner in20 = new Scanner(System.in);
    String c20;
    c20 = in20.next();
    if (c20.equals("1")){
        r58++;
        va++;
        results();
    }
    else if (c20.equals("2")){
        r59++;
        vb++;
        results();
    }
    else if (c20.equals("3")){
        r60++;
        vc++;
        results();
    }
    else{
        System.out.println("Please enter numbers 1, 2, & 3");
        v20();
    }
    in20.close();    
}


public void results(){

    System.out.println((a) + "=" + (va));

    if (r1==1){
        System.out.println(v1);
    }

    if (r4 == 1){
        System.out.println(v2);
    }

    if (r7 == 1){
        System.out.println(v3);
    }

    if (r10 == 1){
        System.out.println(v4);
    }

    if (r13 == 1){
        System.out.println(v5);
    }

    if (r16 == 1){
        System.out.println(v6);
    }

    if (r19 == 1){
        System.out.println(v7);
    }

    if (r22 == 1){
        System.out.println(v8);
    }

    if (r25 == 1){
        System.out.println(v9);
    }

    if (r28 == 1){
        System.out.println(v10);
    }

    if (r31 == 1){
        System.out.println(v11);
    }

    if (r34 == 1){
        System.out.println(v12);
    }

    if (r37 == 1){
        System.out.println(v13);
    }

    if (r40 == 1){
        System.out.println(v14);
    }

    if (r43 == 1){
        System.out.println(v15);
    }

    if (r46 == 1){
        System.out.println(v16);
    }

    if (r49 == 1){
        System.out.println(v17);
    }

    if (r52 == 1){
        System.out.println(v18);
    }

    if (r55 == 1){
        System.out.println(v19);
    }

    if (r58 == 1){
        System.out.println(v20);
    }

    System.out.println("\n"+(b) + "=" + (vb));

    if (r2 == 1){
        System.out.println(v1);
    }

    if (r5 == 1){
        System.out.println(v2);
    }       

    if (r8 == 1){
        System.out.println(v3);
    }

    if (r11 == 1){
        System.out.println(v4);
    }

    if (r14 == 1){
        System.out.println(v5);
    }

    if (r17 == 1){
        System.out.println(v6);
    }

    if (r20 == 1){
        System.out.println(v7);
    }

    if (r23 == 1){
        System.out.println(v8);
    }

    if (r26 == 1){
        System.out.println(v9);
    }

    if (r29 == 1){
        System.out.println(v10);
    }

    if (r32 == 1){
        System.out.println(v11);
    } 

    if (r35 == 1){
        System.out.println(v12);
    }

    if (r38 == 1){
        System.out.println(v13);
    }

    if (r41 == 1){
        System.out.println(v14);
    }

    if (r44 == 1){
        System.out.println(v15);
    }

    if (r47 == 1){
        System.out.println(v16);
    }

    if (r50 == 1){
        System.out.println(v17);
    }

    if (r53 == 1){
        System.out.println(v18);
    }

    if (r56 == 1){
        System.out.println(v19);
    }

    if (r59 == 1){
        System.out.println(v20);
    }

    System.out.println("\n"+(c) + "=" + (vc));
    if (r3 == 1){
        System.out.println(v1);
    }

    if (r6 == 1){
        System.out.println(v2);
    }

    if (r9 == 1){
        System.out.println(v3);
    }

    if (r12 == 1){
        System.out.println(v4);
    }

    if (r15 == 1){
        System.out.println(v5);
    }

    if (r18 == 1){
        System.out.println(v6);
    }

    if (r21 == 1){
        System.out.println(v7);
    }

    if (r24 == 1){
        System.out.println(v8);
    }

    if (r27 == 1){
        System.out.println(v9);
    }

    if (r30 == 1){
        System.out.println(v10);
    }

    if (r33 == 1){
        System.out.println(v11);
    }

    if (r36 == 1){
        System.out.println(v12);
    }

    if (r39 == 1){
        System.out.println(v13);
    }

    if (r42 == 1){
        System.out.println(v14);
    }

    if (r45 == 1){
        System.out.println(v15);
    }

    if (r48 == 1){
        System.out.println(v16);
    }

    if (r51 == 1){
        System.out.println(v17);
    }

    if (r54 == 1){
        System.out.println(v18);
    }

    if (r57 == 1){
        System.out.println(v19);
    }

    if (r60 == 1){
        System.out.println(v20);
    }

}
}

I know it's a poorly written code, I get that a lot. A guy gave me guidelines but I don't seem to get it but here it is

    public class Voting {
final Map<String, List<String>> votingMap = new HashMap<>();

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    Set<String> names = new LinkedHashSet<>(Arrays.asList("1", "2", "3"));
    Voting voting = new Voting(names);
    int counter = 1;

    while (true) {
        System.out.println("1. Vote\n2. Credit\n3. Exit");
        switch (in.nextLine()) {
            case "1":
                voting.vote(in, counter++);
                break;
            case "2":
                voting.dumpResults(names);
                break;
            case "3":
                return;
            default:
                System.out.print("Please input Numbers 1, 2, 3,");
        }
    }
}

public Voting(Set<String> names) {
    for (String name : names) 
        votingMap.put(name, new ArrayList<>());
}

private void vote(Scanner in, int counter) {
    System.out.print("Enter " + counter + nth(counter) + " Voters Name: ");
    String name = in.nextLine();
    while (true) {
        System.out.println("Choose your Vote: " + votingMap.keySet().stream().collect(Collectors.joining(",")));
        String voteFor = in.nextLine();
        List<String> voters = votingMap.get(voteFor);
        if (voters != null) {
            voters.add(name);
            return;
        }
    }
}

private void dumpResults() {
    votingMap.forEach((k, v) -> {
        System.out.println(k + ": "+ v);
    });
}
}

I'm sorry but I need this to graduate middle school :l

share|improve this question
    
Does it work as expected? And are you expecting this to be a comparative-review between the suggestion and your approach? – h.j.k. 14 hours ago
1  
@h.j.k. Yea it works as expected, but my teacher want's it reduced because it's too long for her and yes I wanted to know what's the best way to reduce this – K. Cinches 14 hours ago
    
uhm, am I off topic, I was only sent here from stack overflow. – K. Cinches 13 hours ago
    
What do you want to achieve from your comparative-review? People are free to just say that the second alternative is better due to lower code repetition. My point is that you might or might not learn something from this. – Bruno Costa 13 hours ago
    
@BrunoCosta, so I wasn't offtopic. what I should read to fix this, I'm fine with it. as long as it can be reduced. Don't mind the difficulty thou, I'll try to learn it. It can help my future codes too. – K. Cinches 13 hours ago
up vote 5 down vote accepted

Obligatory style-points

The code in the question does not seem to be properly formatted, but I don't know how this looks in your IDE / text editor, so for now I will ignore it.

Separation of concerns

You have a class called Voting, which you instantiate in the public static void main(String[] args) method. You could separate these, which makes it more readable as well:

public class Main {

    public static void main(String[] args) {
        String[] names = {"1", "2", "3"}; // Not really names, are they?
    }
}

class Voting {

    private final Map<String, List<String>> votingMap;

    public Voting(final String[] names) {
        votingMap = new HashMap<>();
        for (String name : names) {
            votingMap.put(name, new ArrayList<>());
        }
    }
}

Now you can add the voting-specific methods to this class as well:

class Voting {
    // The votingMap and constructor here (see above)

    public boolean vote(final String voterName, final String voteFor) {
        List<String> voters = votingMap.get(voteFor);
        if (voters == null) {
            return false;
        }
        voters.add(voterName);
        return true;
    }

    public void printVotingResults() {
        for (Map.Entry<String, List<String>> candidate : votingMap.entrySet()) {
            System.out.println(candidate.getKey() + " : " + candidate.getValue().toString());
        }
    }

    public String getCandidatesAsString() {
        StringBuilder candidates = new StringBuilder();
        for (String name : votingMap.keySet()) {
            candidates.append(name + ", ");
        }
        return candidates.toString();
    }
}

The endresult is something like this:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        String[] names = { "1", "2", "3" }; // Not really names, are they?
        Voting voting = new Voting(names);

        int voterCount = 1;

        while (true) {
            System.out.println("1. Vote\n2. Credit\n3. Exit");
            switch (input.nextLine()) {
                case "1":
                    performVote(input, voterCount++, voting);
                    break;
                case "2":
                    voting.printVotingResults();
                    break;
                case "3":
                    return;
            }
        }
    }

    private static void performVote(final Scanner input, final int voterCount, final Voting voting) {
        System.out.println("Enter the " + voterCount + "th voter's name: ");
        String name = input.nextLine(); // Not checked if a name is actually entered
        while(true) {
            System.out.println("Choose your vote: " + voting.getCandidatesAsString());
            String voteFor = input.nextLine();
            if (voting.vote(name, voteFor)) {
                return;
            }
        }
    }
}

class Voting {

    private final Map<String, List<String>> votingMap;

    public Voting(final String[] names) {
        votingMap = new HashMap<>();
        for (String name : names) {
            votingMap.put(name, new ArrayList<String>());
        }
    }

    public boolean vote(final String voterName, final String voteFor) {
        List<String> voters = votingMap.get(voteFor);
        if (voters == null) {
            return false;
        }
        voters.add(voterName);
        return true;
    }

    public void printVotingResults() {
        for (Map.Entry<String, List<String>> candidate : votingMap.entrySet()) {
            System.out.println(candidate.getKey() + " : " + candidate.getValue().toString());
        }
    }

    public String getCandidatesAsString() {
        StringBuilder candidates = new StringBuilder();
        for (String name : votingMap.keySet()) {
            candidates.append(name + ", ");
        }
        return candidates.toString();
    }
}

This makes it in my opinion a lot more readable. It's also not limited to java 8 with lambdas. The voting system is now also separated from the input, which means you could reuse the Voting class with a different form of input.

Unlike your first code-snippet, this is also not limited to 20 voters, as arrays are used.

Another option would be for the voting class itself to extend HashMap, which would allow you to reduce the code even more, however, as arrays aren't even explained by your teacher yet, I won't go into subclassing / polymorphism etc.


Edit:

  • votingCount wasn't being incremented - fixed.
  • Detail: The voter's name is not being checked for empty name.
share|improve this answer
    
Thank you so much mate, you explained it very well. This is really helpful. This might change the way I write my codes. – K. Cinches 12 hours ago
    
@K.Cinches edited quickly for some minor changes (see bottom) – Randyr 12 hours ago
    
oh, ok. thanks again – K. Cinches 12 hours ago
    
@K.Cinches Map, ArrayList, HashMap, List, and Scanner should all be imported (just like you already did with Scanner in the first sample. They are all part of the java.util package. I'll update the answer. – Randyr 12 hours ago
    
oh, thanks forgive me for my ignorance – K. Cinches 12 hours ago

I canceled answering this as I saw I was way too late but I would love to add a little:

It took me a while to understand what all those r1 to r60 are. No need to tell you again about arrays or stuff, but especially in Java I feel like this is time for a Voter and a Nominee class.

public class Voter {

    private final String name;
    private boolean[] choice = {false, false, false};

    public Voter(String name){
        this.name = name;
    }

    public String getName(){
        return this.name;
    }

    public boolean voteFor(int i){

        this.choice[0] = false;
        this.choice[1] = false;
        this.choice[2] = false;

        switch(i){
            case 1:
                choice[0] = true;
                return true;
            case 2:
                choice[1] = true;
                return true;
            case 3:
                choice[2] = true;
                return true;
            default:
                return false;
        }
    }

    public int getChoice(){

        //r is returned, is 0 returned, no vote was made
        int r = 0;
        int i = 0;

        for(boolean b: this.choice){

            i += 1;
            if(b) {
                r = i;
            }
        }

        return r;
    }

}

This Class is for representing Voters. It keeps their names and let you vote with voteFor(int), if the vote is invalid no nominee is selected and false is returned, else the vote is locked (Old Vote gets resettet when calling voteFor(int)!). Also this Class expects to have 3 Nominees, needs adjustment if this should be flexible!

public class Nominee {

    final String name;
    private int votes;

    public Nominee(String name){
        this.name = name;
    }

    public String getName(){
        return  this.name;
    }

    public void addVote(){
        this.votes += 1;
    }

    public int getVotes(){
        return this.votes;
    }

}

This Class is for the Nominees which holds their names and how many votes they have.

This feels more consistent to me, in concern of the context.

But it would require a bit more modification of the other Code and since there is already an accepted answer for the biggest issue of the question I'll stop here.

share|improve this answer
    
I agree that both the candidate and voter should have their own model class. This will make it more extendable in the future as well. – Randyr 11 hours ago
    
can you continue mate, I would like to see how this code works too, I might learn something from this in writing my future codes. – K. Cinches 11 hours ago
    
Well then i have to ask a question: how important is it to save the voters choice? Since votes are Counted on the Nominees id say its fine, but, maybe i'm just to real world since its only an exaple, but this would be a gamechanger in case of dynamic numbers of nominees – Lex 11 hours ago
    
I only add it so the voters can confirm if they really vote that person. nothing special – K. Cinches 11 hours ago
    
Okay, well then i think we can move this into our voting method, next thing to think about is what is the point in keeping the Voters, since saving them might get Obsolet. But i will save them and add them so we know who allready voted, that will give them a reason to stay and i don't change semantics of your code but the code itself. Will continue working this out tomorrow – Lex 10 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.