|
| 1 | +package kyu6.nba_teams; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.List; |
| 5 | +import java.util.stream.Collectors; |
| 6 | + |
| 7 | +public class RankingNbaTeams { |
| 8 | + |
| 9 | + private static final String teams = "Los Angeles Clippers,Dallas Mavericks,New York Knicks,Atlanta Hawks,Indiana Pacers,Memphis Grizzlies," |
| 10 | + + "Los Angeles Lakers,Minnesota Timberwolves,Phoenix Suns,Portland Trail Blazers,New Orleans Pelicans," |
| 11 | + + "Sacramento Kings,Los Angeles Clippers,Houston Rockets,Denver Nuggets,Cleveland Cavaliers,Milwaukee Bucks," |
| 12 | + + "Oklahoma City Thunder,San Antonio Spurs,Boston Celtics,Philadelphia 76ers,Brooklyn Nets,Chicago Bulls," |
| 13 | + + "Detroit Pistons,Utah Jazz,Miami Heat,Charlotte Hornets,Toronto Raptors,Orlando Magic,Washington Wizards," |
| 14 | + + "Golden State Warriors,Dallas Mavericks"; |
| 15 | + private static final String COMMA = ","; |
| 16 | + private static final int THREE = 3; |
| 17 | + private static final String SPACE = " "; |
| 18 | + |
| 19 | + static String nbaCup(String resultSheet, String toFind) { |
| 20 | + if (resultSheet == null || resultSheet.isEmpty() || toFind == null || toFind.isEmpty()) return ""; |
| 21 | + String[] matches = resultSheet.split(COMMA); |
| 22 | + List<String> matchesOfUniqueTeam = Arrays.stream(matches) |
| 23 | + .filter(match -> match.contains(toFind)) |
| 24 | + .collect(Collectors.toList()); |
| 25 | + return sumStats(matchesOfUniqueTeam, toFind); |
| 26 | + } |
| 27 | + |
| 28 | + private static Result getResult(String line) { |
| 29 | + String localTeam = null; |
| 30 | + int localResultTeam = 0; |
| 31 | + String visitorTeam = null; |
| 32 | + int visitorResultTeam = 0; |
| 33 | + final String[] teams = RankingNbaTeams.teams.split(COMMA); |
| 34 | + for (String team : teams) { |
| 35 | + if (line.contains(team)) { |
| 36 | + if (localTeam == null) { |
| 37 | + localTeam = team; |
| 38 | + String number = line.substring(line.indexOf(localTeam) + localTeam.length() + 1).split(SPACE)[0]; |
| 39 | + localResultTeam = toNumeric(number); |
| 40 | + } else if (!team.equals(localTeam)) { |
| 41 | + visitorTeam = team; |
| 42 | + String number = line.substring(line.indexOf(visitorTeam) + visitorTeam.length() + 1).split(SPACE)[0]; |
| 43 | + visitorResultTeam = toNumeric(number); |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + return new Result(localTeam, localResultTeam, visitorTeam, visitorResultTeam); |
| 49 | + } |
| 50 | + |
| 51 | + private static String sumStats(List<String> matchesOfUniqueTeam, String toFind) { |
| 52 | + int winners = 0; |
| 53 | + int draws = 0; |
| 54 | + int losses = 0; |
| 55 | + int scored = 0; |
| 56 | + int conceded = 0; |
| 57 | + int points; |
| 58 | + final List<String> teamsArray = Arrays.asList(RankingNbaTeams.teams.split(",")); |
| 59 | + if (!teamsArray.contains(toFind)) return toFind + ":This team didn't play!"; |
| 60 | + for (String match : matchesOfUniqueTeam) { |
| 61 | + if (match.contains(".")) return "Error(float number):" + match; |
| 62 | + final Result result = getResult(match); |
| 63 | + if (result.winLocal() && result.isLocal(toFind)) { // Soy local y gana local |
| 64 | + winners++; |
| 65 | + scored = scored + result.getLocalResultTeam(); |
| 66 | + conceded = conceded + result.getVisitorResultTeam(); |
| 67 | + |
| 68 | + } else if (result.winVisitor() && result.isLocal(toFind)) { // Soy local y pierde local |
| 69 | + losses++; |
| 70 | + scored = scored + result.getLocalResultTeam(); |
| 71 | + conceded = conceded + result.getVisitorResultTeam(); |
| 72 | + |
| 73 | + } else if (result.winLocal() && result.isVisitor(toFind)) { // Soy visitante y gana visitante |
| 74 | + losses++; |
| 75 | + scored = scored + result.getVisitorResultTeam(); |
| 76 | + conceded = conceded + result.getLocalResultTeam(); |
| 77 | + } else if (result.winVisitor() && result.isVisitor(toFind)) { // Soy visitante y pierde visitante |
| 78 | + winners++; |
| 79 | + scored = scored + result.getVisitorResultTeam(); |
| 80 | + conceded = conceded + result.getLocalResultTeam(); |
| 81 | + |
| 82 | + } else { // EMPATE |
| 83 | + draws++; |
| 84 | + scored = scored + result.getLocalResultTeam(); |
| 85 | + conceded = conceded + result.getLocalResultTeam(); |
| 86 | + |
| 87 | + } |
| 88 | + } |
| 89 | + points = winners * THREE + draws; |
| 90 | + //"Team Name:W=nb of wins;D=nb of draws;L=nb of losses;Scored=nb;Conceded=nb;Points=nb" |
| 91 | + return toFind + ":W=" + winners + ";D=" + draws + ";L=" + losses + ";Scored=" + scored + ";Conceded=" + conceded + ";Points=" + points; |
| 92 | + } |
| 93 | + |
| 94 | + private static int toNumeric(String str) throws NumberFormatException { |
| 95 | + return Integer.parseInt(str); |
| 96 | + } |
| 97 | + |
| 98 | + private static class Result { |
| 99 | + private String localTeam; |
| 100 | + private int localResultTeam; |
| 101 | + private String visitorTeam; |
| 102 | + private int visitorResultTeam; |
| 103 | + |
| 104 | + Result(String localTeam, int localResultTeam, String visitorTeam, int visitorResultTeam) { |
| 105 | + this.localTeam = localTeam; |
| 106 | + this.localResultTeam = localResultTeam; |
| 107 | + this.visitorTeam = visitorTeam; |
| 108 | + this.visitorResultTeam = visitorResultTeam; |
| 109 | + } |
| 110 | + |
| 111 | + boolean isLocal(String team) { |
| 112 | + return localTeam.equals(team); |
| 113 | + } |
| 114 | + |
| 115 | + boolean isVisitor(String team) { |
| 116 | + return visitorTeam.equals(team); |
| 117 | + } |
| 118 | + |
| 119 | + boolean winLocal() { |
| 120 | + return localResultTeam > visitorResultTeam; |
| 121 | + } |
| 122 | + |
| 123 | + boolean winVisitor() { |
| 124 | + return localResultTeam < visitorResultTeam; |
| 125 | + } |
| 126 | + |
| 127 | + int getLocalResultTeam() { |
| 128 | + return localResultTeam; |
| 129 | + } |
| 130 | + |
| 131 | + int getVisitorResultTeam() { |
| 132 | + return visitorResultTeam; |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments