Desarrolle un programa que almacene el resultado de partidos de futbol. Lo equipos a considerar son:
- Nacional
- Medellin
- Cali
- America
Cada equipo esta integrado por 5 jugadores (todos diferentes). Cada jugador tendrá los siguientes elementos:
- id
- Nombre
- Numero (camiseta)
- Equipo al que pertenece
- goles (default = 0)
Adicionalmente, cada partido de futbol sera identificado mediante un código (int) y podrá determinarse los equipos que jugaron el partido, además del resultado global del mismo y una tabla de goleadores (ordenada) por equipo.
Se deja como ejercicio para el lector, el control en el array Jugadores necesario para garantizar que no se repitan jugadores. Se propone realizar control en la variable numero (camiseta), debido a que pueden existir 2 jugadores con el mismo nombre.
- En la clase Gol, el id se corresponde con el id del partido (Game). Si el id no es igual, el programa genera un error.
- La solución propuesta no es única.
package certification1;
public class Player {
private final int id;
private final String name;
private final int number;
private final Team.team t;
public int goles = 0;
public Player(int id, String name, int number, Team.team t){
this.id = id;
this.name = name;
this.number = number;
this.t = t;
}
public void goal(){
goles++;
}
public String getName(){
return name;
}
public int getNumber(){
return number;
}
}
package certification1;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Team {
public enum team{Nacional, Medellin, Cali, America}
private final int id;
private final String name;
private final ArrayList
public Team(int id, String name, ArrayList
assert(jugadores.size() == 5):"Array size de jugadores diferente de 5.";
this.id = id;
this.name = name;
this.jugadores = jugadores;
}
public ArrayList
Collections.sort(jugadores, new Comparator
@Override
public int compare(Player a, Player b){
if(a.goles > b.goles){
return -1;
}else if(a.goles < b.goles){
return 1;
}else{
return 0;
}
}
});
return jugadores;
}
public String getName() {
return name;
}
public ArrayList
return jugadores;
}
}
package certification1;
public class Gol {
private final int id;
private final Team t;
private final Player p;
public Gol(int id, Team t, Player p){
this.id = id;
this.t = t;
this.p = p;
}
public int getId() {
return id;
}
public Team getT() {
return t;
}
public Player getP() {
return p;
}
}
package certification1;
import java.util.ArrayList;
public class Game {
private final int id;
private final Team t1, t2;
private int gol_t1, gol_t2;
private ArrayList
private void check_code(int id)throws AssertionError{
assert(this.id == id):"No es el mismo partido. Game class error.";
}
private void check_player(Player p, Team t)throws AssertionError{
assert(t.getJugadores().contains(p)):"Jugador no existe. Player arraylist error.";
}
public Game(int id, Team t1, Team t2){
gol_t1 = 0;
gol_t2 = 0;
this.id = id;
this.gols = new ArrayList<>();
this.t1 = t1;
this.t2 = t2;
}
public void get_Result(){
System.out.println(t1.getName() + ": " + gol_t1 + " | " + t2.getName() + ": " + gol_t2);
}
public void addGol(Gol g){
check_code(g.getId());
check_player(g.getP(),g.getT());
if(g.getT().toString().equals(t1.toString())){
gol_t1++;
g.getP().goal();
}else{
gol_t2++;
}
gols.add(g);
}
}
package certification1;
import java.util.ArrayList;
public class Certification1 {
public static void main(String[] args) {
try{
// Jugadores Nacional
Player n1 = new Player(1,"Carlos",1,Team.team.Nacional);
Player n2 = new Player(2,"Higuain",7,Team.team.Nacional);
Player n3 = new Player(3,"Fredy",12,Team.team.Nacional);
Player n4 = new Player(4,"Camilo",10,Team.team.Nacional);
Player n5 = new Player(5,"Juan",11,Team.team.Nacional);
Player n6 = new Player(5,"Andres",13,Team.team.Nacional);
ArrayList
nacional.add(n1);
nacional.add(n2);
nacional.add(n3);
nacional.add(n4);
nacional.add(n5);
// Jugadores America
Player a1 = new Player(1,"Carlos",1,Team.team.America);
Player a2 = new Player(2,"Higuain",7,Team.team.America);
Player a3 = new Player(3,"Fredy",12,Team.team.America);
Player a4 = new Player(4,"Camilo",10,Team.team.America);
Player a5 = new Player(5,"Juan",11,Team.team.America);
ArrayList
america.add(a1);
america.add(a2);
america.add(a3);
america.add(a4);
america.add(a5);
// Formar Equipos
Team t1 = new Team(100,Team.team.Nacional.toString(),nacional);
Team t2 = new Team(101,Team.team.America.toString(),america);
// Formar Juego
Game first_game = new Game(1001,t1,t2);
Gol gol1 = new Gol(1001, t1, n2);
Gol gol2 = new Gol(1001, t1, n3);
Gol gol3 = new Gol(1001, t1, n3);
first_game.addGol(gol1);
first_game.addGol(gol2);
first_game.addGol(gol3);
first_game.get_Result();
System.out.println("");
// Resumen de Goles t1
t1.getRankedPlayers();
System.out.println("Goles en: " + t1.getName().toString());
for(Player x: t1.getJugadores()){
System.out.printf("%-8s %-2s %-2s %-6s \n",x.getName(),": ",x.goles," goles.");
}
}
catch(AssertionError a){
System.out.println(a.getMessage());
}
catch(Exception e){
System.out.println(e.toString());
}
}
}
Para que el programa funcione correctamente, es recomendable activar assertion en la maquina virtual.
No comments:
Post a Comment