import java.util.ArrayList;
import java.util.List;
class Klasemen {
private List<Klub> daftarKlub;
private static class Klub {
int poin;
public Klub
(String namaKlub
) { this.namaKlub = namaKlub;
this.poin = 0;
}
}
public Klasemen
(String[] klubList
) { daftarKlub = new ArrayList<>();
for (String klub
: klubList
) { daftarKlub.add(new Klub(klub));
}
}
private Klub findKlub
(String nama
) { for (Klub klub : daftarKlub) {
if (klub.namaKlub.equals(nama)) {
return klub;
}
}
}
Klub klub1 = findKlub(klubKandang);
Klub klub2 = findKlub(klubTandang);
String[] splitSkor
= skor.
split(":"); int skor1
= Integer.
parseInt(splitSkor
[0]); int skor2
= Integer.
parseInt(splitSkor
[1]); if (skor1 > skor2) {
klub1.poin += 3;
} else if (skor1 < skor2) {
klub2.poin += 3;
} else {
klub1.poin += 1;
klub2.poin += 1;
}
}
public List<Klub> cetakKlasemen() {
List<Klub> sortedKlub = new ArrayList<>(daftarKlub);
sortedKlub.sort((k1, k2) -> {
if (k2.poin != k1.poin) {
return k2.poin - k1.poin;
}
return k1.namaKlub.compareTo(k2.namaKlub);
});
return sortedKlub;
}
public String ambilPeringkat
(int nomorPeringkat
) { List<Klub> sortedKlub = cetakKlasemen();
if (nomorPeringkat <= 0 || nomorPeringkat > sortedKlub.size()) {
}
return sortedKlub.get(nomorPeringkat - 1).namaKlub;
}
public static void main
(String[] args
) { Klasemen klasemen
= new Klasemen
(new String[] { "Liverpool",
"Chelsea",
"Arsenal" });
// Mencatat hasil permainan
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");
// Cetak klasemen
List<Klub> listKlasemen = klasemen.cetakKlasemen();
for (Klub klub : listKlasemen) {
System.
out.
print(klub.
namaKlub + "=>" + klub.
poin + ","); }
System.
out.
println("Peringkat 2: " + klasemen.
ambilPeringkat(2)); }
}
aW1wb3J0IGphdmEudXRpbC5BcnJheUxpc3Q7CmltcG9ydCBqYXZhLnV0aWwuTGlzdDsKCmNsYXNzIEtsYXNlbWVuIHsKICAgIHByaXZhdGUgTGlzdDxLbHViPiBkYWZ0YXJLbHViOwoKICAgIHByaXZhdGUgc3RhdGljIGNsYXNzIEtsdWIgewogICAgICAgIFN0cmluZyBuYW1hS2x1YjsKICAgICAgICBpbnQgcG9pbjsKCiAgICAgICAgcHVibGljIEtsdWIoU3RyaW5nIG5hbWFLbHViKSB7CiAgICAgICAgICAgIHRoaXMubmFtYUtsdWIgPSBuYW1hS2x1YjsKICAgICAgICAgICAgdGhpcy5wb2luID0gMDsKICAgICAgICB9CiAgICB9CgogICAgcHVibGljIEtsYXNlbWVuKFN0cmluZ1tdIGtsdWJMaXN0KSB7CiAgICAgICAgZGFmdGFyS2x1YiA9IG5ldyBBcnJheUxpc3Q8PigpOwogICAgICAgIGZvciAoU3RyaW5nIGtsdWIgOiBrbHViTGlzdCkgewogICAgICAgICAgICBkYWZ0YXJLbHViLmFkZChuZXcgS2x1YihrbHViKSk7CiAgICAgICAgfQogICAgfQoKICAgIHByaXZhdGUgS2x1YiBmaW5kS2x1YihTdHJpbmcgbmFtYSkgewogICAgICAgIGZvciAoS2x1YiBrbHViIDogZGFmdGFyS2x1YikgewogICAgICAgICAgICBpZiAoa2x1Yi5uYW1hS2x1Yi5lcXVhbHMobmFtYSkpIHsKICAgICAgICAgICAgICAgIHJldHVybiBrbHViOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHRocm93IG5ldyBFcnJvcigpOwogICAgfQoKICAgIHB1YmxpYyB2b2lkIGNhdGF0UGVybWFpbmFuKFN0cmluZyBrbHViS2FuZGFuZywgU3RyaW5nIGtsdWJUYW5kYW5nLCBTdHJpbmcgc2tvcikgewogICAgICAgIEtsdWIga2x1YjEgPSBmaW5kS2x1YihrbHViS2FuZGFuZyk7CiAgICAgICAgS2x1YiBrbHViMiA9IGZpbmRLbHViKGtsdWJUYW5kYW5nKTsKCiAgICAgICAgU3RyaW5nW10gc3BsaXRTa29yID0gc2tvci5zcGxpdCgiOiIpOwogICAgICAgIGludCBza29yMSA9IEludGVnZXIucGFyc2VJbnQoc3BsaXRTa29yWzBdKTsKICAgICAgICBpbnQgc2tvcjIgPSBJbnRlZ2VyLnBhcnNlSW50KHNwbGl0U2tvclsxXSk7CiAgICAgICAgaWYgKHNrb3IxID4gc2tvcjIpIHsKICAgICAgICAgICAga2x1YjEucG9pbiArPSAzOwogICAgICAgIH0gZWxzZSBpZiAoc2tvcjEgPCBza29yMikgewogICAgICAgICAgICBrbHViMi5wb2luICs9IDM7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAga2x1YjEucG9pbiArPSAxOwogICAgICAgICAgICBrbHViMi5wb2luICs9IDE7CiAgICAgICAgfQoKICAgIH0KCiAgICBwdWJsaWMgTGlzdDxLbHViPiBjZXRha0tsYXNlbWVuKCkgewogICAgICAgIExpc3Q8S2x1Yj4gc29ydGVkS2x1YiA9IG5ldyBBcnJheUxpc3Q8PihkYWZ0YXJLbHViKTsKICAgICAgICBzb3J0ZWRLbHViLnNvcnQoKGsxLCBrMikgLT4gewogICAgICAgICAgICBpZiAoazIucG9pbiAhPSBrMS5wb2luKSB7CiAgICAgICAgICAgICAgICByZXR1cm4gazIucG9pbiAtIGsxLnBvaW47CiAgICAgICAgICAgIH0KICAgICAgICAgICAgcmV0dXJuIGsxLm5hbWFLbHViLmNvbXBhcmVUbyhrMi5uYW1hS2x1Yik7CiAgICAgICAgfSk7CiAgICAgICAgcmV0dXJuIHNvcnRlZEtsdWI7CiAgICB9CgogICAgcHVibGljIFN0cmluZyBhbWJpbFBlcmluZ2thdChpbnQgbm9tb3JQZXJpbmdrYXQpIHsKICAgICAgICBMaXN0PEtsdWI+IHNvcnRlZEtsdWIgPSBjZXRha0tsYXNlbWVuKCk7CiAgICAgIC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