Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Да се имплементира класа FootballTable за обработка од податоците за повеќе фудбласки натпревари од една лига и прикажување на табелата на освоени поени според резултатите од натпреварите. Во класата да се имплементираат:
- public void addGame(String homeTeam, String awayTeam, int homeGoals, int awayGoals) - метод за додавање податоци за одигран натпревар помеѓу тимот со име homeTeam (домашен тим) и тимот со име awayTeam (гостински тим), при што homeGoals претставува бројот на постигнати голови од домашниот тим, а awayGoals бројот на постигнати голови од гостинскиот тим.
- public void printTable() - метод за печатење на табелата според одиграните (внесените) натпревари. Во табелата се прикажуваат редниот број на тимот во табелата, името (со 15 места порамнето во лево), бројот на одиграни натпревари, бројот на победи, бројот на нерешени натпревари, бројот на освоени поени (сите броеви се печатат со 5 места порамнети во десно). Бројот на освоени поени се пресметува како број_на_победи x 3 + број_на_нерешени x 1. Тимовите се подредени според бројот на освоени поени во опаѓачки редослед, ако имаат ист број на освоени поени според гол разликата (разлика од постигнатите голови и примените голови) во опаѓачки редослед, а ако имаат иста гол разлика, според името.
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- class Team implements Comparable<Team>{
- private String name;
- private int wins;
- private int losses;
- private int draws;
- private int total_points;
- private int goals_scored;
- private int goals_recieved;
- public Team(String name, int wins, int losses, int draws, int total_points) {
- this.name = name;
- this.wins = wins;
- this.losses = losses;
- this.draws = draws;
- this.total_points = total_points;
- }
- public void win(int goals_scored, int goals_recieved) {
- updateGoals(goals_scored,goals_recieved);
- wins++;
- }
- public String getName() {
- return name;
- }
- public int getWins() {
- return wins;
- }
- public int getLosses() {
- return losses;
- }
- public int getDraws() {
- return draws;
- }
- public int getTotal_points() {
- return total_points;
- }
- public int getGoals_scored() {
- return goals_scored;
- }
- public int getGoals_recieved() {
- return goals_recieved;
- }
- private void updateGoals(int goals_scored, int goals_recieved) {
- this.goals_scored+=goals_scored;
- this.goals_recieved+=goals_recieved;
- }
- public void loss(int goals_scored, int goals_recieved) {
- updateGoals(goals_scored,goals_recieved);
- losses++;
- }
- public void draw(int goals_scored, int goals_recieved) {
- updateGoals(goals_scored,goals_recieved);
- draws++;
- }
- public int points(){
- return ((wins*3) + draws);
- }
- public int goal_diff() {
- return (goals_scored-goals_recieved);
- }
- @Override
- public int compareTo(Team o) {
- int res1 = -Integer.compare(this.points(),o.points());
- if(res1 == 0){
- int res2 = -Integer.compare(this.goal_diff(),o.goal_diff());
- if(res2 == 0){
- return this.name.toLowerCase().compareTo(o.name.toLowerCase());
- }
- return res2;
- }
- return res1;
- }
- @Override
- public String toString() {
- return "Team{" +
- "name='" + name + '\'' +
- ", wins=" + wins +
- ", losses=" + losses +
- ", draws=" + draws +
- ", total_points=" + total_points +
- ", goals_scored=" + goals_scored +
- ", goals_recieved=" + goals_recieved +
- '}';
- }
- public int total_matches() {
- return wins+losses+draws;
- }
- }
- class FootballTable {
- private Map<String,Team> teamByName;
- public FootballTable(){
- teamByName = new HashMap<>();
- }
- public void addGame(String homeTeam, String awayTeam, int homeGoals, int awayGoals){
- Team t1 = new Team(homeTeam,0,0,0,0);
- Team t2 = new Team(awayTeam,0,0,0,0);
- teamByName.putIfAbsent(homeTeam,t1);
- teamByName.putIfAbsent(awayTeam,t2);
- t1 = teamByName.get(t1.getName());
- t2 = teamByName.get(t2.getName());
- if(homeGoals>awayGoals){
- t1.win(homeGoals,awayGoals);
- t2.loss(awayGoals,homeGoals);
- }else if(awayGoals>homeGoals){
- t2.win(awayGoals,homeGoals);
- t1.loss(homeGoals,awayGoals);
- }else {
- t1.draw(homeGoals,awayGoals);
- t2.draw(awayGoals,homeGoals);
- }
- teamByName.put(homeTeam,t1);
- teamByName.put(awayTeam,t2);
- // System.out.println(teamByName.toString());
- }
- public void printTable(){
- List<Team> list = teamByName
- .values()
- .stream()
- .sorted(Team::compareTo)
- .collect(Collectors.toList());
- IntStream.range(0,list.size())
- .forEach(i -> {
- Team getTeam = list.get(i);
- System.out.printf("%2d. %-15s%5d%5d%5d%5d%5d%n",
- (i+1),
- getTeam.getName(),
- getTeam.total_matches(),
- getTeam.getWins(),
- getTeam.getDraws(),
- getTeam.getLosses(),
- getTeam.points());
- });
- }
- }
- public class FootballTableTest {
- public static void main(String[] args) throws IOException {
- FootballTable table = new FootballTable();
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- reader.lines()
- .map(line -> line.split(";"))
- .forEach(parts -> table.addGame(parts[0], parts[1],
- Integer.parseInt(parts[2]),
- Integer.parseInt(parts[3])));
- reader.close();
- System.out.println("=== TABLE ===");
- System.out.printf("%-19s%5s%5s%5s%5s%5s\n", "Team", "P", "W", "D", "L", "PTS");
- table.printTable();
- }
- }
- // Your code here
- Sample input
- Bournemouth;Man Utd;1;5
- Burnley;Swansea;4;3
- Chelsea;West Ham;2;5
- Crystal Palace;West Brom;1;2
- Everton;Tottenham;0;0
- Hull;Leicester;5;0
- Man City;Sunderland;5;1
- Middlesbrough;Stoke;2;2
- Southampton;Watford;3;1
- Leicester;Arsenal;4;1
- Liverpool;Burnley;2;1
- Man Utd;Southampton;0;1
- Stoke;Man City;1;3
- Sunderland;Middlesbrough;0;4
- Swansea;Hull;5;0
- Tottenham;Crystal Palace;2;3
- Watford;Chelsea;2;1
- West Brom;Everton;4;4
- West Ham;Bournemouth;3;2
- Chelsea;Burnley;4;4
- Crystal Palace;Bournemouth;5;1
- Everton;Stoke;4;5
- Hull;Man Utd;5;5
- Leicester;Swansea;4;5
- Man City;West Ham;2;5
- Southampton;Sunderland;5;5
- Tottenham;Liverpool;5;4
- Watford;Arsenal;3;4
- West Brom;Middlesbrough;4;3
- Arsenal;Southampton;0;1
- Bournemouth;West Brom;5;4
- Burnley;Hull;0;4
- Liverpool;Leicester;0;3
- Man Utd;Man City;3;1
- Middlesbrough;Crystal Palace;0;3
- Stoke;Tottenham;3;3
- Sunderland;Everton;2;3
- Swansea;Chelsea;5;3
- West Ham;Watford;0;1
- Chelsea;Liverpool;4;5
- Crystal Palace;Stoke;3;4
- Everton;Middlesbrough;5;5
- Hull;Arsenal;2;1
- Leicester;Burnley;0;4
- Man City;Bournemouth;2;4
- Southampton;Swansea;3;1
- Tottenham;Sunderland;3;0
- Watford;Man Utd;2;0
- West Brom;West Ham;4;2
- Arsenal;Chelsea;1;4
- Bournemouth;Everton;0;0
- Burnley;Watford;0;0
- Liverpool;Hull;4;3
- Man Utd;Leicester;1;0
- Middlesbrough;Tottenham;4;2
- Stoke;West Brom;0;5
- Sunderland;Crystal Palace;2;3
- Swansea;Man City;4;1
- West Ham;Southampton;5;0
- Burnley;Arsenal;3;3
- Everton;Crystal Palace;3;3
- Hull;Chelsea;0;4
- Leicester;Southampton;4;1
- Man Utd;Stoke;0;5
- Sunderland;West Brom;0;5
- Swansea;Liverpool;4;4
- Tottenham;Man City;0;1
- Watford;Bournemouth;1;2
- West Ham;Middlesbrough;5;3
- Arsenal;Swansea;1;1
- Bournemouth;Hull;0;1
- Chelsea;Leicester;4;5
- Crystal Palace;West Ham;5;5
- Liverpool;Man Utd;1;5
- Man City;Everton;1;4
- Middlesbrough;Watford;4;0
- Southampton;Burnley;4;5
- Stoke;Sunderland;3;0
- West Brom;Tottenham;4;2
- Arsenal;Middlesbrough;2;1
- Bournemouth;Tottenham;1;3
- Burnley;Everton;4;3
- Chelsea;Man Utd;5;4
- Hull;Stoke;1;1
- Leicester;Crystal Palace;1;4
- Liverpool;West Brom;3;2
- Man City;Southampton;4;1
- Swansea;Watford;5;2
- West Ham;Sunderland;5;2
- Crystal Palace;Liverpool;3;2
- Everton;West Ham;3;3
- Man Utd;Burnley;1;2
- Middlesbrough;Bournemouth;5;2
- Southampton;Chelsea;5;3
- Stoke;Swansea;0;1
- Sunderland;Arsenal;4;5
- Tottenham;Leicester;3;0
- Watford;Hull;4;4
- Sample output
- === TABLE ===
- Team P W D L PTS
- 1. West Ham 10 6 2 2 20
- 2. Swansea 10 6 2 2 20
- 3. Crystal Palace 10 6 2 2 20
- 4. West Brom 9 6 1 2 19
- 5. Burnley 10 5 3 2 18
- 6. Southampton 10 5 1 4 16
- 7. Stoke 10 4 3 3 15
- 8. Hull 10 4 3 3 15
- 9. Middlesbrough 10 4 2 4 14
- 10. Tottenham 10 4 2 4 14
- 11. Man Utd 10 4 1 5 13
- 12. Liverpool 9 4 1 4 13
- 13. Everton 10 2 6 2 12
- 14. Man City 9 4 0 5 12
- 15. Leicester 10 4 0 6 12
- 16. Arsenal 9 3 2 4 11
- 17. Watford 10 3 2 5 11
- 18. Chelsea 10 3 1 6 10
- 19. Bournemouth 10 3 1 6 10
- 20. Sunderland 10 0 1 9 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement