<file_sep>package four.elite.tournament;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class TournamentResults extends AppCompatActivity {

    TextView playerName;
    TextView teamName;

    ImageView playerImage;

    Gson gson;

    Player player;

    ArrayList<Game> listOfPlayerMatches;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tournament_results);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        gson = new Gson();
        
        playerImage = (ImageView)findViewById(R.id.playerLogo);
        playerName = (TextView)findViewById(R.id.playerName);
        teamName = (TextView)findViewById(R.id.teamName);

        listOfPlayerMatches = gson.fromJson(bundle.getString("matches"), new TypeToken<ArrayList<Game>>(){}.getType());
        player = gson.fromJson(bundle.getString("winningPlayer"), Player.class);

        playerName.setText(player.getName());
        teamName.setText(player.getTeamName());

        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.displayImage(player.getImageUrl(), playerImage);


    }

    public void viewMatchHistory(View v){

        Intent intent = new Intent(TournamentResults.this, MatchHistory.class);

        intent.putExtra("matches",gson.toJson(listOfPlayerMatches));

        startActivity(intent);

    }

    public void leave(View v){
        finish();
    }

}
<file_sep>package four.elite.tournament;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class MainCreate extends AppCompatActivity implements AdapterView.OnItemClickListener {

    TextView tournamentName;
    ListView playersListView;

    Tournament newTournament;
    List<Player> players = new ArrayList<Player>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_create);

        tournamentName = (TextView)findViewById(R.id.tournamentNameLabel);
        playersListView = (ListView)findViewById(R.id.playerListView);

        populatePlayers();

    }

    public void addingNewPlayer(View v){
        Intent intent=new Intent(MainCreate.this,PlayerCreation.class);
        startActivityForResult(intent, 200);
    }

    public void populatePlayers(){

        String[] names = new String[players.size()];
        String[] teams = new String[players.size()];
        String[] imageUrls = new String[players.size()];
        int[] rankings = new int[players.size()];


        for(int i=0; i< players.size(); i++){
            names[i] = players.get(i).getName();
            teams[i] = players.get(i).getTeamName();
            imageUrls[i] = players.get(i).getImageUrl();
        }

        MySimpleArrayAdapter arrayAdapter = new MySimpleArrayAdapter(getApplicationContext(), names, teams, rankings, imageUrls);
        playersListView.setAdapter(arrayAdapter);
        playersListView.setOnItemClickListener(this);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == 200 && resultCode == 200){

            String playerName = data.getStringExtra("Player Name");
            String teamName = data.getStringExtra("Team Name");
            String imageUrl = data.getStringExtra("Image URL");


            Player playerToAdd = new Player(teamName, playerName, imageUrl);

            players.add(playerToAdd);

            populatePlayers();

        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

        new AlertDialog.Builder(this)
                .setMessage("Are you sure you want to remove this user?")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        players.remove(position);
                        populatePlayers();
                    }
                })
                .setNegativeButton("No", null)
                .show();

    }

    public void finishCreation(View v){

        if(players.size() < 2){
            Toast toast = Toast.makeText(getApplicationContext(), "Need at least 2 players", Toast.LENGTH_LONG);
            toast.show();
        }else if (tournamentName.getText().toString().equals("")){
            Toast toast = Toast.makeText(getApplicationContext(), "Not a valid Tournament name", Toast.LENGTH_LONG);
            toast.show();
        }else{

            AlertDialog levelDialog;

            final CharSequence[] options = {"Round Robin","Knockout","Round Robin & Knockout"};

            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle("Select type of tournament!");
            dialog.setSingleChoiceItems(options, -1, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int item) {

                    //Setup intent
                    final Intent intent = new Intent();
                    Tournament newTournament;

                    Gson gson = new Gson();

                    switch (item) {
                        case 0:
                            newTournament = new Tournament(tournamentName.getText().toString(),players,options[0].toString());
                            intent.putExtra("tournament-object", gson.toJson(newTournament));
                            setResult(200, intent);
                            finish();
                            break;
                        case 1:
                            if((players.size() & players.size()-1) != 0){
                                Toast toast = Toast.makeText(getApplicationContext(), "Number of Players Must be a Power of 2", Toast.LENGTH_LONG);
                                toast.show();

                            }
                            else {
                                newTournament = new Tournament(tournamentName.getText().toString(), players, options[1].toString());
                                intent.putExtra("tournament-object", gson.toJson(newTournament));
                                setResult(200, intent);
                                finish();
                            }
                            break;
                        case 2:
                            newTournament = new Tournament(tournamentName.getText().toString(),players,options[2].toString());
                            intent.putExtra("tournament-object", gson.toJson(newTournament));
                            setResult(200, intent);
                            finish();
                            break;
                    }
                }
            });

            dialog.setNegativeButton("Cancel", null);
            levelDialog = dialog.create();
            levelDialog.show();

        }

    }

}
<file_sep>package four.elite.tournament;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MatchActivity extends AppCompatActivity {

    NumberPicker homeTeamScore;
    NumberPicker awayTeamScore;

    TextView homeTeamLabel;
    TextView awayTeamLabel;
    TextView homePlayerName;
    TextView awayPlayerName;

    ImageView homeImage;
    ImageView awayImage;

    Game game;

    Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        gson = new Gson();

        String json = bundle.getString("game");
        game = gson.fromJson(json, Game.class);

        //init all textviews and images
        homeTeamLabel = (TextView)findViewById(R.id.homeTeamLabel);
        awayTeamLabel = (TextView)findViewById(R.id.awayTeamLabel);
        homePlayerName = (TextView)findViewById(R.id.homePlayerLabel);
        awayPlayerName = (TextView)findViewById(R.id.awayPlayerLabel);

        homeTeamLabel.setText(game.getHomeTeam().getTeamName());
        awayTeamLabel.setText(game.getAwayTeam().getTeamName());
        homePlayerName.setText(game.getHomeTeam().getName());
        awayPlayerName.setText(game.getAwayTeam().getName());

        //init number pickers
        homeTeamScore = (NumberPicker)findViewById(R.id.homeTeamScore);
        homeTeamScore.setMaxValue(50);
        homeTeamScore.setMinValue(0);

        awayTeamScore = (NumberPicker)findViewById(R.id.awayTeamScore);
        awayTeamScore.setMaxValue(50);
        awayTeamScore.setMinValue(0);

        //init image loader
        ImageLoader imageLoader = ImageLoader.getInstance();
        homeImage = (ImageView)findViewById(R.id.homeTeamImage);
        awayImage = (ImageView)findViewById(R.id.awayTeamImage);
        imageLoader.displayImage(game.getHomeTeam().getImageUrl(),homeImage);
        imageLoader.displayImage(game.getAwayTeam().getImageUrl(),awayImage);

    }

    public void cancel(View v){
        finish();
    }

    public void confirmScore(View v){
        Intent intent = new Intent();

        intent.putExtra("homeScore", homeTeamScore.getValue());
        intent.putExtra("awayScore", awayTeamScore.getValue());

        setResult(200, intent);
        finish();

    }


}
<file_sep>package four.elite.tournament;

import android.content.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Brandon on 2015-12-03.
 */
public class Testing {

    public static void setupFakeTournaments(Context context){

        List<Tournament> tournaments = new ArrayList<Tournament>();

        List<Player> laLigaPlayers = new ArrayList<Player>();
        Player messi = new Player("Barcelona","<NAME>", null);
        Player ronaldo = new Player("<NAME>","<NAME>", null);
        laLigaPlayers.add(messi);
        laLigaPlayers.add(ronaldo);
        Tournament laLiga = new Tournament("La Liga", laLigaPlayers, "Round Robin");

        DataManager.saveTournaments(context, tournaments);

    }

}
<file_sep>package four.elite.tournament;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Player implements Comparable<Player>{

    private String teamName,playerName,imageUrl;

    private int ranking,gamesPlayed, gamesWon, totalGoalsFor, totalGoalsAgainst;

    public Player(){}

    public Player(String teamName, String playerName, String url){
        this.teamName = teamName;
        this.playerName = playerName;
        this.imageUrl = url;
        this.gamesWon = 0;
        this.gamesPlayed = 0;
        if(url == null){
            this.imageUrl = "http://dogr.io/doge.png";
        }

    }

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

    public String getImageUrl(){
        return this.imageUrl;
    }

    public void setImageUrl(String url){
        this.imageUrl = url;
    }

    public String getTeamName(){
        return this.teamName;
    }

    public int getGamesPlayed(){
        return this.gamesPlayed;
    }

    public int getGamesWon(){
        return this.gamesWon;
    }

    public int getRanking(){
        return this.ranking;
    }

    public void setName(String name){
        this.playerName = name;
    }

    public void setTeamName(String teamName){
        this.teamName = teamName;
    }

    public void setRanking(int rank){
        this.ranking = rank;
    }

    public void incrementGamesWon(){
        this.gamesWon++;
    }

    public void incrementGamesPlayed(){
        this.gamesPlayed++;
    }

    public void addGoalsFor(int goals)
    {
        totalGoalsFor += goals;
    }

    public void addGoalsAgainst(int goalsAgainst)
    {
        totalGoalsAgainst += goalsAgainst;
    }

    public int getTotalGoalsFor()
    {
        return totalGoalsFor;
    }

    public int getTotalGoalsAgainst()
    {
        return totalGoalsAgainst;
    }

    public boolean equals(Object obj){
        Player temp = (Player) obj;
        if(this.teamName.equals(temp.getTeamName()) && this.playerName.equals(temp.getName()) && this.imageUrl.equals(temp.getImageUrl())){
            return true;
        }
        return false;
    }

    @Override
    public int compareTo(Player another) {
        if (this.gamesWon > another.getGamesWon()) {
            return -1;
        } else if (this.gamesWon < another.getGamesWon()) {
            return 1;
        } else{
            if (this.totalGoalsFor > another.getTotalGoalsFor()) {
                return -1;
            } else if (this.totalGoalsFor < another.getTotalGoalsAgainst()) {
                return 1;
            }
            else{
                if (this.totalGoalsAgainst < another.getTotalGoalsAgainst()) {
                    return -1;
                }
                else if (this.totalGoalsAgainst < another.getTotalGoalsAgainst()) {
                    return 1;
                }
                else{
                    return 0;
                }
            }
        }
        }
}
<file_sep>package four.elite.tournament;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.List;

public class TournamentMain extends AppCompatActivity implements AdapterView.OnItemClickListener
{
    public Tournament tournament;

    TextView tourneyNameLabel;
    ListView listView;

    Button nextMatch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tournament_main);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();

        tourneyNameLabel = (TextView)findViewById(R.id.tourneyName);
        listView = (ListView)findViewById(R.id.playerList);

        tournament = DataManager.getTournamentByName(getApplicationContext(),bundle.getString("Tournament Name"));
        tourneyNameLabel.setText(tournament.getName());

        if(tournament.isCompleted()){
            nextMatch = (Button)findViewById(R.id.nextMatch);
            nextMatch.setText("Results");
        }

        populatePlayers();
    }

    private void populatePlayers(){

        String[] names = new String[tournament.getPlayers().size()];
        String[] teams = new String[tournament.getPlayers().size()];
        String[] urls = new String[tournament.getPlayers().size()];
        int[] rankings = new int[tournament.getPlayers().size()];

        for(int i=0; i< tournament.getPlayers().size(); i++){
            names[i] = tournament.getPlayers().get(i).getName();
            teams[i] = tournament.getPlayers().get(i).getTeamName();
            rankings[i] = tournament.getPlayers().get(i).getRanking();
            urls[i] = tournament.getPlayers().get(i).getImageUrl();
        }

        MySimpleArrayAdapter arrayAdapter = new MySimpleArrayAdapter(getApplicationContext(), names, teams, rankings, urls);
        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(TournamentMain.this,PlayerInfo.class);
        intent.putExtra("Tournament Name", tournament.getName());
        intent.putExtra("Player Name", tournament.getPlayers().get(position).getName());
        intent.putExtra("Player Team", tournament.getPlayers().get(position).getTeamName());
        intent.putExtra("Player Ranking", tournament.getPlayers().get(position).getRanking());
        intent.putExtra("Player Games", tournament.getPlayers().get(position).getGamesPlayed());
        intent.putExtra("Player Wins", tournament.getPlayers().get(position).getGamesWon());
        intent.putExtra("Image URL", tournament.getPlayers().get(position).getImageUrl());
        this.startActivity(intent);
    }

    public void viewingNextMatch(View v){
        Intent intent = new Intent(TournamentMain.this,MatchActivity.class);

        Gson gson = new Gson();

        //Store anything in bundle here

        Game nextGame = tournament.getNextGame();

        if(nextGame != null){
            intent.putExtra("game", gson.toJson(nextGame));
            this.startActivityForResult(intent, 200);
        }else{
            if(tournament.getType().equals("Round Robin") || tournament.getType().equals("Knockout")){
                tournament.complete(true);

                Intent tourneyResultIntent = new Intent(TournamentMain.this, TournamentResults.class);
                tourneyResultIntent.putExtra("winningPlayer", gson.toJson(tournament.getPlayer(0)));
                tourneyResultIntent.putExtra("matches", gson.toJson(tournament.getPlayedGames()));
                startActivity(tourneyResultIntent);
            }
            else{
                final AlertDialog numDlg;
                final NumberPicker numPkr;

                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
                numPkr = new NumberPicker(dialogBuilder.getContext());

                numPkr.setMinValue(0);
                numPkr.setMaxValue(tournament.getPlayers().size());

                dialogBuilder.setTitle("Player in Knockout Stage");
                dialogBuilder.setView(numPkr);
                dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                dialogBuilder.setPositiveButton("Set", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        int numProceeding = numPkr.getValue();

                        tournament.sortRankings();

                        while(tournament.getPlayers().size() > numProceeding){
                            tournament.getPlayers().remove(tournament.getPlayers().size() - 1);
                        }
                        tournament = new Tournament(tournament.getName(), tournament.getPlayers(), "Knockout");

                        MainActivity.updateTournaments(getApplicationContext(), tournament);

                        populatePlayers();
                    }
                });
                numDlg = dialogBuilder.create();
                numDlg.show();

            }


        }


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //matchActivity result
        if(requestCode == 200 && resultCode == 200){
            //handle return values
            tournament.getNextGame().setHomeTeamScore(data.getIntExtra("homeScore", -1));
            tournament.getNextGame().setAwayTeamScore(data.getIntExtra("awayScore", -1));
            tournament.getNextGame().setPlayed(true);
            tournament.updatePlayer();
            tournament.sortRankings();
            tournament.setRank();
            populatePlayers();
            System.out.println(tournament.getNextGame().getHomeTeam().getGamesPlayed());


            MainActivity.updateTournaments(getApplicationContext(), tournament);

            System.out.println(tournament.getNextGame().getHomeTeam().getGamesPlayed());
            tournament.incrementGameCounter();


            MainActivity.updateTournaments(getApplicationContext(), tournament);
        }

    }

    public void viewMatches(View v){
        Intent intent = new Intent(TournamentMain.this,MatchHistory.class);
        Gson gson = new Gson();
        String matchesSTR = gson.toJson(tournament.getPlayedGames());
        intent.putExtra("matches",matchesSTR);
        this.startActivity(intent);
    }

}
