|
Re: Search Function for Soccer tournement system. searching strings of arra
|
Posted: Mar 14, 2005 8:51 AM
|
|
Add the team name as info to the player info. If you don't, then you have to searchall teams for a player with this name.
I would not use String arrays at all. this would be my approach to solve the problem. Note that I wrote it in a standard text editor and did not compile it, so it might contain minor errors.
public class Team () {
private String teamName;
private boolean isUpdating;
private boolean PlayerList myPlayers = new PlayerList();
public Team (String teamName) {
this.teamName = teamName;
}
public String getTeamName () {
return teamName;
}
public void addPlayer (Player p) {
if (isUpdating)
return;
isUpdating = true;
if (p != null) {
if (!worksForMe(p)) {
p.workFor(this);
myPlayers.add(p);
}
}
isUpdating = false;
}
public void removePlayer (Player p) {
if (isUpdating)
return;
isUpdating = true;
if (worksForMe) {
myPlayers.remove (p);
p.yourFired();
}
isUpdating = false;
}
private boolean worksForMe (Player p) {
if (p== null)
return false;
return myPlayers.contains (p);
}
}
public class Player () {
String fName;
String lName;
String id;
String suburb;
Team workingFor = null;
public Player (String fName, String lName, String id, String suburb) {
this.fname = fname;
this.lName = lName;
this.id = id;
this.suburb = suburb;
}
public String getFName () {
return fName;
}
public String getLName () {
return lName;
}
public String getID () {
return id;
}
public String getSuburb () {
return suburb;
}
public void workFor (Team newTeam) {
if (isUpdating)
return;
isUpdating = true;
if (workingFor != null)
workingFor.removePlayer (this);
workingFor = newTeam;
if (newTeam != null)
newTeam.addPlayer (this);
isUpdating = false;
}
public void yourFired () {
if (isUpdating)
return;
isUpdating = true;
if (workingFor != null)
workingFor.removePlayer (this);
isUpdating = false;
}
public Team getTeam () {
return workingFor;
}
}
public class PlayerList exteds Vector {
/** Converts the returned Object into Player
*/
public Player cGet(int index) {
Object o = get(index);
return (o == null) ? null : (Player)o;
}
public Player cGetLast(int index) {
Object o = getLast();
return (o == null) ? null : (Player)o;
}
}
public class TeamList exteds Vector {
/** Converts the returned Object into Player
*/
public Team cGet(int index) {
Object o = get(index);
return (o == null) ? null : (Team)o;
}
}
public class caller {
public void main (String args[]) {
TeamList teams = new TeamList();
PlayerList allPlayers = new PlayerList();
for (int i = 0; i < 10; i++) {
teams.add(new Team("Team " + i));
}
for (int i = 0; i < 100; i++) {
allPlayers.add(("fName" + i, "LName" + i, "pl" + i, "dada"));
allPlayers.cGetLast().workFor (teams.cGet (i % 10));
}
Player p = allPlayers.cGet ((int)Math.rnd() * 100);
System.out.println (p.getTeam().getTeamName());
}
}
|
|