fork download
  1. <?php
  2.  
  3. class Klasemen
  4. {
  5. private $poinKlub = [];
  6.  
  7. public function __construct($daftarKlub)
  8. {
  9. foreach ($daftarKlub as $klub) {
  10. $this->poinKlub[$klub] = 0;
  11. }
  12. }
  13.  
  14. public function catatPermainan($klubKandang, $klubTandang, $skor)
  15. {
  16. if (in_array($klubKandang) && in_array($klubTandang)) {
  17. exit();
  18. }
  19.  
  20. list($skorKandang, $skorTandang) = explode(':', $skor);
  21. $skorKandang = (int)$skorKandang;
  22. $skorTandang = (int)$skorTandang;
  23.  
  24. if ($skorKandang > $skorTandang) {
  25. $this->poinKlub[$klubKandang] += 3;
  26. } elseif ($skorKandang < $skorTandang) {
  27. $this->poinKlub[$klubTandang] += 3;
  28. } else {
  29. $this->poinKlub[$klubKandang] += 1;
  30. $this->poinKlub[$klubTandang] += 1;
  31. }
  32. }
  33.  
  34. public function cetakKlasemen()
  35. {
  36. arsort($this->poinKlub);
  37. return $this->poinKlub;
  38. }
  39.  
  40. public function ambilPeringkat($peringkat)
  41. {
  42. $klasemen = $this->cetakKlasemen();
  43. $klubList = array_keys($klasemen);
  44.  
  45. if ($peringkat < 1 || $peringkat > count($klubList)) {
  46. return null;
  47. }
  48.  
  49. return $klubList[$peringkat - 1];
  50. }
  51. }
  52.  
  53. $klasemen = new Klasemen(['Liverpool', 'Chelsea', 'Arsenal']);
  54.  
  55. $klasemen->catatPermainan('Arsenal', 'Liverpool', '2:1');
  56. $klasemen->catatPermainan('Arsenal', 'Chelsea', '1:1');
  57. $klasemen->catatPermainan('Chelsea', 'Arsenal', '0:3');
  58. $klasemen->catatPermainan('Chelsea', 'Liverpool', '3:2');
  59. $klasemen->catatPermainan('Liverpool', 'Arsenal', '2:2');
  60. $klasemen->catatPermainan('Liverpool', 'Chelsea', '0:0');
  61. $klasemen->catatPermainan('Barcelona', 'Madrid', '0:0');
  62.  
  63.  
  64. print_r($klasemen->cetakKlasemen());
  65.  
  66. echo 'Peringkat ke-2: ' . $klasemen->ambilPeringkat(2);
  67.  
Success #stdin #stdout #stderr 0.04s 25704KB
stdin
Standard input is empty
stdout
Array
(
    [Arsenal] => 8
    [Chelsea] => 5
    [Liverpool] => 2
    [Barcelona] => 1
    [Madrid] => 1
)
Peringkat ke-2: Chelsea
stderr
PHP Warning:  in_array() expects at least 2 parameters, 1 given in /home/19VFvS/prog.php on line 16
PHP Warning:  in_array() expects at least 2 parameters, 1 given in /home/19VFvS/prog.php on line 16
PHP Warning:  in_array() expects at least 2 parameters, 1 given in /home/19VFvS/prog.php on line 16
PHP Warning:  in_array() expects at least 2 parameters, 1 given in /home/19VFvS/prog.php on line 16
PHP Warning:  in_array() expects at least 2 parameters, 1 given in /home/19VFvS/prog.php on line 16
PHP Warning:  in_array() expects at least 2 parameters, 1 given in /home/19VFvS/prog.php on line 16
PHP Warning:  in_array() expects at least 2 parameters, 1 given in /home/19VFvS/prog.php on line 16
PHP Notice:  Undefined index: Barcelona in /home/19VFvS/prog.php on line 29
PHP Notice:  Undefined index: Madrid in /home/19VFvS/prog.php on line 30