Advertisement
Guest User

TreeMap

a guest
Mar 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.22 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Iterator;
  3. import java.util.Map;
  4. import java.util.Scanner;
  5. import java.util.TreeMap;
  6.  
  7. public class Main {
  8.  
  9.     private static final String CHECKIN = "C";
  10.     private static final String ATAQUE = "H";
  11.     private static final String PONTO = "K";
  12.     private static final String ERRO = "E";
  13.     private static final String BLOQUEIO = "B";
  14.     private static final String DEFESA = "D";
  15.     private static final String RELATORIO = "R";
  16.     private static double numeroPartidas = 0;
  17.     private static TreeMap<Integer, Jogador> time = new TreeMap<>();
  18.  
  19.     public static void main(String[] args) {
  20.        
  21.         Scanner sc = new Scanner(System.in);
  22.         String linha;
  23.  
  24.         while (sc.hasNextLine()) {
  25.             linha = sc.nextLine();
  26.             linha = formataComando(linha);
  27.             String splitLinha[] = linha.split(" ");
  28.            
  29.             switch (splitLinha[0]) {
  30.             case CHECKIN:
  31.                 checkin(linha, true);
  32.                 break;
  33.  
  34.             case ATAQUE:
  35.                 ataque(linha);
  36.                 break;
  37.  
  38.             case PONTO:
  39.                 ponto(linha);
  40.                 break;
  41.  
  42.             case ERRO:
  43.                 erro(linha);
  44.                 break;
  45.  
  46.             case BLOQUEIO:
  47.                 bloqueio(linha);
  48.                 break;
  49.  
  50.             case DEFESA:
  51.                 defesa(linha);
  52.                 break;
  53.  
  54.             case RELATORIO:
  55.                 relatorio();
  56.                 break;
  57.  
  58.             default: // add jogadores no CHECKIN
  59.                 checkin(linha, false);
  60.  
  61.             }
  62.  
  63.         }
  64.     }
  65.  
  66.     private static String formataComando(String linha) {
  67.         if (linha.charAt(0) == ' ') {
  68.             linha = linha.substring(1);
  69.         }
  70.         return linha;
  71.     }
  72.  
  73.     private static void ataque(String linha) {
  74.         int numeroJogador = pegaJogador(linha);
  75.         time.get(numeroJogador).ataques++;
  76.     }
  77.  
  78.     private static void relatorio() {
  79.         Jogador timeEstatistica = new Main().new Jogador();
  80.        
  81.         System.out.println("Player  Hit Pct    KPG      BPG      DPG");
  82.         System.out.println("-----------------------------------------");
  83.  
  84.         for (Map.Entry<Integer, Jogador> entry : time.entrySet()) {
  85.             int numJogador = entry.getKey();
  86.             Jogador jogador = entry.getValue();
  87.  
  88.             timeEstatistica.ataques += jogador.ataques;
  89.             timeEstatistica.bloqueios += jogador.bloqueios;
  90.             timeEstatistica.defesas += jogador.defesas;
  91.             timeEstatistica.erros += jogador.erros;
  92.             timeEstatistica.pontos += jogador.pontos;
  93.  
  94.             imprimeEstatisticaJogador(numJogador, jogador);
  95.         }
  96.  
  97.         imprimeEstatisticaTeam(timeEstatistica);
  98.  
  99.         time = new TreeMap(); // reset
  100.         numeroPartidas = 0; // reset
  101.  
  102.     }
  103.  
  104.     private static void imprimeEstatisticaTeam(Jogador timeEstatistica) {
  105.         double hit = (timeEstatistica.pontos - timeEstatistica.erros)
  106.                 / (double) (timeEstatistica.pontos + timeEstatistica.erros + timeEstatistica.ataques);
  107.         double kpg = timeEstatistica.pontos / numeroPartidas;
  108.         double bpg = timeEstatistica.bloqueios / numeroPartidas;
  109.         double dpg = timeEstatistica.defesas / numeroPartidas;
  110.  
  111.         if (hit < 0) {
  112.             System.out.format("%-8s%-5.3f%9.3f%9.3f%9.3f\n\n", "team", hit, kpg, bpg, dpg);
  113.         } else {
  114.             System.out.format("%-8s%c%-5.3f%9.3f%9.3f%9.3f\n\n", "team", '+', hit, kpg, bpg, dpg);
  115.         }
  116.  
  117.     }
  118.  
  119.     private static void imprimeEstatisticaJogador(int numJogador, Jogador jogador) {
  120.         double hit = 0;
  121.  
  122.         if ((jogador.pontos + jogador.erros + jogador.ataques) != 0) {
  123.             hit = (jogador.pontos - jogador.erros) / (double) (jogador.pontos + jogador.erros + jogador.ataques);
  124.         }
  125.  
  126.         double kpg = jogador.pontos / jogador.numeroPartidas;
  127.         double bpg = jogador.bloqueios / jogador.numeroPartidas;
  128.         double dpg = jogador.defesas / jogador.numeroPartidas;
  129.         String strNumeroJogador = toStrNum(numJogador);
  130.  
  131.         if (hit < 0) {
  132.             System.out.format("%-8s%.3f%9.3f%9.3f%9.3f\n", strNumeroJogador, hit, kpg, bpg, dpg);
  133.         } else {
  134.             System.out.format("%-8s%c%.3f%9.3f%9.3f%9.3f\n", strNumeroJogador, '+', hit, kpg, bpg, dpg);
  135.         }
  136.  
  137.     }
  138.  
  139.     private static String toStrNum(int numJogador) {
  140.         if (numJogador < 10)
  141.             return "0" + numJogador;
  142.         return "" + numJogador;
  143.     }
  144.  
  145.     private static void erro(String linha) {
  146.         int numeroJogador = pegaJogador(linha);
  147.         time.get(numeroJogador).erros++;
  148.     }
  149.  
  150.     private static void bloqueio(String linha) {
  151.         int numeroJogador = pegaJogador(linha);
  152.         time.get(numeroJogador).bloqueios++;
  153.     }
  154.  
  155.     private static void defesa(String linha) {
  156.         int numeroJogador = pegaJogador(linha);
  157.         time.get(numeroJogador).defesas++;
  158.     }
  159.  
  160.     private static void ponto(String linha) {
  161.         int numeroJogador = pegaJogador(linha);
  162.         time.get(numeroJogador).pontos++;
  163.     }
  164.  
  165.     private static int pegaJogador(String linha) {
  166.         String aux[] = linha.split(" ");
  167.         return toInteger(aux[1]);
  168.     }
  169.  
  170.     private static void checkin(String linha, boolean incrementaPartidas) {
  171.         int inicio = 0;
  172.         if (incrementaPartidas) {
  173.             numeroPartidas++;
  174.             inicio = 2;
  175.         }
  176.  
  177.         String jogadores[] = linha.split(" ");
  178.         int numeroJogador = 0;
  179.         for (int i = inicio; i < jogadores.length; i++) {
  180.             Jogador novoJogador = new Main().new Jogador();
  181.             numeroJogador = toInteger(jogadores[i]);
  182.  
  183.             if (time.get(numeroJogador) == null)
  184.                 time.put(numeroJogador, novoJogador);
  185.             else
  186.                 time.get(numeroJogador).numeroPartidas++;
  187.         }
  188.  
  189.     }
  190.  
  191.     static int toInteger(String strNumber) {
  192.         return Integer.parseInt(strNumber);
  193.     }
  194.  
  195.     private class Jogador {
  196.         int ataques = 0;
  197.         int pontos = 0;
  198.         int erros = 0;
  199.         int defesas = 0;
  200.         int bloqueios = 0;
  201.         double numeroPartidas = 1;
  202.  
  203.         Jogador() {
  204.         }
  205.     }
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement