Question

Modify the method so that instead of manually making teams, it reads it from a text...

Modify the method so that instead of manually making teams, it reads it from a text file called TeamsList.txt. Everything must be done in this method.

public void make_test_teams() {
List<Team> teams = new ArrayList<Team>();

Player burns = new Player("George Burns", "George");
Player allen = new Player("Gracie Allen", "Gracie");
List<Player> ba = new ArrayList<Player>();
ba.add(burns); ba.add(allen);
Team burns_and_allen = new Team("Burns and Allen", ba);
teams.add(burns_and_allen);

Player abbott = new Player("William Abbott", "Bud");
Player costello = new Player("Louis Cristillo", "Lou");
List<Player> ac = new ArrayList<Player>();
ac.add(abbott); ac.add(costello);
Team abbott_and_costello = new Team("Abbott and Costello", ac);
teams.add(abbott_and_costello);

Player chico = new Player("Leonard Marx", "Chico");
Player groucho = new Player("Julius Marx", "Groucho");
Player harpo = new Player("Adolph Marx", "Harpo");
List<Player> mb = new ArrayList<Player>();
mb.add(chico); mb.add(groucho); mb.add(harpo);
Team marx_brothers = new Team("Marx Brothers", mb);
teams.add(marx_brothers);

store_teams(teams);
   }

the text file looks like

team: "Burns and Allen"
player: fullname: George Burns, nickname: George
player: fullname: Gracie Allen, nickname: Gracie

team: "Abbott and Costello"
player: fullname: William Abbott, nickname: Bud
player: fullname: Louis Cristillo, nickname: Lou

team: "Marx Brothers"
player: fullname: Leonard Marx, nickname: Chico
player: fullname: Julius Marx, nickname: Groucho
player: fullname: Adolph Marx, nickname: Harpo

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Method implementation:

public void make_test_teams() throws Exception {
//replace it with path
FileInputStream fstream = new FileInputStream("C:\Users\janikiramreddy.a\Desktop\teams.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
List<Team> teams = new ArrayList<Team>();
List<Player> ba = new ArrayList<Player>();
String teamName="null";
while ((strLine = br.readLine()) != null) {
if(strLine.startsWith("team")) {
if(teamName.equals("null")) {
String te[]=strLine.split(":");
teamName=te[1];
}
else {
Team t=new Team(teamName,ba);
teams.add(t);
String te[]=strLine.split(":");
teamName=te[1];
}
}
else if(strLine.startsWith("player")){
String[] pe=strLine.split(":");
int i=0;
for(String s:pe) {
i++;
}
String[] players=pe[2].split(",");
String player=players[1];
String nick=pe[3];
Player pa=new Player(player,nick);
ba.add(pa);
}
  
}
Team t=new Team(teamName,ba);
teams.add(t);

store_teams(teams);

}

code to test the method:

we have MakeTeams class to test method

and Teams.java and player.java to satisfy requirements

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class MakeTeams {
public static void main(String[] args) throws Exception {
make_test_teams();
}
public static void make_test_teams() throws Exception {
//replace it with path
FileInputStream fstream = new FileInputStream("C:\Users\janikiramreddy.a\Desktop\teams.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
List<Team> teams = new ArrayList<Team>();
List<Player> ba = new ArrayList<Player>();
String teamName="null";
while ((strLine = br.readLine()) != null) {
if(strLine.startsWith("team")) {
if(teamName.equals("null")) {
String te[]=strLine.split(":");
teamName=te[1];
}
else {
Team t=new Team(teamName,ba);
teams.add(t);
String te[]=strLine.split(":");
teamName=te[1];
}
}
else if(strLine.startsWith("player")){
String[] pe=strLine.split(":");
int i=0;
for(String s:pe) {
i++;
}
String[] players=pe[2].split(",");
String player=players[1];
String nick=pe[3];
Player pa=new Player(player,nick);
ba.add(pa);
}
  
}
Team t=new Team(teamName,ba);
teams.add(t);
System.out.println("teams");
for(Team t1:teams) {
System.out.println(t1.getTeamName());
}
}
}

Team.java:

import java.util.List;

public class Team {

   private String teamName;
   public String getTeamName() {
       return teamName;
   }

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

   private List<Player> ba;

   public Team(String teamName, List<Player> ba) {
       // TODO Auto-generated constructor stub
   this.teamName=teamName;
   this.ba=ba;
   }
}

Player.java:

public class Player {
private String nick;
private String player;
public Player(String player, String nick) {
// TODO Auto-generated constructor stub
this.player=player;
this.nick=nick;
}

}

output:

Add a comment
Know the answer?
Add Answer to:
Modify the method so that instead of manually making teams, it reads it from a text...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify the method so that instead of manually making teams, it reads it from a text...

    Modify the method so that instead of manually making teams, it reads it from a text file called TeamsList.txt. Everything must be done in this method. public void make_test_teams() { List<Team> teams = new ArrayList<Team>(); Player burns = new Player("George Burns", "George"); Player allen = new Player("Gracie Allen", "Gracie"); List<Player> ba = new ArrayList<Player>(); ba.add(burns); ba.add(allen); Team burns_and_allen = new Team("Burns and Allen", ba); teams.add(burns_and_allen); Player abbott = new Player("William Abbott", "Bud"); Player costello = new Player("Louis Cristillo", "Lou"); List<Player>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT