Advertisement
Guest User

Vetor version

a guest
Mar 27th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.19 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 Jogador[] time = new Jogador[55];
  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[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 (int numJogador = 0; numJogador < 55; numJogador++) {
  85.             if(time[numJogador] != null){
  86.                 timeEstatistica.ataques += time[numJogador].ataques;
  87.                 timeEstatistica.bloqueios += time[numJogador].bloqueios;
  88.                 timeEstatistica.defesas += time[numJogador].defesas;
  89.                 timeEstatistica.erros += time[numJogador].erros;
  90.                 timeEstatistica.pontos += time[numJogador].pontos;
  91.    
  92.                 imprimeEstatisticaJogador(numJogador, time[numJogador]);
  93.             }
  94.         }
  95.  
  96.         imprimeEstatisticaTeam(timeEstatistica);
  97.  
  98.         time = new Jogador[55]; // reset
  99.         numeroPartidas = 0; // reset
  100.  
  101.     }
  102.  
  103.     private static void imprimeEstatisticaTeam(Jogador timeEstatistica) {
  104.         double hit = (timeEstatistica.pontos - timeEstatistica.erros)
  105.                 / (double) (timeEstatistica.pontos + timeEstatistica.erros + timeEstatistica.ataques);
  106.         double kpg = timeEstatistica.pontos / numeroPartidas;
  107.         double bpg = timeEstatistica.bloqueios / numeroPartidas;
  108.         double dpg = timeEstatistica.defesas / numeroPartidas;
  109.  
  110.         if (hit < 0) {
  111.             System.out.format("%-8s%-5.3f%9.3f%9.3f%9.3f\n\n", "team", hit, kpg, bpg, dpg);
  112.         } else {
  113.             System.out.format("%-8s%c%-5.3f%9.3f%9.3f%9.3f\n\n", "team", '+', hit, kpg, bpg, dpg);
  114.         }
  115.  
  116.     }
  117.  
  118.     private static void imprimeEstatisticaJogador(int numJogador, Jogador jogador) {
  119.         double hit = 0;
  120.  
  121.         if ((jogador.pontos + jogador.erros + jogador.ataques) != 0) {
  122.             hit = (jogador.pontos - jogador.erros) / (double) (jogador.pontos + jogador.erros + jogador.ataques);
  123.         }
  124.  
  125.         double kpg = jogador.pontos / jogador.numeroPartidas;
  126.         double bpg = jogador.bloqueios / jogador.numeroPartidas;
  127.         double dpg = jogador.defesas / jogador.numeroPartidas;
  128.         String strNumeroJogador = toStrNum(numJogador);
  129.  
  130.         if (hit < 0) {
  131.             System.out.format("%-8s%.3f%9.3f%9.3f%9.3f\n", strNumeroJogador, hit, kpg, bpg, dpg);
  132.         } else {
  133.             System.out.format("%-8s%c%.3f%9.3f%9.3f%9.3f\n", strNumeroJogador, '+', hit, kpg, bpg, dpg);
  134.         }
  135.  
  136.     }
  137.  
  138.     private static String toStrNum(int numJogador) {
  139.         if (numJogador < 10)
  140.             return "0" + numJogador;
  141.         return "" + numJogador;
  142.     }
  143.  
  144.     private static void erro(String linha) {
  145.         int numeroJogador = pegaJogador(linha);
  146.         time[numeroJogador].erros++;
  147.     }
  148.  
  149.     private static void bloqueio(String linha) {
  150.         int numeroJogador = pegaJogador(linha);
  151.         time[numeroJogador].bloqueios++;
  152.     }
  153.  
  154.     private static void defesa(String linha) {
  155.         int numeroJogador = pegaJogador(linha);
  156.         time[numeroJogador].defesas++;
  157.     }
  158.  
  159.     private static void ponto(String linha) {
  160.         int numeroJogador = pegaJogador(linha);
  161.         time[numeroJogador].pontos++;
  162.     }
  163.  
  164.     private static int pegaJogador(String linha) {
  165.         String aux[] = linha.split(" ");
  166.         return toInteger(aux[1]);
  167.     }
  168.  
  169.     private static void checkin(String linha, boolean incrementaPartidas) {
  170.         int inicio = 0;
  171.         if (incrementaPartidas) {
  172.             numeroPartidas++;
  173.             inicio = 2;
  174.         }
  175.  
  176.         String jogadores[] = linha.split(" ");
  177.         int numeroJogador = 0;
  178.         for (int i = inicio; i < jogadores.length; i++) {
  179.             Jogador novoJogador = new Main().new Jogador();
  180.             numeroJogador = toInteger(jogadores[i]);
  181.  
  182.             if (time[numeroJogador] == null)
  183.                 time[numeroJogador] = novoJogador;
  184.             else
  185.                 time[numeroJogador].numeroPartidas++;
  186.         }
  187.  
  188.     }
  189.  
  190.     static int toInteger(String strNumber) {
  191.         return Integer.parseInt(strNumber);
  192.     }
  193.  
  194.     private class Jogador {
  195.         int ataques = 0;
  196.         int pontos = 0;
  197.         int erros = 0;
  198.         int defesas = 0;
  199.         int bloqueios = 0;
  200.         double numeroPartidas = 1;
  201.  
  202.         Jogador() {
  203.         }
  204.     }
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement