fork download
  1. <?php
  2.  
  3. class Klasemen {
  4. private $klubPoin = [];
  5.  
  6. public function __construct($daftarKlub) {
  7. foreach ($daftarKlub as $klub) {
  8. $this->klubPoin[$klub] = 0;
  9. }
  10. }
  11.  
  12. public function catatPermainan($klubKandang, $klubTandang, $skor) {
  13. list($skorKandang, $skorTandang) = explode(':', $skor);
  14. $skorKandang = (int)$skorKandang;
  15. $skorTandang = (int)$skorTandang;
  16.  
  17. if ($skorKandang > $skorTandang) {
  18. $this->klubPoin[$klubKandang] += 3;
  19. } elseif ($skorKandang < $skorTandang) {
  20. $this->klubPoin[$klubTandang] += 3;
  21. } else {
  22. $this->klubPoin[$klubKandang] += 1;
  23. $this->klubPoin[$klubTandang] += 1;
  24. }
  25. }
  26.  
  27. public function cetakKlasemen() {
  28. arsort($this->klubPoin);
  29. return $this->klubPoin;
  30. }
  31.  
  32. public function ambilPeringkat($nomorPeringkat) {
  33. $klasemen = $this->cetakKlasemen();
  34. $klubArray = array_keys($klasemen);
  35. $index = $nomorPeringkat - 1;
  36.  
  37. return $klubArray[$index] ?? null;
  38. }
  39. }
  40.  
  41. $klasemen = new Klasemen(['Liverpool', 'Chelsea', 'Arsenal']);
  42. $klasemen->catatPermainan('Arsenal', 'Liverpool', '2:1');
  43. $klasemen->catatPermainan('Arsenal', 'Chelsea', '1:1');
  44. $klasemen->catatPermainan('Chelsea', 'Arsenal', '0:3');
  45. $klasemen->catatPermainan('Chelsea', 'Liverpool', '3:2');
  46. $klasemen->catatPermainan('Liverpool', 'Arsenal', '2:2');
  47. $klasemen->catatPermainan('Liverpool', 'Chelsea', '0:0');
  48.  
  49. print_r($klasemen->cetakKlasemen());
  50. print_r($klasemen->ambilPeringkat(2));
  51.  
Success #stdin #stdout 0.02s 25980KB
stdin
Standard input is empty
stdout
Array
(
    [Arsenal] => 8
    [Chelsea] => 5
    [Liverpool] => 2
)
Chelsea