Avatar of argonauta

argonauta's solution

to Tournament in the Java Track

Published at Jul 26 2019 · 0 comments
Instructions
Test suite
Solution

Tally the results of a small football competition.

Based on an input file containing which team played against which and what the outcome was, create a file with a table like this:

Team                           | MP |  W |  D |  L |  P
Devastating Donkeys            |  3 |  2 |  1 |  0 |  7
Allegoric Alaskans             |  3 |  2 |  0 |  1 |  6
Blithering Badgers             |  3 |  1 |  0 |  2 |  3
Courageous Californians        |  3 |  0 |  1 |  2 |  1

What do those abbreviations mean?

  • MP: Matches Played
  • W: Matches Won
  • D: Matches Drawn (Tied)
  • L: Matches Lost
  • P: Points

A win earns a team 3 points. A draw earns 1. A loss earns 0.

The outcome should be ordered by points, descending. In case of a tie, teams are ordered alphabetically.

Input

Your tallying program will receive input that looks like:

Allegoric Alaskans;Blithering Badgers;win
Devastating Donkeys;Courageous Californians;draw
Devastating Donkeys;Allegoric Alaskans;win
Courageous Californians;Blithering Badgers;loss
Blithering Badgers;Devastating Donkeys;loss
Allegoric Alaskans;Courageous Californians;win

The result of the match refers to the first team listed. So this line

Allegoric Alaskans;Blithering Badgers;win

Means that the Allegoric Alaskans beat the Blithering Badgers.

This line:

Courageous Californians;Blithering Badgers;loss

Means that the Blithering Badgers beat the Courageous Californians.

And this line:

Devastating Donkeys;Courageous Californians;draw

Means that the Devastating Donkeys and Courageous Californians tied.

Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

$ gradle test

Use gradlew.bat if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore("Remove to run test") annotation.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

TournamentTest.java

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class TournamentTest {

    private Tournament tournament;

    @Before
    public void setUp() {
        tournament = new Tournament();
    }

    @Test
    public void justTheHeaderIfNoInput() {
        assertEquals("Team                           | MP |  W |  D |  L |  P\n", tournament.printTable());
    }

    @Ignore("Remove to run test")
    @Test
    public void aWinIsThreePointsALossIsZeroPoints() {
        tournament.applyResults("Allegoric Alaskans;Blithering Badgers;win");
        assertEquals(
                "Team                           | MP |  W |  D |  L |  P\n" +
                "Allegoric Alaskans             |  1 |  1 |  0 |  0 |  3\n" +
                "Blithering Badgers             |  1 |  0 |  0 |  1 |  0\n",
                tournament.printTable());
    }

    @Ignore("Remove to run test")
    @Test
    public void aWinCanAlsoBeExpressedAsALoss() {
        tournament.applyResults("Blithering Badgers;Allegoric Alaskans;loss");
        assertEquals(
                "Team                           | MP |  W |  D |  L |  P\n" +
                "Allegoric Alaskans             |  1 |  1 |  0 |  0 |  3\n" +
                "Blithering Badgers             |  1 |  0 |  0 |  1 |  0\n",
                tournament.printTable());
    }

    @Ignore("Remove to run test")
    @Test
    public void aDifferentTeamCanWin() {
        tournament.applyResults("Blithering Badgers;Allegoric Alaskans;win");
        assertEquals(
                "Team                           | MP |  W |  D |  L |  P\n" +
                "Blithering Badgers             |  1 |  1 |  0 |  0 |  3\n" +
                "Allegoric Alaskans             |  1 |  0 |  0 |  1 |  0\n",
                tournament.printTable());
    }

    @Ignore("Remove to run test")
    @Test
    public void aDrawIsOnePointEach() {
        tournament.applyResults("Allegoric Alaskans;Blithering Badgers;draw");
        assertEquals(
                "Team                           | MP |  W |  D |  L |  P\n" +
                "Allegoric Alaskans             |  1 |  0 |  1 |  0 |  1\n" +
                "Blithering Badgers             |  1 |  0 |  1 |  0 |  1\n",
                tournament.printTable());
    }

    @Ignore("Remove to run test")
    @Test
    public void thereCanBeMoreThanOneMatch() {
        tournament.applyResults(
                "Allegoric Alaskans;Blithering Badgers;win\n" +
                "Allegoric Alaskans;Blithering Badgers;win");
        assertEquals(
                "Team                           | MP |  W |  D |  L |  P\n" +
                "Allegoric Alaskans             |  2 |  2 |  0 |  0 |  6\n" +
                "Blithering Badgers             |  2 |  0 |  0 |  2 |  0\n",
                tournament.printTable());
    }

    @Ignore("Remove to run test")
    @Test
    public void thereCanBeMoreThanOneWinner() {
        tournament.applyResults(
                "Allegoric Alaskans;Blithering Badgers;loss\n" +
                "Allegoric Alaskans;Blithering Badgers;win");
        assertEquals(
                "Team                           | MP |  W |  D |  L |  P\n" +
                "Allegoric Alaskans             |  2 |  1 |  0 |  1 |  3\n" +
                "Blithering Badgers             |  2 |  1 |  0 |  1 |  3\n",
                tournament.printTable());
    }

    @Ignore("Remove to run test")
    @Test
    public void thereCanBeMoreThanTwoTeams() {
        tournament.applyResults(
                "Allegoric Alaskans;Blithering Badgers;win\n" +
                "Blithering Badgers;Courageous Californians;win\n" +
                "Courageous Californians;Allegoric Alaskans;loss");
        assertEquals(
                "Team                           | MP |  W |  D |  L |  P\n" +
                "Allegoric Alaskans             |  2 |  2 |  0 |  0 |  6\n" +
                "Blithering Badgers             |  2 |  1 |  0 |  1 |  3\n" +
                "Courageous Californians        |  2 |  0 |  0 |  2 |  0\n",
                tournament.printTable());
    }

    @Ignore("Remove to run test")
    @Test
    public void typicalInput() {
        tournament.applyResults(
                "Allegoric Alaskans;Blithering Badgers;win\n" +
                "Devastating Donkeys;Courageous Californians;draw\n" +
                "Devastating Donkeys;Allegoric Alaskans;win\n" +
                "Courageous Californians;Blithering Badgers;loss\n" +
                "Blithering Badgers;Devastating Donkeys;loss\n" +
                "Allegoric Alaskans;Courageous Californians;win");
        assertEquals(
                "Team                           | MP |  W |  D |  L |  P\n" +
                "Devastating Donkeys            |  3 |  2 |  1 |  0 |  7\n" +
                "Allegoric Alaskans             |  3 |  2 |  0 |  1 |  6\n" +
                "Blithering Badgers             |  3 |  1 |  0 |  2 |  3\n" +
                "Courageous Californians        |  3 |  0 |  1 |  2 |  1\n",
                tournament.printTable());
    }

    @Ignore("Remove to run test")
    @Test
    public void incompleteCompetition() {
        tournament.applyResults(
                "Allegoric Alaskans;Blithering Badgers;loss\n" +
                "Devastating Donkeys;Allegoric Alaskans;loss\n" +
                "Courageous Californians;Blithering Badgers;draw\n" +
                "Allegoric Alaskans;Courageous Californians;win");
        assertEquals(
                "Team                           | MP |  W |  D |  L |  P\n" +
                "Allegoric Alaskans             |  3 |  2 |  0 |  1 |  6\n" +
                "Blithering Badgers             |  2 |  1 |  1 |  0 |  4\n" +
                "Courageous Californians        |  2 |  0 |  1 |  1 |  1\n" +
                "Devastating Donkeys            |  1 |  0 |  0 |  1 |  0\n",
                tournament.printTable());
    }

    @Ignore("Remove to run test")
    @Test
    public void tiesBrokenAlphabetically() {
        tournament.applyResults(
                "Courageous Californians;Devastating Donkeys;win\n" +
                "Allegoric Alaskans;Blithering Badgers;win\n" +
                "Devastating Donkeys;Allegoric Alaskans;loss\n" +
                "Courageous Californians;Blithering Badgers;win\n" +
                "Blithering Badgers;Devastating Donkeys;draw\n" +
                "Allegoric Alaskans;Courageous Californians;draw");
        assertEquals(
                "Team                           | MP |  W |  D |  L |  P\n" +
                "Allegoric Alaskans             |  3 |  2 |  1 |  0 |  7\n" +
                "Courageous Californians        |  3 |  2 |  1 |  0 |  7\n" +
                "Blithering Badgers             |  3 |  0 |  1 |  2 |  1\n" +
                "Devastating Donkeys            |  3 |  0 |  1 |  2 |  1\n",
                tournament.printTable());
    }
}

src/main/java/Info.java

public class Info implements Comparable<Info> {
        private String nombre;
        private int partido;
        private  int win;
        private  int draw;
        private  int less;
        private int total;
    public Info() {
    }

    public Info(String nombre, int partido, int win, int draw, int less) {
        this.nombre = nombre;
        this.partido = partido;
        this.win = win;
        this.draw = draw;
        this.less = less;
    }

    public String getNombre() {
        return nombre;
    }
    
    

    public int getPartido() {
        return partido;
    }

    public int getWin() {
        return win;
    }

    public int getDraw() {
        return draw;
    }

    public int getLess() {
        return less;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    @Override
    public int compareTo(Info o) {
       int  c=(int)(this.total - o.getTotal());//To change body of generated methods, choose Tools | Templates.
        int u=0;
       
      
       
       
        if (c==0) {
           u=this.nombre.compareToIgnoreCase(o.getNombre());
        }else {
             if (total>o.getTotal()) {
                u= -1;
            }
            if (total < o.getTotal()) {
               u= 1;
            }
        }
        return u;
    }
        
       
        
        
}

src/main/java/Tournament.java

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


public class Tournament {
    private String datos;
    
   

    public void applyResults(String datos) {
        this.datos = datos;
    }
    
    
    String printTable(){
        
        String patron2="%s\t| %d  |  %d |  %d |  %d |  %d\n";
        String pa="%s\t\t\t| %s |  %s |  %s |  %s |  %s\n";
     
       
      
        String ans=String.format(pa,"Team","MP","W","D","L","P");
        if (!datos.isEmpty()) {
             
           
         
        
           Map<String,Integer> equipos=new HashMap<>();
           Map<String,Info>lista2=new HashMap<>();
            String resultados []=datos.split("\n");
              List<Info>listas=new ArrayList<>();
            ///begin for
            
          
             int cont1=-1;
             
            for (int i = 0; i < resultados.length; i++) {
             
                 
             
               
                String resultado [] = resultados[i].split(";");
               
               cont1++;
               
                    String string = resultado[2];
                    
                    switch(string){
                    case "win":
                      
                        equipos.put(resultado[1],cont1);
                         equipos.put(resultado[0],cont1);
                       listas.add(new Info(resultado[0], 1, 1, 0, 0));
                       listas.add(new Info(resultado[1], 1, 0, 0, 1));
                       
                 
                    break;  
                     case "loss":
                         equipos.put(resultado[1],cont1);
                           equipos.put(resultado[0],cont1);
                       listas.add(new Info(resultado[0], 1, 0, 0, 1));
                       listas.add(new Info(resultado[1], 1, 1, 0, 0)); 
                         
                    
                    break;  
                       
                  
                     case "draw":
                         
                          equipos.put(resultado[1],cont1);
                           equipos.put(resultado[0],cont1);
                          listas.add(new Info(resultado[0], 1, 0, 1, 0));
                          listas.add(new Info(resultado[1], 1, 0, 1, 0)); 
                    break; 
                } 
                    
                    
             
                
                
            }
            
            ///end for
            
           
            for (Map.Entry<String, Integer> entry : equipos.entrySet()) {
                String key = entry.getKey();
               int ty=0;
                int draw = 0;
               int win = 0;
                int less=0;
                String n="";
                for (Info lista : listas) {
                    if (key.equalsIgnoreCase(lista.getNombre())) {
                        ty+=lista.getPartido();
                        win+=lista.getWin();
                        draw+=lista.getDraw();
                        less+=lista.getLess();
                         n=lista.getNombre();
                       
                    }
                    
                }
                /// ans+=n+"\t\t"+ty+"     "+win+"    "+draw+"    "+less+"    "+((3*win)+(1*draw))+"\n";
                Info in=new Info(n,ty,win,draw,less);
                in.setTotal(((3*win)+(1*draw)));
                lista2.put(n, in);
            }
            
            
     
           List<Info> employeeById = new ArrayList<>(lista2.values());
            Collections.sort(employeeById);
           String patron="%s             | %d  |  %d |  %d |  %d |  %d\n";
            
            for (Info info : employeeById) {
                ans+= String.format(patron2,info.getNombre(),info.getPartido(),info.getWin(),info.getDraw(),info.getLess(),info.getTotal());
            }
            
        }
        
        return ans;
    }
    
    
    
    
   

    
    
    
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?