3
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Does it work as expected? And are you expecting this to be a comparative-review between the suggestion and your approach? \$\endgroup\$ – h.j.k. Jan 3 '17 at 12:17
  • 1
    \$\begingroup\$ @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 \$\endgroup\$ – K. Cinches Jan 3 '17 at 12:19
  • \$\begingroup\$ uhm, am I off topic, I was only sent here from stack overflow. \$\endgroup\$ – K. Cinches Jan 3 '17 at 13:16
  • \$\begingroup\$ 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. \$\endgroup\$ – Bruno Costa Jan 3 '17 at 13:16
  • \$\begingroup\$ @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. \$\endgroup\$ – K. Cinches Jan 3 '17 at 13:20
6
\$\begingroup\$

Editted one last time: I did not run the Code when i finished my post since i was in a hurry. I added the main()-Method for running it as stand-alone, just remove it if not needed. I made the mistake closing System in so there could not be any new line read. I fixed this and will paste the code as a whole here, also wanted to do this earlyer.

import java.util.Scanner;
public class Vote
{

    public static void main(String[] args){
        new Vote().main_menu();
    }

    private Nominee a, b, c;
    private Voter[] voters = new Voter[20];

    public void main_menu(){

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


    private void setNominees(){

        boolean redoo = true;
        Scanner in = new Scanner(System.in);

        do{
            System.out.print("Enter 1st Nominee: ");
            a = new Nominee(in.nextLine());
            System.out.print("Enter 2nd Nominee: ");
            b = new Nominee(in.nextLine());
            System.out.print("Enter 3rd Nominee: ");
            c = new Nominee(in.nextLine());
            System.out.printf("Nominees are: %s, %s, %s\n", a.getName(), b.getName(), c.getName());


            /*
            confirm() comes here
            its looped until a break is called with A or B
            when A the outer loop is stoped aswell and we continue
            when B the inner loop breaks but the outer do-loop is repeated
            */
            do{
                System.out.println("Confirm Nominees ? A.Yes or B.No");
                String s = in.nextLine();

                if (s.equals("A")){
                    redoo = false;
                    break;
                }
                else if (s.equals("B")){
                    break;
                }
                else{
                    System.out.println("Please Enter A or B ");
                }
            }
            while(true);
        }
        while(redoo);
        //in.close();

    }

    private void voting(){

        /*
        the for-loop iterates as often as your voters-array has fields.
        In case you remove the voters array cause you decide to not NSA into everyones Votes,
        set numbervoters to the number, how often the loop should iterate.
         */
        int numbervoters = voters.length;
        for(int i = 0; i < numbervoters; i++ ){
            Scanner in1 = new Scanner(System.in);
            System.out.printf("Enter #%s Voters Name: \n", i+1);
            String name = in1.nextLine();
            //you might ask now if the Name is correct and build a loop over this.
            //when user misspelled his name you repeat the loop until he entered it correct, just an option
            voters[i] = new Voter(name);

            do {
                System.out.printf("Choose your Vote: 1. %s 2. %s 3. %s\n", a.getName(), b.getName(), c.getName());
                Scanner in2 = new Scanner(System.in);
                String choice = in2.next();

                if (choice.equals("1")) {
                    System.out.printf("Sure to vote for %s? Y/y=yes", a.getName());
                    Scanner in3 = new Scanner(System.in);
                    String sure = in3.next();
                    if (sure.equals("Y") || sure.equals("y")) {
                        a.addVote();
                        break;
                    }
                } else if (choice.equals("2")) {
                    System.out.printf("Sure to vote for %s? Y/y=yes", b.getName());
                    Scanner in3 = new Scanner(System.in);
                    String sure = in3.next();
                    if (sure.equals("Y") || sure.equals("y")) {
                        b.addVote();
                        break;
                    }
                } else if (choice.equals("3")) {
                    System.out.printf("Sure to vote for %s? Y/y=yes", c.getName());
                    Scanner in3 = new Scanner(System.in);
                    String sure = in3.next();
                    if (sure.equals("Y") || sure.equals("y")) {
                        c.addVote();
                        break;
                    }
                } else {
                    System.out.print("You have to chose one of the 3 Nominees! No chance to not Vote!");
                }
            }
            while(true);
            voters[i].voted();
        }

        System.out.printf("Results are: \n%d votes for %s\n%d votes for %s\n%d votes for %s\n",
                a.countVotes(), a.getName(),
                b.countVotes(), b.getName(),
                c.countVotes(), c.getName());
    }

}

Below first Edit and original Post.

Okay, here are is my "new Version" with the seperated Classes (sorry for Tophandour who allready did alot of editing my bad language in first answer, thx!). I will keep the old answer on the bottom of this one.

I will try to stick to your very first postet code, so we see what is changed and why - and not just rewriting code for a Voting-System.

First of all I will add the classes Voter and Nominee. We do this, so data we save is held in the right context and we shift responsibility for those data into the objects holding them. This way we can reduce the lines:

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; 

as communicated in comments we will not save the Voters choice since its not necessary (allready changing the semantic ...), but this would allredy reduce these lines to:

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

Even if we reduced the saved data just but deleting it, I will create a "Voter" class for saving the Voters Name and to see if he allready made a choice, but since we are a good democracy we don't save his vote connected with his name. We will add the Voters into an arraylist so we store all voters who made theyr votes. This is the Voters Class:

public class Voter {

   private final String name;
   private boolean didvote;

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

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

   public void voted(){
       this.didvote = true;
   }

   public boolean allreadyVoted(){
       return this.didvote;
   }

}

Now you may add an ID or something to your voters, that unequly identifies them. This way you can check if a person did allready vote by searching them in the List/Array. You might even have a List of all legal Voters, let them insert theyr ID and then show theyr names and see if they allready did vote. Just some idea if you go to extend this.

Next we have our Nominees. This Class holds theyr name and the numbers of votes allready collected.

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 countVotes(){
        return this.votes;
    }

}

This is how the new first lines look like:

import java.util.Scanner;
public class Vote
{
    Nominee a, b, c;
    Voter[] voters = new Voter[20];

I will stick to your 3 Nominees and 20 Voters here. In case you did not talked about arrays you might rethink keeping Voters at all. But to stick as close as possible to your Code we will keep them in.

Method main_menu looks ok, you might reduce liney by one by declaring String n when u add value to it. Also exit-Method can be deleeted and System.exit(0) can be called right in place, since exit-Method does deliver anything else, so we have:

public void main_menu(){

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

Next onto method voting. I will change it's name, so we get what is does faster, to: setNominees(). Also i will add the functionality of confirm into this Method, since confirm is written to be only used by method voting(or now setNominees). I will also change the way you print, did this your way for a veryyy long time myslef since i found printf(). Just have a look and you might get how it works. While working on this i saw why you had confirm in an own method, you called the Methods Recursivley. You can do this, but i will change this to looping so we can have everything nice and sweet in one method, this is also changed for calling setNominees called by option b.

private void setNominees(){

    boolean redoo = true;
    Scanner in = new Scanner(System.in);

    do{
        System.out.print("Enter 1st Nominee: ");
        a = new Nominee(in.nextLine());
        System.out.print("Enter 2nd Nominee: ");
        b = new Nominee(in.nextLine());
        System.out.print("Enter 3rd Nominee: ");
        c = new Nominee(in.nextLine());
        System.out.printf("Nominees are: %s, %s, %s\n", a.getName(), b.getName(), c.getName());


        /*
        confirm() comes here
        its looped until a break is called with A or B
        when A the outer loop is stoped aswell and we continue
        when B the inner loop breaks but the outer do-loop is repeated
        */
        do{
            System.out.println("Confirm Nominees ? A.Yes or B.No");
            String s = in.next();

            if (s.equals("A")){
                redoo = false;
                break;
            }
            else if (s.equals("B")){
                break;
            }
            else{
                System.out.println("Please Enter A or B ");
            }
        }
        while(true);
    }
    while(redoo);
    in.close();

    return;
}

Also we now need to change our main_menu()-method a little: we have to change from the function call voting() to setNominee(). I will only add setNominee() as call before voting() and keep it there, so we now call our "new" vote-method which will be created/renamed in the next step. This way we also seperated the code for different tasks and gave it names others might faster understand: in setNominees we setup the vote by creating the Nominees, in voting() the actual vote is done.

so we have our main_menue now:

public void main_menu(){

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

Well next as said is to write/build/rearange the Code we have to get our voting-Method. Method y() will be deleted as she had no purpose except making a y() call a v1() call. When looking at v1() to v20() there is one thin comming to my mind: don't repeat yourself! (DRY). Well this is the point your teacher want's you to change. The reason is: in case you want to change anyting on the voting process, you have tzo change the same thing 20 times. Maybe you want to have even more voters, what would make even more methods. sO we will call a method which does a loop over her functionality, so we call this method once and every voter can give his vote.

private void voting(){

    /*
    the for-loop iterates as often as your voters-array has fields.
    In case you remove the voters array cause you decide to not NSA into everyones Votes,
    set numbervoters to the number, how often the loop should iterate.
     */
    int numbervoters = voters.length;
    for(int i = 0; i < numbervoters; i++ ){
        Scanner in1 = new Scanner(System.in);
        System.out.printf("Enter #%s Voters Name: \n", i+1);
        String name = in1.nextLine();
        //you might ask now if the Name is correct and build a loop over this.
        //when user misspelled his name you repeat the loop until he entered it correct, just an option
        voters[i] = new Voter(name);
        in1.close();

        do {
            System.out.printf("Choose your Vote: 1. %s 2. %s 3. %s\n", a.getName(), b.getName(), c.getName());
            Scanner in2 = new Scanner(System.in);
            String choice = in2.next();
            in2.close();

            if (choice.equals("1")) {
                System.out.printf("Sure to vote for %s? Y/y=yes", a.getName());
                Scanner in3 = new Scanner(System.in);
                String sure = in3.next();
                in3.close();
                if (sure.equals("Y") || sure.equals("y")) {
                    a.addVote();
                    break;
                }
            } else if (choice.equals("2")) {
                System.out.printf("Sure to vote for %s? Y/y=yes", b.getName());
                Scanner in3 = new Scanner(System.in);
                String sure = in3.next();
                in3.close();
                if (sure.equals("Y") || sure.equals("y")) {
                    b.addVote();
                    break;
                }
            } else if (choice.equals("3")) {
                System.out.printf("Sure to vote for %s? Y/y=yes", c.getName());
                Scanner in3 = new Scanner(System.in);
                String sure = in3.next();
                in3.close();
                if (sure.equals("Y") || sure.equals("y")) {
                    c.addVote();
                    break;
                }
            } else {
                System.out.print("You have to chose one of the 3 Nominees! No chance to not Vote!");
            }
        }
        while(true);
        voters[i].voted();
    }

    System.out.printf("Results are: \n%d votes for %s\n%d votes for %s\n%d votes for %s\n",
            a.countVotes(), a.getName(),
            b.countVotes(), b.getName(),
            c.countVotes(), c.getName());
}

This Method covers all voting doings overy every voter. Hope this helps you.

Below old Post:

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – randyr Jan 3 '17 at 14:47
  • \$\begingroup\$ 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. \$\endgroup\$ – K. Cinches Jan 3 '17 at 15:03
  • \$\begingroup\$ 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 \$\endgroup\$ – SchreiberLex Jan 3 '17 at 15:29
  • \$\begingroup\$ I only add it so the voters can confirm if they really vote that person. nothing special \$\endgroup\$ – K. Cinches Jan 3 '17 at 15:31
  • \$\begingroup\$ 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 \$\endgroup\$ – SchreiberLex Jan 3 '17 at 15:44
5
\$\begingroup\$

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.
\$\endgroup\$
  • \$\begingroup\$ Thank you so much mate, you explained it very well. This is really helpful. This might change the way I write my codes. \$\endgroup\$ – K. Cinches Jan 3 '17 at 13:38
  • \$\begingroup\$ @K.Cinches edited quickly for some minor changes (see bottom) \$\endgroup\$ – randyr Jan 3 '17 at 13:42
  • \$\begingroup\$ @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. \$\endgroup\$ – randyr Jan 3 '17 at 13:52
  • \$\begingroup\$ I see, now I can modify the code from here on out thanks for the big help mate. \$\endgroup\$ – K. Cinches Jan 3 '17 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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