Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.87 KB | None | 0 0
  1. public class Mannschaft {
  2.  
  3.     private String name;
  4.     private int tore;
  5.     private int gegentore;
  6.     private int siege;
  7.     private int niederlagen;
  8.     private int unentschieden;
  9.  
  10.     public Mannschaft(String name){
  11.         this.name = name;
  12.         this.tore = 0;
  13.         this.gegentore = 0;
  14.         this.siege = 0;
  15.         this.niederlagen = 0;
  16.         this.unentschieden = 0;
  17.     }
  18.  
  19.     public String getName(){
  20.         return this.name;
  21.     }
  22.     public int getTore(){
  23.         return this.tore;
  24.     }
  25.     public int getGegentore(){
  26.         return this.gegentore;
  27.     }
  28.     public int getPunkte(){
  29.         return siege * 3 + unentschieden;
  30.     }
  31.     public int getSpiele(){
  32.         return siege + niederlagen + unentschieden;
  33.     }
  34.     public int getTorDifferenz(){
  35.         return tore - gegentore;
  36.     }
  37.  
  38.     public void eintragenErgebnis(int tore, int gegentore){
  39.         if (tore > gegentore){
  40.             this.siege++;
  41.         }else if (tore == gegentore){
  42.             this.unentschieden++;
  43.         }else {
  44.             this.niederlagen++;
  45.         }
  46.         this.tore += tore;
  47.         this.gegentore += gegentore;
  48.     }
  49.     public void print(){
  50.         System.out.println(this.name + "\tTD:" +
  51.                 this.getTorDifferenz() + "\tPKT:" +
  52.                 this.getPunkte() + "\t\tS:"+
  53.                 this.siege + "\t\tU:" +
  54.                 this.unentschieden+ "\t\tN:" +
  55.                 this.niederlagen );
  56.     }
  57. }
  58. // Klasse Tabelle
  59. package alexander_treise_bundesliga;
  60.  
  61. public class Tabelle {
  62.  
  63.     private static String[] namen = {"Schalke 04", "Bayer 04", "FC Bayern", "Werder", "HSV", "Dortmund", "Hoffenheim", "Mainz 05", "Wolfsburg", "Eintracht",
  64.             "M'gladbach", "Freiburg", "Hannover", "1.FC Köln", "Stuttgart", "Bochum", "Nürnberg", "Hertha BSC"};
  65.     private static final int LIGA_GROESSE = namen.length;
  66.     private Mannschaft[] team;
  67.     private Mannschaft[] tabelle = new Mannschaft[namen.length];
  68.  
  69.     public Tabelle(){
  70.         team = new Mannschaft[namen.length];
  71.         for (int i = 0; i <namen.length ; i++) {
  72.             team[i] = new Mannschaft(namen[i]);
  73.         }
  74.     }
  75.  
  76.     public boolean spielen(String heim, String gast, int heimTore, int gastTore){
  77.         boolean rc = false;
  78.         int indexH;
  79.         for(indexH = 0; indexH < namen.length; indexH++){
  80.             if (heim.equalsIgnoreCase(team[indexH].getName())){
  81.                 break;
  82.             }
  83.         }
  84.         int indexG;
  85.         for (indexG = 0; indexG < namen.length;indexG++){
  86.             if (gast.equalsIgnoreCase(team[indexG].getName())){
  87.                 break;
  88.             }
  89.         }
  90.         if (indexH<LIGA_GROESSE && indexG<LIGA_GROESSE && indexH != indexG){
  91.             team[indexH].eintragenErgebnis(heimTore,gastTore);
  92.             team[indexG].eintragenErgebnis(gastTore,heimTore);
  93.             rc = true;
  94.         }
  95.         return rc;
  96.     }
  97.  
  98.  
  99.     public void print(){
  100.         Mannschaft[] teamTemp = new Mannschaft[namen.length];
  101.         for (int i = 0; i <team.length ; i++) {
  102.             for (int j = 1; j <team.length-i ; j++) {
  103.                 if (team[j].getPunkte()>team[j-1].getPunkte()){
  104.                     teamTemp[i]=team[j-1];
  105.                     team[j-1]=team[j];
  106.                     team[j]=teamTemp[i];
  107.                 }
  108.             }
  109.         }
  110.         System.out.println("R. \tName \t\tTorDi. \tPunkte \t\tSiege \tUnents. Niederl.");
  111.         for (int i = 0; i < namen.length;i++) {
  112.             System.out.print(i + "\t");
  113.             team[i].print();
  114.         }
  115.     }
  116.  
  117.  
  118.  
  119.  
  120.     public static void main(String[] args) {
  121.         Tabelle bundesliga = new Tabelle();
  122.         bundesliga.spielen("FC Bayern","Werder",3,1);
  123.         bundesliga.spielen("Werder","FC Bayern",1,3);
  124.         bundesliga.spielen("Schalke 04","HSV",1,1);
  125.         bundesliga.spielen("HSV","Schalke 04",1,1);
  126.         bundesliga.spielen("Hannover","Hoffenheim",1,2);
  127.         bundesliga.spielen("Hoffenheim","Hannover",2,1);
  128.         bundesliga.spielen("Eintracht","Mainz 05",4,2);
  129.         bundesliga.spielen("Mainz 05","Eintracht",0,2);
  130.         bundesliga.print();
  131.  
  132.  
  133.     }
  134.  
  135. }
  136.  
  137. Ausgabe:
  138.  
  139. R.  Name        TorDi.  Punkte      Siege   Unents. Niederl.
  140. 0   FC Bayern   TD:4    PKT:6       S:2     U:0     N:0
  141. 1   Hoffenheim  TD:2    PKT:6       S:2     U:0     N:0
  142. 2   Eintracht   TD:4    PKT:6       S:2     U:0     N:0
  143. 3   Schalke 04  TD:0    PKT:2       S:0     U:2     N:0
  144. 4   HSV TD:0    PKT:2       S:0     U:2     N:0
  145. 5   Bayer 04    TD:0    PKT:0       S:0     U:0     N:0
  146. 6   Werder  TD:-4   PKT:0       S:0     U:0     N:2
  147. 7   Dortmund    TD:0    PKT:0       S:0     U:0     N:0
  148. 8   Mainz 05    TD:-4   PKT:0       S:0     U:0     N:2
  149. 9   Wolfsburg   TD:0    PKT:0       S:0     U:0     N:0
  150. 10  M'gladbach  TD:0    PKT:0       S:0     U:0     N:0
  151. 11  Freiburg    TD:0    PKT:0       S:0     U:0     N:0
  152. 12  Hannover    TD:-2   PKT:0       S:0     U:0     N:2
  153. 13  1.FC Köln  TD:0    PKT:0       S:0     U:0     N:0
  154. 14  Stuttgart   TD:0    PKT:0       S:0     U:0     N:0
  155. 15  Bochum  TD:0    PKT:0       S:0     U:0     N:0
  156. 16  Nürnberg   TD:0    PKT:0       S:0     U:0     N:0
  157. 17  Hertha BSC  TD:0    PKT:0       S:0     U:0     N:0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement