-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCalcularPropina.kt
270 lines (255 loc) · 10.9 KB
/
CalcularPropina.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package com.example.calculadorpropines
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Star
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.calculadorpropines.ui.theme.CalculadorPropinesTheme
class CalcularPropina : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
CalculadorPropinesTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Propina(modifier = Modifier.padding(innerPadding))
}
}
}
}
}
/**
* This function renders an activity including different types of objects
* Allows to the user to calculate the amount of a dinner including the bartender tip
* @author Raimon Izard
* @since 12/10/2024
*/
@OptIn(ExperimentalLayoutApi::class)
@Preview(showBackground = true, showSystemUi = true)
@Composable
fun Propina(modifier: Modifier = Modifier) {
var import: Float by remember { mutableStateOf(0.0f) }
var percentatge: Int by remember { mutableStateOf(0) }
var preuFinal: Float by remember { mutableStateOf(0.0f) }
var propinaCambrers: Float by remember { mutableStateOf(0.0f) }
var stars: Int by remember { mutableStateOf(0) }
var coins: Int by remember { mutableStateOf(0) }
// Definim el contenidor del fons de la imatge
Box(
modifier = Modifier
.fillMaxSize()
) {
Image(
contentScale = ContentScale.FillBounds,
painter = painterResource(id = R.drawable.wooden_restaurant),
contentDescription = "Background restaurant",
modifier = Modifier
.fillMaxSize()
.matchParentSize()
)
}
Box(
modifier = Modifier
.padding(top = 50.dp), contentAlignment = Alignment.Center
) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(20.dp, 40.dp)
.verticalScroll(
rememberScrollState()
)
) {
Text(
"Benvingut/da al calculador de propines!",
modifier = Modifier
.background(Color.White.copy(alpha = 0.7f))
.fillMaxWidth()
.padding(10.dp), color = Color.Black
)
TextField(
modifier = Modifier
.background(Color.Transparent)
.fillMaxWidth()
.padding(10.dp),
value = import.toString(),
onValueChange = { contingutTextField ->
import = contingutTextField.toFloatOrNull() ?: 0.0f
if (import < 0.0f) {
import = 0.0f
}
preuFinal = 0.0f
propinaCambrers = 0.0f
},
label = { Text("Introdueix l'import del sopar...") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
)
TextField(
modifier = Modifier
.background(Color.Transparent)
.fillMaxWidth()
.padding(10.dp),
value = percentatge.toString(),
onValueChange = { contingutTextField ->
percentatge = contingutTextField.toIntOrNull() ?: 0
if (percentatge > 100) percentatge = 100
else if (percentatge < 0) percentatge = 0
preuFinal = 0.0f
propinaCambrers = 0.0f
},
label = { Text("Introdueix el percentatge de propina que vols deixar [0 - 100%]") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number)
)
Row(modifier = Modifier.padding(8.dp)) {
Button(
onClick = { if (percentatge >= 5) percentatge -= 5 },
//modifier = Modifier.padding(horizontal = 8.dp),
colors = ButtonDefaults.buttonColors(/*contentColor = Color.Red*/ containerColor = Color.Red)
) { Text("-5%") }
Button(
onClick = { if (percentatge <= 95) percentatge += 5 },
modifier = Modifier.padding(horizontal = 8.dp),
colors = ButtonDefaults.buttonColors(/*contentColor = Color.Green*/
containerColor = Color.Green
)
) { Text("+5%") }
Row(modifier = Modifier.padding(10.dp)) {
when (percentatge) {
in 1..5 -> stars = 1
in 5..10 -> stars = 2
in 10..15 -> stars = 3
in 15..20 -> stars = 4
in 25..100 -> stars = 5
else -> stars = 0
}
for (i in 1..stars) {
Icon(
imageVector = Icons.Default.Star,
contentDescription = "Icon Example",
tint = Color.Yellow
)
}
}
}
Row {
Button(
onClick = {
import = 0.0f
percentatge = 0
propinaCambrers = 0.0f
preuFinal = 0.0f
},
colors = ButtonDefaults.buttonColors(containerColor = Color.DarkGray),
modifier = Modifier.padding(8.dp)
) { Text("Esborrar dades") }
Button(
onClick = {
preuFinal = import * (1.0f + (percentatge.toFloat() / 100.0f))
propinaCambrers = import * (percentatge.toFloat() / 100.0f)
},
modifier = Modifier.padding(8.dp),
enabled = import > 0.0f && percentatge in 0..100, // Only enable the button if import > 0 :)
colors = ButtonDefaults.buttonColors(containerColor = Color.Blue),
elevation = ButtonDefaults.buttonElevation(defaultElevation = 2.dp)
) { Text("Calcular import") }
}
Column(
modifier = Modifier
.padding(top = 40.dp)
) {
Text(
//text = "El preu final a pagar és: ${preuFinal.toString()} €",
text = "La propina que rebrà el cambrer/a és de: ${
String.format(
"%.2f",
propinaCambrers
)
} €",
modifier = Modifier
.background(Color.White.copy(alpha = 0.7f))
.fillMaxWidth()
.padding(10.dp), color = Color.Black
)
Text(
//text = "El preu final a pagar és: ${preuFinal.toString()} €",
text = "El preu final a pagar és: ${String.format("%.2f", preuFinal)} €",
modifier = Modifier
.background(Color.White.copy(alpha = 0.7f))
.fillMaxWidth()
.padding(10.dp), color = Color.Black
)
}
Row(
modifier = Modifier
.padding(top = 0.dp)
.fillMaxSize(),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.Bottom
) {
when {
propinaCambrers in 0.5f..5.0f -> coins = 1
propinaCambrers in 5.0f..15.0f -> coins = 2
propinaCambrers in 15.0f..30.0f -> coins = 3
propinaCambrers in 30.0f..40.0f -> coins = 4
propinaCambrers in 40.0f..50.0f -> coins = 5
propinaCambrers in 50.0f..60.0f -> coins = 6
propinaCambrers in 60.0f..70.0f -> coins = 7
propinaCambrers in 70.0f..80.0f -> coins = 8
propinaCambrers > 80.0f -> coins = 9
else -> coins = 0
}
FlowRow(
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight() // Adjust height as needed
.padding(horizontal = 16.dp, vertical = 70.dp),
horizontalArrangement = Arrangement.spacedBy(2.dp), // Espai entre monedes
verticalArrangement = Arrangement.spacedBy(2.dp) // Espai entre files
) {
for (i in 1..coins) {
Image(
contentScale = ContentScale.None,
painter = painterResource(id = R.drawable.euro_coin),
contentDescription = "euro coin",
)
}
}
}
}
}
}