Avatar of AlKYala

AlKYala's solution

to Tournament in the Java Track

Published at Oct 04 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());
    }
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

public class Tournament {
	
	public Tournament() {}
	
	private String table = String.format("Team                           | MP |  W |  D |  L |  P");
	
	private HashMap<String, Integer> points = new HashMap<String, Integer>();
	private HashMap<String, Integer> wins = new HashMap<String, Integer>();
	private HashMap<String, Integer> losses = new HashMap<String, Integer>();
	private HashMap<String, Integer> draws = new HashMap<String, Integer>();
	private HashMap<String, Integer> matchesPlayed = new HashMap<String, Integer>();
	
	ArrayList<String> teams = new ArrayList<String>(); //eintragen aller bekannten teams
	ArrayList<Team> team;
	
	void applyResultsLine(String s) {
		String[] res = s.split(";");
		
		if(points.get(res[0]) == null){ //team exists nowhere
			points.put(res[0], 0); //will be updated
			wins.put(res[0], 0);//will be updated
			losses.put(res[0], 0);//will be updated
			draws.put(res[0], 0);//will be updated
			matchesPlayed.put(res[0], 0); 
			teams.add(res[0]);
		}
		
		if(points.get(res[1]) == null){ //team exists nowhere
			points.put(res[1], 0); //will be updated
			wins.put(res[1], 0);//will be updated
			losses.put(res[1], 0);//will be updated
			draws.put(res[1], 0);//will be updated
			matchesPlayed.put(res[1], 0);
			teams.add(res[1]);
		}
		
		//System.out.println(res[0]);
		//System.out.println(res[1]);
		
		if(res[2].equals("win")) { //res0 wins
			points.put(res[0], points.get(res[0]) + 3);
			wins.put(res[0], wins.get(res[0]) + 1);
			losses.put(res[1], losses.get(res[1]) + 1);
			matchesPlayed.put(res[0], matchesPlayed.get(res[0]) + 1);
			matchesPlayed.put(res[1], matchesPlayed.get(res[1]) + 1);
		}
		else if(res[2].equals("loss")) { //res1 wins
			points.put(res[1], points.get(res[1]) + 3);
			wins.put(res[1], wins.get(res[1]) + 1);
			losses.put(res[0], losses.get(res[0]) + 1);
			matchesPlayed.put(res[0], matchesPlayed.get(res[0]) + 1);
			matchesPlayed.put(res[1], matchesPlayed.get(res[1]) + 1);
		}
		else if(res[2].equals("draw")) {
			points.put(res[0], points.get(res[0]) + 1);
			points.put(res[1], points.get(res[1]) + 1);
			draws.put(res[0], draws.get(res[0]) +1);
			draws.put(res[1], draws.get(res[1]) +1);
			matchesPlayed.put(res[0], matchesPlayed.get(res[0]) + 1);
			matchesPlayed.put(res[1], matchesPlayed.get(res[1]) + 1);
		}
		
	}
	
	public void applyResults(String x) {
		String[] sa = x.split("\n");
		
		for(int i = 0; i < sa.length; i++) {
			this.applyResultsLine(sa[i]);
		}
	}
	
	public Object[] getResultsOfTeam(String s) {
		Object[] x = new Object[6];
		x[0] = s;
		x[1] = matchesPlayed.get(s);
		x[2] = wins.get(s);
		x[3] = draws.get(s);
		x[4] = losses.get(s);
		x[5] = points.get(s);
		return x;
	}
	
	public ArrayList<Team> createTable() {
		
		ArrayList<Team> content = new ArrayList<Team>();
		//System.out.println(this.teams);
		
		
		for(int i = 0; i < teams.size(); i++) {
			String s = teams.get(i);
			content.add(new Team(s, matchesPlayed.get(s),
			wins.get(s),
			draws.get(s),
			losses.get(s),
			points.get(s)));
			
		}
		
		if(content.size() == 0) {
			return null;
		}
		
		return content;
	}
	
	String printTable() {
		try {
			this.sortTable();
		} catch(NullPointerException e) {
			return this.table;
		}
		
		
		String s = table+"\n";
		
		if(this.team == null) {
			return s;
		}
		
		for(int i = 0; i < this.team.size(); i++) {
			s += String.format("%s\n", this.team.get(i).toString());
		}
		return s;
	}
	
	public void sortTable() {
		this.team = this.createTable();
		if(this.createTable() == null) {
			return;
		}
	    Comparator<Team> teamComparator
	      = Comparator.comparing(Team::getPoints).reversed()
	    	.thenComparing(Team::getName);
	     
	    Collections.sort(this.team, teamComparator);

	}
	
	
	/*public static void main(String[] args) {
		Tournament a = new Tournament();
		a.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");
		//System.out.println(Arrays.toString(a.getResultsOfTeam("Allegoric Alaskans")));
		System.out.println(a.printTable());
	}*/
	
	

}


class Team{
	
	public Team(String s, int mp, int wins, int draws, int losses, int points){
		this.name = s;
		this.mp = mp;
		this.wins = wins;
		this.draws = draws;
		this.losses = losses;
		this.points = points;
	}
	
	Integer getPoints() {
		return this.points;
	}
	
	String getName() {
		return this.name;
	}
	
	String name;
	Integer points;
	int mp;
	int wins;
	int losses;
	int draws;
	
	public String  teamF(String s) {
		int x = s.length();
		String xx = "";
		for(int i = 0; i < 31-x; i++) {
			xx += " ";
		}
		return s + xx;
	}
	
	public String toString() {
		return String.format("%s|  %d |  %d |  %d |  %d |  %d", teamF(this.name), this.mp, this.wins, this.draws, this.losses, this.points);
	}
	
}

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?