I'm currently working on a knock-out tournament
Overall Requirements:
-UserInput for 8 teams (teamName and playerNames) stored in a file (MY test is currently with 2 teams)
-Setup for matches (who vs. who) stored in a file
-Results of the matches with points to the team(s) stored in a file
-Ranking so we know who is out/in of the tournament
*****CURRENT PROBLEM IS THIS*******
I need a method which takes a team from my ArrayList<Team> or file (teamData.txt) and put it against another team - BOTH chosen from the user
e.g. with 8 teams (team1-team8)
team1 vs. team5
Afterwards saves the match data in a file
I HAVE NO CLUE ON HOW MY APPROACH SHOULD BE IN THIS... Any inputs? Thanks in advance
public class Main { public static void main(String[] args) { Tournament tournament = IO.input(2); IO.writeTeamFile(tournament); FileIO.readGameData(); } }import java.util.ArrayList; public class Tournament { private ArrayList<Team> teams; public Tournament() { this.teams = new ArrayList<Team>(); } public ArrayList<Team> getTeams() { return teams; } }
import java.io.FileWriter; import java.util.Scanner; public class IO { private static Scanner scan = new Scanner(System.in); public static int teamCount; private static String readUserInput(String msg) { System.out.print(msg); return scan.nextLine(); } public static Tournament input(int numTeams) { Tournament tournament = new Tournament(); for (int i = 0; i < numTeams; i++) { String teamName = readUserInput("Team name: "); Team team = new Team(teamName); tournament.getTeams().add(team); int numPlayersPerTeam = Integer.parseInt(readUserInput("Hvor mange spillere er der på holdet?")); for (int j = 0; j < numPlayersPerTeam; j++) { String playerName = readUserInput("Player name: "); Player player = new Player(playerName); team.getPlayers().add(player); } } return tournament; } public static void writeTeamFile(Tournament tournament) { try{ FileWriter fileWriterDefault = new FileWriter("src/teamData.txt"); fileWriterDefault.write("teamName, teamPlayers"); fileWriterDefault.close(); } catch (Exception e) { e.getStackTrace(); } for (Team team : tournament.getTeams()) { String fullPlayerNames = ""; teamCount ++; //Later use to numMatches for (Player player : team.getPlayers()) { String tmpPlayer = player.getName() + ", "; fullPlayerNames = fullPlayerNames + tmpPlayer; } try { FileWriter fileWriter = new FileWriter("src/teamData.txt", true); fileWriter.write("\n"+ team.getName() + ", " + fullPlayerNames); fileWriter.close(); } catch (Exception e) { e.getStackTrace(); } } System.out.println(teamCount); } }
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class FileIO { public static void readGameData() { String path = "src/teamData.txt"; String teamNamesFile = ""; try{ BufferedReader br = new BufferedReader(new FileReader(path)); br.readLine(); //Reads first line, so it skips the header while((teamNamesFile = br.readLine()) != null){ String [] teamNames = teamNamesFile.split(","); System.out.println("Team fra fil :" + teamNames[0]); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
teamData.txt under here:
teamName, teamPlayers
teamA, A1, A2, A3, A4,
teamB, B1, B2, B3, B4
public class Match { public static void Match() { } }