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