Expand|Select|Wrap|Line Numbers
- import java.util.Arrays;
- import java.util.Collections;
- public class Test {
- public static void main (String [] arg) {
- // "012345678910" alignment guide
- String [] results = { "AR 4 BR 5",
- "CR 0 DR 1",
- "AR 10 CR 9",
- "BR 3 DR 1",
- "AR 0 DR 2",
- "BR 2 AR 2",
- "BR 0 CR 5",
- "CR 1 BR 3",
- "CR 4 AR 2",
- "DR 1 AR 1",
- "DR 2 BR 1",
- "DR 0 CR 2",
- "ES 2 FO 2",
- "GR 0 HE 0",
- "FO 1 GR 2",
- "HE 3 ES 5",
- "ES 4 GR 3",
- "ES 0 HE 2",
- "GR 1 FO 3",
- "GR 2 ES 1",
- "FO 2 HE 1",
- "FO 3 ES 3",
- "HE 1 FO 0",
- "HE 2 GR 1"};
- League myLeague = new League();
- myLeague.setupDiv1();
- myLeague.setupDiv2();
- for(String r: results){
- myLeague.addResult(r);
- }// end for each r
- myLeague.print();
- Arrays.sort(results);
- myLeague.print();
- //myLeague.testFindTeam("DR");
- } //end method main ()
- }// end class Test
Expand|Select|Wrap|Line Numbers
- import java.util.*;
- import java.util.Arrays;
- import java.util.Vector;
- import java.util.Collections;
- public class League {
- Vector<Team> teams;
- String [] list1 = {"AR Arad", "BR Braila", "CR Craiova", "DR Drobeta"};
- String [] list2 = {"ES Escandov", "FO Foscani", "GR Gronstadt", "HE Hermansburg"};
- public League() {
- teams = new Vector<Team>();
- }// end constructor League()
- public void setupDiv1 () {
- for (int i=0; i<list1.length; i++) {
- String code = list1[i].substring(0,2);
- String name = list1[i].substring(3);
- addTeam(1, code, name);
- } //end for i
- }// end method setupDiv1 ()
- public void setupDiv2 () {
- for (int i=0; i<list2.length; i++) {
- String code = list2[i].substring(0,2);
- String name = list2[i].substring(3);
- addTeam(2, code, name);
- } //end for i
- }// end method setupDiv2 ()
- public void addTeam (int division, String code, String name) {
- Team aTeam = new Team(division, code, name);
- teams.add(aTeam);
- }// end method addTeam()
- public Team findTeam(String code) {
- Team aTeam = null;
- for (int i=0; i<teams.size(); i++) {
- aTeam = teams.get(i);
- if (aTeam.code.equals (code))break;
- }// end for i
- return aTeam;
- }//end method findTeam()
- public void testFindTeam(String code) {
- Team aTeam = findTeam(code);
- System.out.println("Found Team = "+aTeam.name);
- }//end method findTeam()
- public void addResult(String result){
- String hCode = "AR";
- String aCode = "BR";
- int hScore = 4;
- int aScore = 5;
- // "01234567890" alignment guide
- //result = "AR 4 BR 5"
- hCode = result.substring(0,2).trim();
- aCode = result.substring(6,8).trim();
- String hS = result.substring(3,5).trim();
- String aS = result.substring(9).trim();
- hScore = Integer.parseInt(hS);
- aScore = Integer.parseInt(aS);
- Team homeTeam = this.findTeam(hCode);
- homeTeam.addHomeResult(hScore,aScore);
- Team awayTeam = this.findTeam(aCode);
- awayTeam.addAwayResult(hScore,aScore);
- }//end method addResult()
- public void print() {
- for (Team aTeam: teams) {
- aTeam.print();
- }// end forall teams
- }// end method print()
- }// end class League
Expand|Select|Wrap|Line Numbers
- import java.util.*;
- import java.util.Collections;
- import java.util.Comparator;
- public class Team implements Comparable <Team> {
- int division;
- String name;
- String code;
- int points;
- int played;
- int scored;
- int conceded;
- int difference;
- public int compareTo(Team aTeam){
- //if
- return 0;
- }//end method compareTo()
- public Team(int division, String code, String name) {
- this.division = division;
- this.code = code;
- this.name = name;
- }// end constructor Team(code, name)
- public void addResult(int home, int away) {
- // home and away are goals score by home and away teams in a match
- }// end method addResult()
- public void addHomeResult(int home, int away) {
- if (home==away) points = points + 1; // Draw
- if (home > away) points = points + 2; // Home Win
- //if (home < away) points = points + 3; // Away Win
- played++;
- scored = scored + home;
- conceded = conceded + away;
- difference = scored - conceded;
- }// end method addHomeResult()
- public void addAwayResult(int home, int away) {
- if (home==away) points = points + 1; // Draw
- //if (home > away) points = points + 2; // Home Win
- if (home < away) points = points + 3; // Away Win
- played++;
- scored = scored + home;
- conceded = conceded + away;
- difference = scored - conceded;
- }// end method addAwayResult()
- public void print() {
- System.out.println("DIV"+"\t"+"CODE"+"\t"+"POINTS"+"\t"+"PLAYED"+"\t"+"SCORED"+"\t"+"CONC"+"\t"+"DIFF");
- System.out.println(division+"\t"+code+"\t"+points+"\t"+played+"\t"+scored+"\t"+conceded+"\t"+difference+"\t");
- } //end method print()
- }// end class Team
How can i sort my data with regards to the total number of points, im having difficulty with that!!
can someone help, thanksssssssss