ลอง code บ้านๆ ซักตัวเผื่อดูว่าใครจะอยู่หรือไป

25Jun10

มันเกิดปัญหาเมื่อผมอยากรู้ผลว่า สเปนจะเข้ารอบได้อย่างไร นั่งเขียนกระดาษ พิมพ์ notepad ก็ดูไม่เห็นภาพ เลยนึกจะเขียน code ครับ ก็เลยลองดู basic พื้นฐาน + พื้นมั่วครับ (-/\-)

จากสถานการณ์จริงของกลุ่มนี้ คะแนน ลูกได้เสียจริง
และผมคิดว่าถ้าจบลงด้วย

Spain 2 – 0 Chile
Switzerland 2 – 0 Honduras


package madness.work.summaryreport;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CheckTeamResult {

@SuppressWarnings("unchecked")
public static void main(String[] args) {
TeamInfo spain = new TeamInfo("Spain", 3, 2, 1);
TeamInfo chile= new TeamInfo("Chile", 6, 2, 0);
TeamInfo switzerland = new TeamInfo("Switzerland", 3, 1, 1);
TeamInfo holdulus = new TeamInfo("Honduras", 0, 0, 3);

List list = whoInWhoOut(spain, 2, chilli, 0, switserland, 2, holdulus, 0);

Collections.sort(list, new Comparator() {

public int compare(Object o1, Object o2) {
TeamInfo team1 = (TeamInfo) o1;
TeamInfo team2 = (TeamInfo) o2;
String ga1 = Integer.toString(team1.getGoalDifferrent());
String ga2 = Integer.toString(team2.getGoalDifferrent());
return ga2.compareToIgnoreCase(ga1);
}

});

for (TeamInfo team : list) {
System.out.println(team.getTeamName() + " " + team.getPoint() + " " + team.getGoalDifferrent());
}

}

@SuppressWarnings("unchecked")
public static List whoInWhoOut(TeamInfo team1, int team1Score, TeamInfo team2, int team2Score,
TeamInfo team3, int team3Score, TeamInfo team4, int team4Score) {
List teams = new ArrayList();

team1.setGoalDifferrent(getGoalDifferent(team1Score, team1.goalScore, team1.goalAgains));
team2.setGoalDifferrent(getGoalDifferent(team2Score, team2.goalScore, team2.goalAgains));
team3.setGoalDifferrent(getGoalDifferent(team3Score, team3.goalScore, team3.goalAgains));
team4.setGoalDifferrent(getGoalDifferent(team4Score, team4.goalScore, team4.goalAgains));

List result1 = getFixture(team1, team1Score, team2, team2Score);
List result2 = getFixture(team3, team3Score, team4, team4Score);

for (TeamInfo team : result1) {
teams.add(team);
}
for (TeamInfo team : result2) {
teams.add(team);
}

Collections.sort(teams, new Comparator() {

public int compare(Object o1, Object o2) {
TeamInfo team1 = (TeamInfo) o1;
TeamInfo team2 = (TeamInfo) o2;
String point1 = Integer.toString(team1.getPoint());
String point2 = Integer.toString(team2.getPoint());
return point2.compareToIgnoreCase(point1);
}

});

return teams;
}

public static int getGoalDifferent(int score, int gs, int ga) {

int goalDiferrent = (score + gs) - ga;
return goalDiferrent;
}

public static ArrayList getFixture(TeamInfo team1, int score1, TeamInfo team2, int score2) {
ArrayList list = new ArrayList();

if (score1 == score2) {
team1.setPoint(team1.getPoint() + 1);
team2.setPoint(team2.getPoint() + 1);
} else if (score1 > score2) {
team1.setPoint(team1.getPoint() + 3);
} else {
team2.setPoint(team2.getPoint() + 3);
}

list.add(team1);
list.add(team2);

return list;
}

public static class TeamInfo {

private String teamName = "";

private int point = 0;

private int goalScore = 0;

private int goalAgains = 0;

private int goalDifferrent = 0;

public TeamInfo(String teamName, int point, int goalScore, int goalAgains) {
super();
this.teamName = teamName;
this.point = point;
this.goalScore = goalScore;
this.goalAgains = goalAgains;
}

public int getGoalAgains() {
return goalAgains;
}

public void setGoalAgains(int goalAgains) {
this.goalAgains = goalAgains;
}

public int getGoalScore() {
return goalScore;
}

public void setGoalScore(int goalScore) {
this.goalScore = goalScore;
}

public int getPoint() {
return point;
}

public void setPoint(int point) {
this.point = point;
}

public String getTeamName() {
return teamName;
}

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

public int getGoalDifferrent() {
return goalDifferrent;
}

public void setGoalDifferrent(int goalDifferrent) {
this.goalDifferrent = goalDifferrent;
}

}
}

Advertisement


One Response to “ลอง code บ้านๆ ซักตัวเผื่อดูว่าใครจะอยู่หรือไป”

  1. 1 jokebenja

    พยายามมากแก ตอเรสรู้คงปลื้มมม


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.