Tugas 6
Tugas 6 - Currency Converter
Ahmad Fauzan Alghifari - 5025211091
Problem Statement
Membuat aplikasi konversi mata uang dengan android studio. Disini saya membuat aplikasi konverter dengan empat mata uang yakni IDR, USD, JPY, dan SGD.
Kode Sumber : Github
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
package com.example.currencyconverter | |
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.compose.foundation.layout.* | |
import androidx.compose.material3.* | |
import androidx.compose.runtime.* | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.unit.dp | |
class MainActivity : ComponentActivity() { | |
private val ratesToIDR = mapOf( | |
"IDR" to 1.0, | |
"USD" to 16858.0, | |
"JPY" to 118.58, | |
"SGD" to 12867.10 | |
) | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
CurrencyConverter() | |
} | |
} | |
@Composable | |
fun CurrencyConverter() { | |
var amountInput by remember { mutableStateOf("") } | |
var fromCurrency by remember { mutableStateOf("IDR") } | |
var toCurrency by remember { mutableStateOf("USD") } | |
var result by remember { mutableStateOf("") } | |
val currencies = listOf("IDR", "USD", "JPY", "SGD") | |
Column(modifier = Modifier.padding(16.dp)) { | |
OutlinedTextField( | |
value = amountInput, | |
onValueChange = { amountInput = it }, | |
label = { Text("Amount") }, | |
modifier = Modifier.fillMaxWidth() | |
) | |
Spacer(modifier = Modifier.height(8.dp)) | |
DropdownMenuBox( | |
label = "From", | |
options = currencies, | |
selected = fromCurrency, | |
onSelectedChange = { fromCurrency = it } | |
) | |
Spacer(modifier = Modifier.height(8.dp)) | |
DropdownMenuBox( | |
label = "To", | |
options = currencies, | |
selected = toCurrency, | |
onSelectedChange = { toCurrency = it } | |
) | |
Spacer(modifier = Modifier.height(16.dp)) | |
Button(onClick = { | |
val amount = amountInput.toDoubleOrNull() | |
if (amount != null) { | |
val resultAmount = amount * (ratesToIDR[fromCurrency] ?: 1.0) / (ratesToIDR[toCurrency] ?: 1.0) | |
result = "%.2f $toCurrency".format(resultAmount) | |
} else { | |
result = "Invalid input" | |
} | |
}) { | |
Text("Convert") | |
} | |
Spacer(modifier = Modifier.height(16.dp)) | |
Text(text = result) | |
} | |
} | |
@Composable | |
fun DropdownMenuBox( | |
label: String, | |
options: List<String>, | |
selected: String, | |
onSelectedChange: (String) -> Unit | |
) { | |
var expanded by remember { mutableStateOf(false) } | |
Column { | |
Text(text = label) | |
Box { | |
OutlinedButton(onClick = { expanded = true }) { | |
Text(selected) | |
} | |
DropdownMenu( | |
expanded = expanded, | |
onDismissRequest = { expanded = false } | |
) { | |
options.forEach { option -> | |
DropdownMenuItem( | |
text = { Text(option) }, | |
onClick = { | |
onSelectedChange(option) | |
expanded = false | |
} | |
) | |
} | |
} | |
} | |
} | |
} | |
} |
Video Presentasi
Comments
Post a Comment