fork download
  1. <?php
  2.  
  3. class Klasemen {
  4. private $poin = [];
  5.  
  6. public function __construct($klubList) {
  7. foreach ($klubList as $klub) {
  8. $this->poin[$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->poin[$klubKandang] += 3;
  19. } elseif ($skorKandang < $skorTandang) {
  20. $this->poin[$klubTandang] += 3;
  21. } else {
  22. $this->poin[$klubKandang] += 1;
  23. $this->poin[$klubTandang] += 1;
  24. }
  25. }
  26.  
  27. public function cetakKlasemen() {
  28. arsort($this->poin);
  29. return $this->poin;
  30. }
  31.  
  32. public function ambilPeringkat($nomorPeringkat) {
  33. $hasilKlasemen = $this->cetakKlasemen();
  34. $namaKlub = array_keys($hasilKlasemen);
  35.  
  36. if ($nomorPeringkat > 0 && $nomorPeringkat <= count($namaKlub)) {
  37. return $namaKlub[$nomorPeringkat - 1];
  38. }
  39.  
  40. return null;
  41. }
  42. }
  43.  
  44. $klasemen = new Klasemen(['Liverpool', 'Chelsea', 'Arsenal']);
  45.  
  46. $klasemen->catatPermainan('Arsenal', 'Liverpool', '2:1');
  47. $klasemen->catatPermainan('Arsenal', 'Chelsea', '1:1');
  48. $klasemen->catatPermainan('Chelsea', 'Arsenal', '0:3');
  49. $klasemen->catatPermainan('Chelsea', 'Liverpool', '3:2');
  50. $klasemen->catatPermainan('Liverpool', 'Arsenal', '2:2');
  51. $klasemen->catatPermainan('Liverpool', 'Chelsea', '0:0');
  52.  
  53. echo "Klasemen:\n";
  54. print_r($klasemen->cetakKlasemen());
  55.  
  56. echo "\nPeringkat ke-2: " . $klasemen->ambilPeringkat(2) . "\n";
  57.  
  58. ?>
Success #stdin #stdout 0.03s 25916KB
stdin
Standard input is empty
stdout
Klasemen:
Array
(
    [Arsenal] => 8
    [Chelsea] => 5
    [Liverpool] => 2
)

Peringkat ke-2: Chelsea