For my uni task I have to adapt a piece of code that we are given to create an app that adds up total costs of items and displays them in Java, and i'm very new to mobile development. We were given the code for everything other than the totalling up and displaying the cost. I added a few variables at the top and a `total()` method.

Currently, when the app runs it will grab all the prices from the items and add/subtract them as required, but the amounts it's adding and subtracting by are wrong, (everything adds and subtracts by 1.5 instead of the prices defined when the objects are created) and i'm not sure why. Any ideas?

Let me know if you need anymore info from the other classes.

Thanks

EDIT: What I am expecting to happen with the app is that it will have 4 rows, each contain an image associated with one of the Dessert objects, text to say which dessert it is, and then a button to increment and decrement the quantity. I also want to display a total cost of all of the currently selected desserts, e.g if a quantity of 1 donut was selected and a quantity of 2 cookies were selected, the expected total should display as £3.88. The total should update whenever another dessert is added or subtracted

The code I received had the functionality of the list and the increment and decrement functions, so I was tasked with adding prices to the objects, and to make them total up.

CODE:

Main-Acitvity.java

```
package com.example.annascott.buttondemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

// quantity of desert
    int desertNumber;

    Dessert currentDessert;
    double totalAmount = 0;

    // Create an ArrayList of Dessert objects
    final ArrayList<Dessert> desserts = new ArrayList<Dessert>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //CREATE THE DESSERT OBJECTS
        desserts.add(new Dessert("Donut", 0, R.drawable.doughnut, 1.99));
        desserts.add(new Dessert("Cookie", 0, R.drawable.cookie, 0.99));
        desserts.add(new Dessert("PieceOfCake", 0, R.drawable.piece_of_cake, 2.99));
        desserts.add(new Dessert("Pastry", 0, R.drawable.pastry, 1.50));

        // Create an {@link DessertAdapter}, whose data source is a list of
        // {@link Dessert}s. The adapter knows how to create list item views for each item
        // in the list.
        DesertAdapter flavorAdapter = new DesertAdapter(this, desserts);

        // Get a reference to the ListView, and attach the adapter to the listView.
        ListView listView = (ListView) findViewById(R.id.listview_dessert);
        listView.setAdapter(flavorAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                switch (i) {
                    case 0:
                        Intent donut = new Intent(MainActivity.this, Donut.class);
                        startActivity(donut);
                        break;
                    case 1:
                        Intent cookie = new Intent(MainActivity.this, Cookie.class);
                        startActivity(cookie);
                        break;
                    case 2:
                        Intent pieceOfCake = new Intent(MainActivity.this, PieceOfCake.class);
                        startActivity(pieceOfCake);
                        break;
                    case 3:
                        Intent pastry = new Intent(MainActivity.this, Pastry.class);
                        startActivity(pastry);
                        break;
                }
            }
        });
    }

    public void Decrement(View view) {

        LinearLayout parentRow = (LinearLayout) view.getParent();

        TextView quantityView = (TextView) parentRow.findViewById(R.id.dessert_number);
        String quantityString = quantityView.getText().toString();
        desertNumber = Integer.parseInt(quantityString);
        desertNumber -= 1;

        if (desertNumber < 0) {
            desertNumber = 0;
            Toast.makeText(MainActivity.this, "Can not be less than 0",
                    Toast.LENGTH_SHORT).show();
        }
        quantityView.setText(String.valueOf(desertNumber));

        total(false);
    }

    public void Increment(View view) {

        // Set the dessert amount to the text view
        LinearLayout parentRow = (LinearLayout) view.getParent();

        TextView quantityView = (TextView) parentRow.findViewById(R.id.dessert_number);
        String quantityString = quantityView.getText().toString();
        desertNumber = Integer.parseInt(quantityString);
        desertNumber += 1;
        quantityView.setText(String.valueOf(desertNumber));
        total(true);

    }

    // Add the total cost of the items to be bought
    public void total(boolean change) {

        //get all of the desserts
        for (int i = 0; i < desserts.size(); i++) {
            currentDessert = desserts.get(i);
        }

        // if increment add price else subtract
        TextView shoppingCartView = (TextView) findViewById(R.id.shopping_cart);
        if (change) {
            totalAmount += currentDessert.getMyPrice();
        } else {
            totalAmount -= currentDessert.getMyPrice();
        }

        shoppingCartView.setText(Double.toString(totalAmount));
    }
}

```

Dessert.java

```
package com.example.annascott.buttondemo;

/**
 * {@link Dessert} represents type of desert.
 * Each object has 3 properties: name, number, and image resource ID.
 */

public class Dessert {
    // Name of the desert
    private String mDessertName;

    // Number of desserts
    private int mDessertNumber;

    // Drawable resource ID
    private int mImageResourceId;

    // Price of desert
    private double myPrice;

    /*
     * Create a new dessert object.
     *
     * @param vName is the name of the dessert
     * @param vNumber is the corresponding number of desserts
     * @param image is drawable reference ID that corresponds to the dessert
     * */
    public Dessert(String vName, int vNumber, int imageResourceId, double price)

    {
        mDessertName = vName;
        mDessertNumber = vNumber;
        mImageResourceId = imageResourceId;
        myPrice = price;
    }

    /**
     * Get the name of the dessert
     */
    public String getDessertName() {
        return mDessertName;
    }

    /**
     * Get the  number of desserts
     */
    public int getDessertNumber() {
        return mDessertNumber;
    }

    /**
     * Get the image resource ID
     */
    public int getImageResourceId() {
        return mImageResourceId;
    }

    public double getMyPrice() {

        return myPrice ;
    }
    }

```