Advertisement
Guest User

gb2

a guest
May 28th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1. package np;
  2.  
  3. import java.util.*;
  4.  
  5. public class GoldenBallTest {
  6.   public static void main(String[] args) {
  7.     Scanner scanner = new Scanner(System.in);
  8.     GoldenBall goldenBall = new GoldenBall();
  9.     while (scanner.hasNextLine()) {
  10.       String line = scanner.nextLine();
  11.       if (line == null || line.isEmpty()) break;
  12.       String[] players = line.split(";");
  13.       goldenBall.addVote(players);
  14.     }
  15.     scanner.close();
  16.     System.out.println("===== TOP 10 ======");
  17.     goldenBall.listTopN(10);
  18.     System.out.println("===== MAX 1 PLACE =====");
  19.     goldenBall.maxPlace(1);
  20.     System.out.println("===== MAX 2 PLACE =====");
  21.     goldenBall.maxPlace(2);
  22.     System.out.println("===== MAX 3 PLACE =====");
  23.     goldenBall.maxPlace(3);
  24.     System.out.println("===== COUNT =====");
  25.     goldenBall.count();
  26.   }
  27. }
  28.  
  29. class Player
  30. {
  31.     String name;
  32.     int poeni;
  33.     int c1;
  34.     int c2;
  35.     int c3;
  36.     static int max = 0;
  37.    
  38.     public Player(String name)
  39.     {
  40.         this.name = name;
  41.         poeni = 0;
  42.         c1 = 0;
  43.         c2 = 0;
  44.         c3 = 0;
  45.     }
  46.     public double procent()
  47.     {
  48.        
  49.         return poeni / (max * 5);
  50.     }
  51.    
  52.     public void prvo()
  53.     {
  54.         poeni += 5;
  55.         c1++;
  56.     }
  57.     public void vtoro()
  58.     {
  59.         poeni += 3;
  60.         c2++;
  61.     }
  62.     public void treto()
  63.     {
  64.         poeni += 1;
  65.         c3++;
  66.     }
  67. }
  68.  
  69. class GoldenBall
  70. {
  71.     Map<String , Player> nominees;
  72.     public GoldenBall()
  73.     {
  74.         this.nominees = new TreeMap<>();
  75.     }
  76.     public void addVote(String[] players) {
  77.          if(!nominees.containsKey(players[0]))
  78.          {
  79.              Player n = new Player(players[0]);
  80.              n.prvo();
  81.              nominees.put(players[0] , n );
  82.          }
  83.          else
  84.          {
  85.              Player n = nominees.get(players[0]);
  86.              n.prvo();
  87.          }
  88.        
  89.          if(!nominees.containsKey(players[1]))
  90.          {
  91.              Player n = new Player(players[1]);
  92.              n.vtoro();
  93.              nominees.put(players[1] , n );
  94.          }
  95.          else
  96.          {
  97.              Player n = nominees.get(players[1]);
  98.              n.vtoro();
  99.          }
  100.          
  101.          if(!nominees.containsKey(players[2]))
  102.          {
  103.              Player n = new Player(players[2]);
  104.              n.treto();
  105.              nominees.put(players[2] , n );
  106.          }
  107.          else
  108.          {
  109.              Player n = nominees.get(players[2]);
  110.              n.treto();
  111.          }
  112.          Player.max++;
  113.     }
  114.  
  115.     public void listTopN(int n) {
  116.         List<Player> toplist = new ArrayList<>();
  117.         for(String s : nominees.keySet())
  118.         {
  119.             toplist.add(nominees.get(s));
  120.         }
  121.         int i = 0;
  122.         Collections.sort(toplist , new CustomComp());
  123.         Iterator<Player> it = toplist.iterator();
  124.         while(it.hasNext() && i < n )
  125.         {
  126.             Player p = it.next();
  127.             System.out.println(String.format("%d . %s %d %.3f %" , (i+1) , p.name , p.poeni , p.procent()));
  128.         }
  129.        
  130.        
  131.     }
  132.  
  133.     public void maxPlace(int i) {
  134.         if(i == 1)
  135.         {
  136.             int max = 0;
  137.             String tmp = "";
  138.            
  139.             for(String s : nominees.keySet())
  140.             {
  141.                 if(nominees.get(s).c1 >= max)
  142.                 {
  143.                     max = nominees.get(s).c1;
  144.                     tmp = s;
  145.                 }
  146.             }
  147.             System.out.println(String.format("%s ", tmp));
  148.            
  149.         }
  150.         else if(i == 2)
  151.         {
  152.             int max = 0;
  153.             String tmp = "";
  154.            
  155.             for(String s : nominees.keySet())
  156.             {
  157.                 if(nominees.get(s).c2 >= max)
  158.                 {
  159.                     max = nominees.get(s).c1;
  160.                     tmp = s;
  161.                 }
  162.             }
  163.             System.out.println(String.format("%s ", tmp));
  164.         }
  165.         else if (i == 3)
  166.         {
  167.             int max = 0;
  168.             String tmp = "";
  169.            
  170.             for(String s : nominees.keySet())
  171.             {
  172.                 if(nominees.get(s).c3 >= max)
  173.                 {
  174.                     max = nominees.get(s).c1;
  175.                     tmp = s;
  176.                 }
  177.             }
  178.             System.out.println(String.format("%s ", tmp));
  179.         }
  180.        
  181.     }
  182.     public void count() {
  183.        
  184.        
  185.     }
  186. }
  187.  
  188. class CustomComp implements Comparator<Player>
  189. {
  190.  
  191.     @Override
  192.     public int compare(Player p1, Player p2) {
  193.         int n1 = p1.poeni;
  194.         int n2 = p2.poeni;
  195.         if(n1 == n2) return p1.name.compareTo(p2.name);
  196.         else return Integer.compare(n1, n2);
  197.     }
  198.    
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement