Tugas 6 PPB A
Nama: Meylina Tesa Hapsari
NRP: 5025221149
Kelas: PPB A
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
KonversimatauangTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
CurrencyConverter(modifier = Modifier.padding(innerPadding))
}
}
}
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CurrencyConverter(modifier: Modifier = Modifier) {
val exchangeRates = mapOf(
"USD - Dollar AS" to 0.000063,
"EUR - Euro" to 0.000058,
"JPY - Yen Japan" to 9.52,
"GBP - Pound England" to 0.00005,
"AUD - Dollar Australia" to 0.0001,
"CAD - Dollar Canada" to 0.000086,
"CHF - Franc Swiss" to 0.000057,
"SGD - Dollar Singaporean" to 0.000084,
"MYR - Ringgit Malaysia" to 0.0003,
"CNY - Yuan China" to 0.00045,
)
var inputValue by remember { mutableStateOf("") }
var selectedCurrency by remember { mutableStateOf(exchangeRates.keys.first()) }
var result by remember { mutableStateOf("") }
Column(modifier = Modifier
.padding(16.dp)
.fillMaxSize()) {
OutlinedTextField(
value = inputValue,
onValueChange = { inputValue = it },
label = { Text("Input IDR") },
modifier = Modifier.fillMaxWidth(),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
)
Spacer(modifier = Modifier.height(16.dp))
// Dropdown
var expanded by remember { mutableStateOf(false) }
ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = { expanded = it },
) {
OutlinedTextField(
value = selectedCurrency,
onValueChange = {},
label = { Text("Select the destination currency") },
readOnly = true,
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = expanded) },
modifier = Modifier
.fillMaxWidth()
.menuAnchor() // Penting untuk menautkan dropdown dengan field
)
ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
exchangeRates.keys.forEach { currency ->
DropdownMenuItem(
text = { Text(currency) },
onClick = {
selectedCurrency = currency
expanded = false
}
)
}
}
}
Spacer(modifier = Modifier.height(16.dp))
Button(onClick = {
val idr = inputValue.toDoubleOrNull()
val rate = exchangeRates[selectedCurrency] ?: 1.0
result = if (idr != null) {
val converted = idr * rate
"Result: $converted $selectedCurrency"
} else {
"Invalid Input"
}
}, modifier = Modifier.fillMaxWidth()) {
Text("Conversion")
}
Spacer(modifier = Modifier.height(16.dp))
Text(result)
}
}
Komentar
Posting Komentar