-6

Hello :) I have a shop in my application but the problem is, if I am buying button1 (coin doubler) it also buys button2 (extra life) and if I press button6 (return to game) it upgrades something .. I think it is a problem in my brackets or in my IF inside IF inside IF ..

so here is my code:

   package com.FirstAppDevelopment.BlueTAP;

import com.example.whitetap.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Shop extends Activity implements OnClickListener {
    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;
    private Button button6;
    private TextView textView1;
    private int value;
    private SharedPreferences prefs;
    private int lives;
    private int doubler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shoplayout);
        textView1 = (TextView) findViewById(R.id.textView1);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(this);
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(this);
        button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(this);
        button4 = (Button) findViewById(R.id.button4);
        button4.setOnClickListener(this);
        button5 = (Button) findViewById(R.id.button5);
        button5.setOnClickListener(this);
        button6 = (Button) findViewById(R.id.button6);
        button6.setOnClickListener(this);
        prefs = getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
        value = prefs.getInt("key2", 0);
        textView1.setText("You have:" + value + "coins.");
        doubler = prefs.getInt("key3", 0);
        lives = prefs.getInt("key4",0);
        if (lives == 0) {
            button2.setText("Buy extra life for one turn! cost 200 coins :)");
            button2.setBackgroundResource(R.drawable.bluex);
        }
        else if (lives == 1) {
            button2.setText("You have already bought an extra life");
            button2.setBackgroundResource(R.drawable.bluev);
        }
        if (doubler == 0) {
            button1.setText("Get 2 coins for every click! cost 1000 coins :)");
        } else if (doubler == 1) {
            button1.setText("Get 4 coins for every click! cost 2500 coins :)");
            button1.setBackgroundResource(R.drawable.blueb1);
        } else if (doubler == 2) {
            button1.setText("Get 6 coins for every click! cost 5000 coins :)");
            button1.setBackgroundResource(R.drawable.blue2);
        } else if (doubler == 3) {
            button1.setText("You have reached the maximum level of this upgrade :)");
            button1.setBackgroundResource(R.drawable.blueb3);
        }
    }

    public void onClick(View v) {
        textView1.setText("You have:" + value + "coins.");
        int id = v.getId();
        if (id == R.id.button6) {
            Context context = getApplicationContext();
            CharSequence text = "You went back to the game";
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            Intent i = new Intent(this, FirstActivity.class);
            startActivity(i);
        }

        else if (id == R.id.button1) {
            if (doubler == 0) {
                button1.setText("Get 2 coins for every click! cost 1000 coins :)");

                if (value > 1000) {
                    value = value - 1000;
                    Context context = getApplicationContext();
                    CharSequence text = "You bought coin doubler";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                    Editor edit = prefs.edit();
                    edit.putInt("key2", value - 1000);
                    edit.putInt("key3", 1);
                    edit.commit();
                    textView1.setText("You have:" + value + "coins.");
                } else if (value < 1000) {
                    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                            case DialogInterface.BUTTON_POSITIVE:
                                // ADD THE BUY FUNCTION
                                break;

                            case DialogInterface.BUTTON_NEGATIVE:
                                DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        switch (which) {
                                        case DialogInterface.BUTTON_POSITIVE:
                                            // ADD THE BUY FUNCTION
                                            break;

                                        case DialogInterface.BUTTON_NEGATIVE:
                                            break;
                                        }
                                    }
                                };
                                AlertDialog.Builder builder = new AlertDialog.Builder(
                                        Shop.this);
                                builder.setMessage(
                                        "Are you sure? Remember, You will buy coins to you and beer to me :)")
                                        .setPositiveButton("Well, fine.",
                                                dialogClickListener)
                                        .setNegativeButton("Sorry, no :(",
                                                dialogClickListener).show();
                                break;
                            }
                        }
                    };

                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage(
                            "You don't have enough money, do you want to buy more coins?")
                            .setPositiveButton("Yes", dialogClickListener)
                            .setNegativeButton("No", dialogClickListener)
                            .show();
                }
            }
        }
        if (doubler == 2) {
            button1.setText("Get 6 coins for every click! cost 5000 coins :)");
            button1.setBackgroundResource(R.drawable.blue2);
            if (value > 5000) {
                value = value - 5000;
                textView1.setText("You have:" + value + "coins.");

                Context context = getApplicationContext();
                CharSequence text = "You upgraded the coin doubler";
                int duration = Toast.LENGTH_SHORT;
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
                Editor edit = prefs.edit();
                edit.putInt("key2", value - 5000);
                edit.putInt("key3", 3);
                edit.commit();
                textView1.setText("You have:" + value + "coins.");
                button1.setText("You have reached the maximum level of this upgrade :)");

            } else if (value < 5000) {
                textView1.setText("You have:" + value + "coins.");

                DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                        case DialogInterface.BUTTON_POSITIVE:
                            // ADD THE BUY FUNCTION
                            break;

                        case DialogInterface.BUTTON_NEGATIVE:
                            DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    switch (which) {
                                    case DialogInterface.BUTTON_POSITIVE:
                                        // ADD THE BUY FUNCTION
                                        break;

                                    case DialogInterface.BUTTON_NEGATIVE:
                                        break;
                                    }
                                }
                            };
                            AlertDialog.Builder builder = new AlertDialog.Builder(
                                    Shop.this);
                            builder.setMessage(
                                    "Are you sure? Remember, You will buy coins to you and beer to me :)")
                                    .setPositiveButton("Well, fine.",
                                            dialogClickListener)
                                    .setNegativeButton("Sorry, no :(",
                                            dialogClickListener).show();
                            break;
                        }
                    }
                };
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage(
                        "You don't have enough money, do you want to buy more coins?")
                        .setPositiveButton("Yes", dialogClickListener)
                        .setNegativeButton("No", dialogClickListener).show();
            }
        }
        if (doubler == 3) {
            Context context = getApplicationContext();
            CharSequence text = "You have reached the maximum level of this upgrade :)";
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            button1.setBackgroundResource(R.drawable.blueb3);
        }
        if (doubler == 1) {
            button1.setBackgroundResource(R.drawable.blueb1);
            button1.setText("Get 4 coins for every click! cost 2500 coins :)");
            if (value > 2500) {
                value = value - 2500;
                textView1.setText("You have:" + value + "coins.");

                Context context = getApplicationContext();
                CharSequence text = "You upgraded the coin doubler";
                int duration = Toast.LENGTH_SHORT;
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
                Editor edit = prefs.edit();
                edit.putInt("key2", value - 2500);
                edit.putInt("key3", 2);
                edit.commit();
                textView1.setText("You have:" + value + "coins.");
                button1.setText("Get 6 coins for every click! cost 5000 coins :)");

            } else if (value < 2500) {
                textView1.setText("You have:" + value + "coins.");

                DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                        case DialogInterface.BUTTON_POSITIVE:
                            // ADD THE BUY FUNCTION
                            break;

                        case DialogInterface.BUTTON_NEGATIVE:
                            DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    switch (which) {
                                    case DialogInterface.BUTTON_POSITIVE:
                                        // ADD THE BUY FUNCTION
                                        break;

                                    case DialogInterface.BUTTON_NEGATIVE:
                                        break;
                                    }
                                }
                            };
                            AlertDialog.Builder builder = new AlertDialog.Builder(
                                    Shop.this);
                            builder.setMessage(
                                    "Are you sure? Remember, You will buy coins to you and beer to me :)")
                                    .setPositiveButton("Well, fine.",
                                            dialogClickListener)
                                    .setNegativeButton("Sorry, no :(",
                                            dialogClickListener).show();
                            break;
                        }
                    }
                };
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage(
                        "You don't have enough money, do you want to buy more coins?")
                        .setPositiveButton("Yes", dialogClickListener)
                        .setNegativeButton("No", dialogClickListener).show();
            }

        }

        // EXTRALIFE!!!!!!!!!!!!!!!!!!!!!!!!!!
        // ---------------------------------
        // EXTRALIFE!!!!!!!!!!!!!!!!!!!!!!!!!!
        // ---------------------------------
        // EXTRALIFE!!!!!!!!!!!!!!!!!!!!!!!!!!
        // ---------------------------------
        // EXTRALIFE!!!!!!!!!!!!!!!!!!!!!!!!!!
        // ---------------------------------
        else if (id == R.id.button2) {
            lives = prefs.getInt("key4", 0);

            if (lives == 0) {
                if (value > 200) {
                    value = value - 200;
                    textView1.setText("You have:" + value + "coins.");

                    Context context = getApplicationContext();
                    CharSequence text = "You bought an extra life";
                    int duration = Toast.LENGTH_SHORT;
                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();
                    Editor edit = prefs.edit();
                    edit.putInt("key2", value - 200);
                    edit.putInt("key4", 1);
                    edit.commit();
                }
                if (value < 200) {
                    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                            case DialogInterface.BUTTON_POSITIVE:
                                // ADD THE BUY FUNCTION
                                break;

                            case DialogInterface.BUTTON_NEGATIVE:
                                DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        switch (which) {
                                        case DialogInterface.BUTTON_POSITIVE:
                                            // ADD THE BUY FUNCTION
                                            break;

                                        case DialogInterface.BUTTON_NEGATIVE:
                                            break;
                                        }
                                    }
                                };
                                AlertDialog.Builder builder = new AlertDialog.Builder(
                                        Shop.this);
                                builder.setMessage(
                                        "Are you sure? Remember, You will buy coins to you and beer to me :)")
                                        .setPositiveButton("Well, fine.",
                                                dialogClickListener)
                                        .setNegativeButton("Sorry, no :(",
                                                dialogClickListener).show();
                                break;
                            }
                        }
                    };

                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage(
                            "You don't have enough money, do you want to buy more coins?")
                            .setPositiveButton("Yes", dialogClickListener)
                            .setNegativeButton("No", dialogClickListener)
                            .show();
                }
            }
        }
        if (lives == 1) {
            Context context = getApplicationContext();
            CharSequence text = "You have already bought an extra life";
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }
    }
}

Can someone find a problem here? :) ty.

The YES/NO dialogs will be to open in app purchases .

user3821158
  • 71
  • 1
  • 5

1 Answers1

0

After stripping out everything besides the control statements, this is what we're left with:

public void onClick(View v) 
{
    if (id == R.id.button6) 
    {
        //snip
    }
    else if (id == R.id.button1) 
    {
        if (doubler == 0) 
        {
            if (value > 1000) 
            {
                //snip
            } 
            else if (value < 1000) 
            {
                //snip
            }
        }
    }
    if (doubler == 2) 
    {
        if (value > 5000) 
        {
            //snip
        } 
        else if (value < 5000)
        {
            //snip
        }
    }
    if (doubler == 3) 
    {
        //snip
    }
    if (doubler == 1)
    {
        if (value > 2500) 
        {
            //snip
        } 
        else if (value < 2500) 
        {
            //snip
        }

    }
    else if (id == R.id.button2) 
    {
        if (lives == 0) 
        {
            if (value > 200) 
            {
                //snip
            }
            if (value < 200) 
            {
                //snip
            }
        }
    }
    if (lives == 1) 
    {
        //snip
    }
}

You're correct about having misplaced closing braces. Move if (doubler == 2) and if (doubler == 3) to be inside of else if (id == R.id.button1) and if (lives == 1) inside of else if (id == R.id.button2) and everything should work as intended.

Also, I would highly recommend using >= instead of just > in your value checks and moving your dialog code into a separate function instead of having duplicate code all over the place.

jcmanke
  • 56
  • 1
  • 4