Created
October 14, 2019 16:12
-
-
Save adeds/85ec4bb08dde9cf2012bcac9e5d82edb to your computer and use it in GitHub Desktop.
WP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<TextView | |
android:id="@+id/text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Hello World!" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Alternatif(var kode: String, var nama: String) : Comparable<Alternatif> { | |
var nilai = 0.0 | |
override fun toString(): String { | |
return String.format("%s-%s:%.3f", kode, nama, nilai) | |
} | |
override fun compareTo(a: Alternatif): Int { | |
return java.lang.Double.compare(this.nilai, a.nilai) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const val SANGAT_TIDAK_PENTING = 1.0 | |
const val TIDAK_PENTING = 2.0 | |
const val CUKUP_PENTING = 3.0 | |
const val PENTING = 4.0 | |
const val SANGAT_PENTING = 5.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Kriteria( | |
var kode: String, | |
var nama: String, | |
var benefit: Boolean, | |
var bobot: Double | |
) { | |
fun weighted(total: Double): Double { | |
val w = bobot / total | |
return if (benefit) w else w * -1 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class KriteriaAlternatif( | |
var alternatifKode: String, | |
var kriteriaKode: String, | |
var nilai: Double | |
) { | |
var temp: Double = 0.toDouble() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
private lateinit var alternatif: MutableList<Alternatif> | |
private lateinit var kriteria: MutableList<Kriteria> | |
private lateinit var kriteriaAlternatif: MutableList<KriteriaAlternatif> | |
var s = "" | |
@RequiresApi(Build.VERSION_CODES.N) | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
initList() | |
val hasil = hitung() | |
val highest = Collections.max(hasil).kode | |
hasil.forEach { | |
s += if (it.kode.equals(highest)) | |
"${it.nama} adalah yang tertinggi \n" | |
else "${it.nama}\n" | |
} | |
text.text = s | |
} | |
private fun initList() { | |
alternatif = mutableListOf() | |
kriteria = mutableListOf() | |
kriteriaAlternatif = mutableListOf() | |
alternatif = Arrays.asList( | |
Alternatif("A1", "Samsung Galaxy J5"), | |
Alternatif("A2", "Xiaomi Redmi 4X"), | |
Alternatif("A3", "Oppo A57") | |
) | |
kriteria = Arrays.asList( | |
Kriteria("C1", "Harga", false, CUKUP_PENTING), | |
Kriteria("C2", "Kapasitas RAM/ROM", true, SANGAT_PENTING), | |
Kriteria("C3", "Kualitas Kamera", true, CUKUP_PENTING), | |
Kriteria("C4", "Kapasitas Baterai", true, PENTING) | |
) | |
kriteriaAlternatif = Arrays.asList( | |
KriteriaAlternatif("A1", "C1", 82.0), | |
KriteriaAlternatif("A1", "C2", 76.0), | |
KriteriaAlternatif("A1", "C3", 85.0), | |
KriteriaAlternatif("A1", "C4", 76.0), | |
KriteriaAlternatif("A2", "C1", 75.0), | |
KriteriaAlternatif("A2", "C2", 80.0), | |
KriteriaAlternatif("A2", "C3", 75.0), | |
KriteriaAlternatif("A2", "C4", 82.0), | |
KriteriaAlternatif("A3", "C1", 80.0), | |
KriteriaAlternatif("A3", "C2", 76.0), | |
KriteriaAlternatif("A3", "C3", 79.0), | |
KriteriaAlternatif("A3", "C4", 75.0) | |
) | |
} | |
@RequiresApi(Build.VERSION_CODES.N) | |
fun pangkatW(ka: KriteriaAlternatif): KriteriaAlternatif { | |
// Total nilai bobot untuk perhitungan perbaikan bobot (∑Wj) | |
val totalW = kriteria.stream() | |
.mapToDouble { (_, _, _, bobot) -> bobot } | |
.sum() | |
// Perhitungan nilai kriteria berpangkat bobot perbaikan | |
kriteria.stream() | |
.filter { (kode) -> ka.kriteriaKode == kode } | |
.forEach { k -> ka.temp = Math.pow(ka.nilai, k.weighted(totalW)) } | |
return ka | |
} | |
@RequiresApi(Build.VERSION_CODES.N) | |
fun vecktorS(a: Alternatif): Alternatif { | |
// Perhitungan nilai vektor S | |
kriteriaAlternatif.stream() | |
.map { pangkatW(it) } | |
.filter { (alternatifKode) -> a.kode == alternatifKode } | |
.forEach { ka -> a.nilai = if (a.nilai == 0.0) ka.temp else a.nilai * ka.temp } | |
return a | |
} | |
@RequiresApi(Build.VERSION_CODES.N) | |
fun hitung(): List<Alternatif> { | |
// Total niali S (∑Sj) | |
val totalS = alternatif.stream() | |
.map { vecktorS(it) } | |
.mapToDouble { a -> a.nilai } | |
.sum() | |
// Nilai V (Sj / ∑Sj) | |
alternatif.forEach { a -> a.nilai = a.nilai / totalS } | |
return alternatif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment