<?php
class Klasemen {
private $klub;
private $poin;
public function __construct($klubList) {
$this->klub = $klubList;
}
public function catatPermainan($klubKandang, $klubTandang, $skor) {
if ($golKandang > $golTandang) {
// Klub kandang menang
$this->poin[$klubKandang] += 3;
} elseif ($golKandang < $golTandang) {
// Klub tandang menang
$this->poin[$klubTandang] += 3;
} else {
// Seri
$this->poin[$klubKandang] += 1;
$this->poin[$klubTandang] += 1;
}
}
public function cetakKlasemen() {
// Urutkan berdasarkan poin (desc), lalu alfabetis jika poin sama
return $this->poin;
}
public function ambilPeringkat($nomorPeringkat) {
$klasemen = $this->cetakKlasemen();
return $klubList[$nomorPeringkat - 1] ?? null;
}
}
// Contoh penggunaan
$klasemen = new Klasemen(['Liverpool', 'Chelsea', 'Arsenal']);
$klasemen->catatPermainan('Arsenal', 'Liverpool', '2:1');
$klasemen->catatPermainan('Arsenal', 'Chelsea', '1:1');
$klasemen->catatPermainan('Chelsea', 'Arsenal', '0:3');
$klasemen->catatPermainan('Chelsea', 'Liverpool', '3:2');
$klasemen->catatPermainan('Liverpool', 'Arsenal', '2:2');
$klasemen->catatPermainan('Liverpool', 'Chelsea', '0:0');
print_r($klasemen->cetakKlasemen()); echo $klasemen->ambilPeringkat(2);
PD9waHAKY2xhc3MgS2xhc2VtZW4gewogICAgcHJpdmF0ZSAka2x1YjsKICAgIHByaXZhdGUgJHBvaW47CgogICAgcHVibGljIGZ1bmN0aW9uIF9fY29uc3RydWN0KCRrbHViTGlzdCkgewogICAgICAgICR0aGlzLT5rbHViID0gJGtsdWJMaXN0OwogICAgICAgICR0aGlzLT5wb2luID0gYXJyYXlfZmlsbF9rZXlzKCRrbHViTGlzdCwgMCk7CiAgICB9CgogICAgcHVibGljIGZ1bmN0aW9uIGNhdGF0UGVybWFpbmFuKCRrbHViS2FuZGFuZywgJGtsdWJUYW5kYW5nLCAkc2tvcikgewogICAgICAgIGxpc3QoJGdvbEthbmRhbmcsICRnb2xUYW5kYW5nKSA9IGV4cGxvZGUoJzonLCAk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