By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
463,190 Members | 667 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 463,190 IT Pros & Developers. It's quick & easy.

Football League Program

P: 2
Expand|Select|Wrap|Line Numbers
  1. import java.util.Arrays;
  2. import java.util.Collections;
  3.  
  4. public class Test {
  5.  
  6.     public static void main (String [] arg) {
  7.     //                     "012345678910" alignment guide 
  8.     String [] results = {  "AR  4 BR  5",
  9.                            "CR  0 DR  1",
  10.                            "AR 10 CR  9",
  11.                            "BR  3 DR  1",
  12.                            "AR  0 DR  2",
  13.                            "BR  2 AR  2",
  14.                            "BR  0 CR  5",
  15.                            "CR  1 BR  3",
  16.                            "CR  4 AR  2",
  17.                            "DR  1 AR  1",
  18.                            "DR  2 BR  1",
  19.                            "DR  0 CR  2",
  20.  
  21.                            "ES  2 FO  2",
  22.                            "GR  0 HE  0",
  23.                            "FO  1 GR  2",
  24.                            "HE  3 ES  5",
  25.                            "ES  4 GR  3",
  26.                            "ES  0 HE  2",
  27.                            "GR  1 FO  3",
  28.                            "GR  2 ES  1",
  29.                            "FO  2 HE  1",
  30.                            "FO  3 ES  3",
  31.                            "HE  1 FO  0",
  32.                            "HE  2 GR  1"};                    
  33.  
  34.  
  35.  
  36.     League myLeague = new League();
  37.     myLeague.setupDiv1();
  38.     myLeague.setupDiv2();
  39.     for(String r: results){
  40.     myLeague.addResult(r);    
  41.     }// end for each r    
  42.  
  43.     myLeague.print(); 
  44.     Arrays.sort(results);
  45.     myLeague.print();
  46.     //myLeague.testFindTeam("DR");
  47.  
  48.  
  49.     } //end method main ()
  50.  
  51.  
  52.  
  53. }// end class Test
  54.  
  55.  
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.util.Arrays;
  3. import java.util.Vector;
  4. import java.util.Collections;
  5.  
  6. public class League {
  7.  
  8.     Vector<Team> teams;
  9.  
  10.     String [] list1 = {"AR Arad", "BR Braila", "CR Craiova", "DR Drobeta"};
  11.     String [] list2 = {"ES Escandov", "FO Foscani", "GR Gronstadt", "HE Hermansburg"};
  12.  
  13.  
  14.     public League() {
  15.     teams = new Vector<Team>();
  16.     }// end constructor League()
  17.  
  18.     public void setupDiv1 () {
  19.     for (int i=0; i<list1.length; i++) {
  20.         String code = list1[i].substring(0,2);
  21.         String name = list1[i].substring(3);
  22.         addTeam(1, code, name);
  23.     } //end for i
  24.     }// end method setupDiv1 ()
  25.  
  26.  
  27.     public void setupDiv2 () {
  28.     for (int i=0; i<list2.length; i++) {
  29.         String code = list2[i].substring(0,2);
  30.         String name = list2[i].substring(3);
  31.         addTeam(2, code, name);
  32.     } //end for i
  33.     }// end method setupDiv2 ()
  34.  
  35.  
  36.     public void addTeam (int division, String code, String name) {
  37.     Team aTeam = new Team(division, code, name);
  38.     teams.add(aTeam);
  39.     }// end method addTeam()
  40.  
  41.     public Team findTeam(String code) {
  42.         Team aTeam = null;
  43.         for (int i=0; i<teams.size(); i++) {
  44.             aTeam = teams.get(i);
  45.             if (aTeam.code.equals (code))break;
  46.         }// end for i
  47.         return aTeam;
  48.     }//end method findTeam()
  49.  
  50.     public void testFindTeam(String code) {
  51.     Team aTeam = findTeam(code);
  52.     System.out.println("Found Team = "+aTeam.name);
  53.     }//end method findTeam()
  54.  
  55.  
  56.     public void addResult(String result){
  57.     String hCode = "AR";
  58.     String aCode = "BR";
  59.     int hScore = 4;
  60.     int aScore = 5;
  61.     //         "01234567890" alignment guide 
  62.     //result = "AR  4 BR  5"
  63.     hCode = result.substring(0,2).trim();
  64.     aCode = result.substring(6,8).trim();
  65.  
  66.     String hS = result.substring(3,5).trim();
  67.     String aS = result.substring(9).trim();
  68.  
  69.     hScore = Integer.parseInt(hS);
  70.     aScore = Integer.parseInt(aS);
  71.  
  72.     Team homeTeam = this.findTeam(hCode);
  73.     homeTeam.addHomeResult(hScore,aScore);
  74.  
  75.     Team awayTeam = this.findTeam(aCode);
  76.     awayTeam.addAwayResult(hScore,aScore);
  77.  
  78.  
  79.     }//end method addResult() 
  80.  
  81.     public void print() {
  82.         for (Team aTeam: teams) {
  83.             aTeam.print();
  84.  
  85.         }// end forall teams
  86.     }// end method print()
  87.  
  88. }// end class League
  89.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.*;
  3. import java.util.Collections;
  4. import java.util.Comparator;
  5.  
  6. public class Team implements Comparable <Team> {
  7.  
  8. int division;
  9. String name;
  10. String code;
  11. int points;
  12. int played;
  13. int scored;
  14. int conceded;
  15. int difference;
  16.  
  17. public int compareTo(Team aTeam){
  18. //if 
  19. return 0;
  20. }//end method compareTo() 
  21.  
  22. public Team(int division, String code, String name) {
  23. this.division = division;
  24. this.code = code;
  25. this.name = name;
  26. }// end constructor Team(code, name)
  27.  
  28. public void addResult(int home, int away) {
  29. // home and away are goals score by home and away teams in a match
  30. }// end method addResult()
  31.  
  32. public void addHomeResult(int home, int away) {
  33. if (home==away) points = points + 1;  // Draw
  34. if (home > away) points = points + 2; // Home Win
  35. //if (home < away) points = points + 3; // Away Win
  36. played++;
  37. scored = scored + home;
  38. conceded = conceded + away;
  39. difference = scored - conceded;
  40. }// end method addHomeResult()
  41.  
  42. public void addAwayResult(int home, int away) {
  43. if (home==away) points = points + 1;  // Draw
  44. //if (home > away) points = points + 2; // Home Win
  45. if (home < away) points = points + 3; // Away Win
  46. played++;
  47. scored = scored + home;
  48. conceded = conceded + away;
  49. difference = scored - conceded;
  50. }// end method addAwayResult()
  51.  
  52. public void print() {
  53. System.out.println("DIV"+"\t"+"CODE"+"\t"+"POINTS"+"\t"+"PLAYED"+"\t"+"SCORED"+"\t"+"CONC"+"\t"+"DIFF");
  54. System.out.println(division+"\t"+code+"\t"+points+"\t"+played+"\t"+scored+"\t"+conceded+"\t"+difference+"\t");
  55.  
  56. } //end method print()
  57.  
  58. }// end class Team
  59.  


How can i sort my data with regards to the total number of points, im having difficulty with that!!

can someone help, thanksssssssss
Dec 2 '08 #1
Share this Question
Share on Google+
4 Replies

Expert 10K+
P: 11,448
@minus
Have a look at the Comparator interface and the Collections.sort() or Arrays.sort() methods. btw, your 's' key seems to be stuck; fix that.

kind regards,

Jos
Dec 2 '08 #2

Nepomuk
Expert 2.5K+
P: 3,112
Actually, the OP's already using that in the class Team - but just look at your compareTo method:
Expand|Select|Wrap|Line Numbers
  1. public int compareTo(Team aTeam){
  2. //if 
  3. return 0;
  4. }//end method compareTo() 
Do you know, what this method should be doing?

Greetings,
Nepomuk
Dec 2 '08 #3

Expert 10K+
P: 11,448
@Nepomuk
No he isn't; he's using the Comparable interface. A Comparator interface is easier when you want to sort on different criteria.

kind regards,

Jos
Dec 2 '08 #4

Nepomuk
Expert 2.5K+
P: 3,112
Ah, sorry, you're right and I'm wrong. I should read more carefully. ^^

Greetings,
Nepomuk
Dec 2 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.