I m making app in which person can select bottle size and name. Then user presses buy button and program looks if there are any that type of bottles in the bottle dispenser in case of true, a bottle is bought. Here is the spinner code: (everything is in onCreate except @override -> and so on)
sizeSpinner = (Spinner) findViewById(R.id.spinner);
nameSpinner = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(this, R.array.sizes, android.R.layout.simple_spinner_item);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sizeSpinner.setAdapter(arrayAdapter);
sizeSpinner.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> arrayAdapterName = ArrayAdapter.createFromResource(this, R.array.bottleNames, android.R.layout.simple_spinner_item);
arrayAdapterName.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
nameSpinner.setAdapter(arrayAdapterName);
nameSpinner.setOnItemSelectedListener(this);
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()) {
case R.id.spinner2:
selectedName = parent.getItemAtPosition(position).toString();
break;
case R.id.spinner:
selectedSize = parent.getItemAtPosition(position).toString();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
this is my string.xml :
<resources>
<string name="app_name">kappa8</string>
<string-array name = "sizes">
<item>small</item>
<item>medium</item>
<item>large</item>
</string-array>
<string-array name = "bottleNames">
<item>Pepsi Max</item>
<item>Coca-Cola Zero</item>
<item>Fanta Zero</item>
</string-array>
then the pressed button calls buyBottle function that calls another buyBottle function in bottleDispenser (bd)
public void buyBottle(View v) {
if (bd.buyBottle(selectedSize, selectedName)){
bottleTextRefresh();
displayText.setText("Bought a bottle");
} else {
displayText.setText("Not enough MONEY or no such bottle found");
}
}
buyBottle function in bottle dispenser:
public boolean buyBottle(String size, String name) {
Double sizee = 0.0;
int indexOfBottle = -1;
String trimSize = size.trim();
System.out.println(size + " before if statements");
System.out.println("small" == trimSize);
if (trimSize == "large") {
sizee = 1.5;
System.out.println("This is big bottle");
}
if (trimSize == "medium") {
sizee = 1.0;
}
if (trimSize == "small") {
sizee = 0.5;System.out.println("This is small bottle");
}
System.out.println(sizee + " after if statement");
for (Bottle bottle : bottle_array) {
if ((bottle.getName() == name.toString()) && (bottle.getSize() == sizee)) {
indexOfBottle = bottle_array.indexOf(bottle);
System.out.println("bottle found");
}
}
if (indexOfBottle != -1) {
if (money < bottle_array.get(indexOfBottle).getCost()) {
System.out.println("Add money first!");
return false;
} else {
bottles -= 1;
money -= Math.round(bottle_array.get(indexOfBottle).getCost() * 100.0) / 100.0;
System.out.println("KACHUNK! " + bottle_array.get(indexOfBottle).getName() + " came out of the dispenser!");
bottle_array.remove(indexOfBottle);
return true;
}
} else {
System.out.println("missing bottle");
return false;
}
}
User can only choose between 3 sizes and thats why i have made if statements for each one. Problem happens in bd buyBottle because it doesn't change the sizee (Double) value from initial value. I don't know why but it does not reconize either string or name as normal string or name. For example i have selected small fanta bottle from the spinners but the system out print is this:
I/System.out: small before if statements
I/System.out: false
0.0 after if statement
missing bottle
It even says false when i did the "small" == trimSize part. Why so and mostly how can i fix this? I'm not so sure where the problem is so that's why i posted bunch of code.