Created
December 13, 2023 08:22
-
-
Save MartiVilas/36270af414f34a7e7fed008e27ca2434 to your computer and use it in GitHub Desktop.
This file contains 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
# Exemple d'exercici de kotlin | |
## Enunciat: | |
Et dediques a vendre figures Otaku comprades a Tokio per | |
wallapop. Si estan en bon estat les pots revendre per un +25%, si | |
no, per +10%. Implementa l’algoritme. | |
## Solució usant programació modular en kotlin | |
``` kotlin | |
fun readFloat(pMessage: String | |
, pErrorMsg: String | |
, pMin: Float | |
, pMax: Float) : Float{ | |
val read : Scanner = Scanner(System.`in`) | |
var floatValue : Float = 0.0f // Guardar valor float | |
var correctValue : Boolean = false // Semàfor tipus de dades | |
do{ | |
print(pMessage) | |
correctValue = read.hasNextFloat() | |
if(!correctValue){ // correctValue = false | |
println("ERROR: $pErrorMsg !") | |
}else{ // correctValue = true | |
floatValue = read.nextFloat() | |
if (floatValue < pMin || floatValue > pMax){ | |
println("ERROR: Int value out of bounds") | |
correctValue = false | |
} | |
} | |
read.nextLine() | |
}while(!correctValue) | |
return floatValue | |
} | |
fun readBoolean(pMessage: String | |
, pErrorMsg: String | |
) : Boolean{ | |
val read : Scanner = Scanner(System.`in`) | |
var booleanValue : Boolean = false // Guardar valor booleà | |
var correctValue : Boolean = false // Semàfor tipus de dades | |
do{ | |
print(pMessage) | |
correctValue = read.hasNextBoolean() | |
if(!correctValue){ // correctValue = false | |
println("ERROR: $pErrorMsg !") | |
}else{ // correctValue = true | |
booleanValue = read.nextBoolean() | |
} | |
read.nextLine() | |
}while(!correctValue) | |
return booleanValue | |
} | |
fun calculRevendaFigura(pCostInicial : Float | |
, pEstat : Boolean) : Float{ | |
var importFinal : Float = 0.0f | |
if(pEstat){ | |
importFinal = pCostInicial * 1.25f | |
}else{ | |
importFinal = pCostInicial * 1.10f | |
} | |
return importFinal | |
} | |
fun propietatsFigura() : Float{ | |
var preuRevenda = 0.0f | |
var importCompra : Float = | |
readFloat("Diga'm quan et va costar la figura originalment: " | |
, "Import no numèric" | |
, 0.0f | |
, 100000.0f) | |
var bonEstat : Boolean = | |
readBoolean("Diga'm si la figura estava en bon estat o no..." | |
, "El valor introduït no és booleà") | |
preuRevenda = calculRevendaFigura(importCompra, bonEstat) | |
return preuRevenda | |
} | |
fun main(){ | |
println("Hola benvingudes al wallapop d'Otaku...") | |
var importRevenda : Float = propietatsFigura() | |
print("Podràs revendre la figura per $importRevenda") | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment