package com.example.BTC_Break_Even_Calculator;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.lang.Math;
import java.text.DecimalFormat;
import java.lang.String;

public class MyActivity extends Activity
{
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final EditText editFirst = (EditText) findViewById(R.id.editFirst);
        final EditText editSecond = (EditText) findViewById(R.id.editSecond);
        final EditText editThird = (EditText) findViewById(R.id.editThird);
        final EditText editFourth = (EditText) findViewById(R.id.editFourth);
        final EditText editFifth = (EditText) findViewById(R.id.editFifth);
        final EditText editSixth = (EditText) findViewById(R.id.editSixth);
        final TextView editResultText = (TextView) findViewById(R.id.resultText);
        final TextView editOptimizeText = (TextView) findViewById(R.id.optimizeMessage);

        Button buttonCalculate = (Button) findViewById(R.id.calculate);
        Button buttonOptimize = (Button) findViewById(R.id.optimize);

        buttonCalculate.setOnClickListener(new View.OnClickListener()
        {
            public double roundTwoDecimals(double d)
            {
                DecimalFormat twoDForm = new DecimalFormat("#.##");
                return Double.valueOf(twoDForm.format(d));
            }

            @Override
            public void onClick(View v)
            {
                double buyAmount, buyCost, sellAmount, sellPrice, buyAmount2, buyCost2, remainder,
                        totalCost, totalAmount, finalPrice;
                boolean didItWork = true; boolean validTransaction = true;

                try
                {
                    buyAmount = Double.valueOf(editFirst.getText().toString());
                    buyCost = Double.valueOf(editSecond.getText().toString());
                    sellAmount = Double.valueOf(editThird.getText().toString());
                    sellPrice = Double.valueOf(editFourth.getText().toString());
                    buyAmount2 = Double.valueOf(editFifth.getText().toString());
                    buyCost2 = Double.valueOf(editSixth.getText().toString());

                    remainder = Math.abs((buyAmount - sellAmount));

                    if(sellAmount > buyAmount)
                    {
                        // create new dialog popup
                        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MyActivity.this);

                        // set title
                        alertDialog.setTitle("Error");

                        // set dialog message
                        alertDialog.setMessage("You cannot sell more than you own.");
                        alertDialog.setCancelable(false);
                        alertDialog.setNeutralButton("Ok", new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int id)
                            {
                                // if this button is clicked, close current activity
                                dialog.cancel();
                            }
                        });
                        alertDialog.show();
                        validTransaction = false;
                    }

                    totalCost = buyAmount * buyCost;
                    totalAmount = buyAmount2 + remainder;
                    finalPrice = totalCost / totalAmount;

                    if(validTransaction)
                    {
                        editResultText.setText("You need to sell " + String.valueOf(totalAmount) + " BTC at " + String.valueOf(roundTwoDecimals(finalPrice)));
                    }
                }
                catch(Exception e)
                {
                    didItWork = false;
                }
                finally
                {
                    if(!didItWork)
                    {
                        // create new dialog popup
                        final AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(MyActivity.this);

                        // set title
                        alertDialog2.setTitle("Error");

                        // set dialog message
                        alertDialog2.setMessage("Please fill in all fields.");
                        alertDialog2.setCancelable(false);
                        alertDialog2.setNeutralButton("Ok", new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int id)
                            {
                                // if this button is clicked, close current activity
                                dialog.cancel();
                            }
                        });
                        alertDialog2.show();
                    }
                }

            }
        });

        buttonOptimize.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                double sellAmount, sellPrice, buyCost2, newBalance, optimalBTC;
                boolean didItWork = true;

                try
                {
                    sellAmount = Double.valueOf(editThird.getText().toString());
                    sellPrice = Double.valueOf(editFourth.getText().toString());
                    buyCost2 = Double.valueOf(editSixth.getText().toString());

                    String amount = Double.toString(sellAmount);
                    String price = Double.toString(sellPrice);
                    String cost = Double.toString(buyCost2);

                    newBalance = sellAmount * sellPrice;
                    optimalBTC = newBalance / buyCost2;

                    editOptimizeText.setText("The optimal BTC you should buy is " + String.valueOf(optimalBTC) + ".");
                }
                catch (Exception e)
                {
                    // set bool to false to execute finally block below
                    didItWork = false;
                }
                finally
                {
                    if(!didItWork)
                    {
                        // create new dialog popup
                        final AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(MyActivity.this);

                        // set title
                        alertDialog3.setTitle("Error");

                        // set dialog message
                        alertDialog3.setMessage("Please look at the note.");
                        alertDialog3.setCancelable(false);
                        alertDialog3.setNeutralButton("Ok", new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int id)
                            {
                                // if this button is clicked, close current activity
                                dialog.cancel();
                            }
                        });
                        alertDialog3.show();
                    }
                }
            }
        });
    }
}
Merge branch 'master' of https://github.com/christarazi/btcbreakevenapp