<?php
class Klasemen
{
private $klubPoin;
public function __construct
(array $klub) {
}
public function catatPermainan(string $klubKandang, string $klubTandang, string $skor): void
{
[$skorKandang, $skorTandang] = explode(':', $skor); // Pecah string
// Ubah string to int
$skorKandang = (int) $skorKandang;
$skorTandang = (int) $skorTandang;
// Cek kondisi skor
if ($skorKandang > $skorTandang) {
$this->klubPoin[$klubKandang] += 3;
} elseif ($skorKandang < $skorTandang) {
$this->klubPoin[$klubTandang] += 3;
} else {
$this->klubPoin[$klubKandang] += 1;
$this->klubPoin[$klubTandang] += 1;
}
}
public function cetakKlasemen
(): array {
arsort($this->klubPoin); // Sorting return $this->klubPoin;
}
public function ambilPeringkat(int $nomorPeringkat): string
{
return $klasemen[$nomorPeringkat - 1] ?? 'Peringkat ke-' . $nomorPeringkat . 'tidak tersedia';
}
}
//Eksekusi
$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);
PD9waHAKCmNsYXNzIEtsYXNlbWVuCnsKICAgIHByaXZhdGUgJGtsdWJQb2luOwoKICAgIHB1YmxpYyBmdW5jdGlvbiBfX2NvbnN0cnVjdChhcnJheSAka2x1YikKICAgIHsKICAgICAgICAkdGhpcy0+a2x1YlBvaW4gPSBhcnJheV9maWxsX2tleXMoJGtsdWIsIDApOwogICAgfQoKICAgIHB1YmxpYyBmdW5jdGlvbiBjYXRhdFBlcm1haW5hbihzdHJpbmcgJGtsdWJLYW5kYW5nLCBzdHJpbmcgJGtsdWJUYW5kYW5nLCBzdHJpbmcgJHNrb3IpOiB2b2lkCiAgICB7CiAgICAgICAgWyRza29yS2FuZGFuZywgJHNrb3JUYW5kYW5nXSA9IGV4cGxvZGUoJzonLCAkc2tvcik7IC8vIFBlY2FoIHN0cmluZwoKICAgICAgICAvLyBVYmFoIHN0cmluZyB0byBpbnQKICAgICAgICAkc2tvckthbmRhbmcgPSAoaW50KSAkc2tvckthbmRhbmc7CiAgICAgICAgJHNrb3JUYW5kYW5nID0gKGludCkgJHNrb3JUYW5kYW5nOwoKICAgICAgICAvLyBDZWsga29uZGlzaSBza29yCiAgICAgICAgaWYgKCRza29yS2FuZGFuZyA+ICRza29yVGFuZGFuZykgewogICAgICAgICAgICAkdGhpcy0+a2x1YlBvaW5bJGtsdWJLYW5kYW5nXSArPSAzOwogICAgICAgIH0gZWxzZWlmICgkc2tvckthbmRhbmcgPCAkc2tvclRhbmRhbmcpIHsKICAgICAgICAgICAgJHRoaXMtPmtsdWJQb2luWyRrbHViVGFuZGFuZ10gKz0gMzsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAkdGhpcy0+a2x1YlBvaW5bJGtsdWJLYW5kYW5nXSArPSAxOwogICAgICAgICAgICAkdGhpcy0+a2x1YlBvaW5bJGtsdWJUYW5kYW5nXSArPSAxOwogICAgICAgIH0KICAgIH0KCiAgICBwdWJsaWMgZnVuY3Rpb24gY2V0YWtLbGFzZW1lbigpOiBhcnJheQogICAgewogICAgICAgIGFyc29ydCgkdGhpcy0+a2x1YlBvaW4pOyAvLyBTb3J0aW5nCiAgICAgICAgcmV0dXJuICR0aGlzLT5rbHViUG9pbjsKICAgIH0KCiAgICBwdWJsaWMgZnVuY3Rpb24gYW1iaWxQZXJpbmdrYXQoaW50ICRub21vclBlcmluZ2thdCk6IHN0cmluZwogICAgewogICAgICAgICRrbGFzZW1lbiA9IGFycmF5X2tleXMoJHRoaXMtPmNldGFrS2xhc2VtZW4oKSk7CiAgICAgICAgcmV0dXJuICRrbGFzZW1lblskbm9tb3JQZXJpbmdrYXQgLSAxXSA/PyAnUG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