3
Hello, I need to compare 2 products, taking into account their weight and values, to know which product is more advantageous to buy.
Ex: Rice 5kg - R$ 10,00 Rice 2kg - R$ 6,00
Thinking that you can compare grams to kg, making the correct conversion. I am beginner in programming for Android and I will be very grateful for the help.
The layout part I already understand, I really need it from Mainactivity. In the image shows more or less how it will look at the end.
I am not able to do this calculation. How do I check if the chosen unit is 'g' or 'kg', 'l', 'ml' and how to do the conversion based calculation
My Activity is like this at the moment, remembering that I’m starting in mobile development and I’m really enjoying it.
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.b);
b.setOnClickListener(new View.OnClickListener(){
public void onClick (View v) {
//unidades
Spinner peso1 = (Spinner) findViewById(R.id.spinner1);
Spinner peso2 = (Spinner) findViewById(R.id.spinner2);
//quantidade em unidades
EditText qtd1 = (EditText) findViewById(R.id.qtd1);
EditText qtd2 = (EditText) findViewById(R.id.qtd2);
//valor do produto
EditText valor1 = (EditText) findViewById(R.id.valor1);
EditText valor2 = (EditText) findViewById(R.id.valor2);
Double resultado1;
Double resultado2;
Double vlr1 = Double.parseDouble(valor1.getText().toString());
Double vlr2 = Double.parseDouble(valor2.getText().toString());
Integer quantidade1 = Integer.parseInt(qtd1.getText().toString());
Integer quantidade2 = Integer.parseInt(qtd2.getText().toString());
//deve fazer as conversão para o cálculo
if (peso1.getSelectedItem().toString().equals("g"){
quantidade1 = quantidade1 * 0.001; //converte pra kg
}
if (peso2.getSelectedItem().toString().equals("g"){
quantidade2 = quantidade2 * 0.001;/ //converte pra kg
}
resultado1 = quantidade1 * vlr1;
resultado2 = quantidade2 * vlr2;
if (quantidade1 > quantidade2 || resultado1 < resultado2) {
Toast.makeText(getApplicationContext(), "Compre o produto tal", Toast.LENGTH_SHORT).show();
}
else if (quantidade1 < quantidade2 || resultado1 > resultado2) {
Toast.makeText(getApplicationContext(), "Compre o produto tal", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "A vantagem dos produtos é a mesma", Toast.LENGTH_SHORT).show();
}
if (quantidade1 == quantidade2){
Toast.makeText(getApplicationContext(), "Mesma quantidade", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Post the code of
Activity
that you have, helps contextualize and facilitates in the construction of the answer.– Wakim
The difficulty is in calculating or obtaining form values?
– bfavaretto
Yes, my difficulty is to understand how this calculation is done in view of the values informed.
– Flávia Amaral
@Fláviaamaral looking like this looks like you made the correct algorithm. Did you have any error? Where exactly is your doubt?
– Math
I am not able to do this calculation. How do I check if the chosen unit is 'g' or 'kg', 'l', 'ml' and how to do the conversion based calculation.
– Flávia Amaral
@Fláviaamaral you’ve done it here:
if(peso1.getSelectedItem().toString().equals("g")){quantidade1 = quantidade1 * 0.001;}
– Math
Already, already yes. It’s there in the codes above. Am I doing it right and even I don’t know? Rs
– Flávia Amaral
@Fláviaamaral I believe that you have solved the problem yes :) except for the fact that there was a missing parentheses before the key, but if that’s what your code wouldn’t even compile. Notice my comment up there
))
after the equals.– Math
Converting L/mL to kg/g would be a little more complicated. You cannot calculate without knowing the density of the product. 1 liter of water weighs 1kg, but 1 liter of something else can weigh more, or less, than that.
– bfavaretto
vc can see the calculation in rotasul.net/tools/comparator.php by the way I always use this page, even offline []’s
– Sergio