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. list($skorKandang, $skorTandang) = explode(':', $skor);
  17. $skorKandang = (int)$skorKandang;
  18. $skorTandang = (int)$skorTandang;
  19.  
  20. if ($skorKandang > $skorTandang) {
  21. $this->poinKlub[$klubKandang] += 3;
  22. } elseif ($skorKandang < $skorTandang) {
  23. $this->poinKlub[$klubTandang] += 3;
  24. } else {
  25. $this->poinKlub[$klubKandang] += 1;
  26. $this->poinKlub[$klubTandang] += 1;
  27. }
  28. }
  29.  
  30. public function cetakKlasemen()
  31. {
  32. arsort($this->poinKlub);
  33. return $this->poinKlub;
  34. }
  35.  
  36. public function ambilPeringkat($peringkat)
  37. {
  38. $klasemen = $this->cetakKlasemen();
  39. $klubList = array_keys($klasemen);
  40.  
  41. if ($peringkat < 1 || $peringkat > count($klubList)) {
  42. return null;
  43. }
  44.  
  45. return $klubList[$peringkat - 1];
  46. }
  47. }
  48.  
  49. $klasemen = new Klasemen(['Liverpool', 'Chelsea', 'Arsenal']);
  50.  
  51. $klasemen->catatPermainan('Arsenal', 'Liverpool', '2:1');
  52. $klasemen->catatPermainan('Arsenal', 'Chelsea', '1:1');
  53. $klasemen->catatPermainan('Chelsea', 'Arsenal', '0:3');
  54. $klasemen->catatPermainan('Chelsea', 'Liverpool', '3:2');
  55. $klasemen->catatPermainan('Liverpool', 'Arsenal', '2:2');
  56. $klasemen->catatPermainan('Liverpool', 'Chelsea', '0:0');
  57. $klasemen->catatPermainan('Madrid', 'Barcelona', '10:10');
  58.  
  59. print_r($klasemen->cetakKlasemen());
  60.  
  61. echo 'Peringkat ke-2: ' . $klasemen->ambilPeringkat(6);
  62.  
Success #stdin #stdout #stderr 0.03s 26016KB
stdin
Standard input is empty
stdout
Array
(
    [Arsenal] => 8
    [Chelsea] => 5
    [Liverpool] => 2
    [Madrid] => 1
    [Barcelona] => 1
)
Peringkat ke-2: 
stderr
PHP Notice:  Undefined index: Madrid in /home/AafdOE/prog.php on line 25
PHP Notice:  Undefined index: Barcelona in /home/AafdOE/prog.php on line 26