Pages

Monday, September 19, 2016

Android Example -Tip Calculator (Using Double)


Should you tip?
Yes, if the service was acceptable. Many jobs in the service industry pay very little. Without tips, these workers would have a hard time raising a family.
Who should you tip?
Just about anyone in the service industry. Including, but not limited to, non-fast food restaurant workers, barbers, maids, taxi drivers, bartenders, and webmasters of conversion sites. ðŸ˜‰
How much should you tip?
The generally accepted value is 15% to 20%, though outside North America it may be different.
If service was horrible, tip nothing and notify management. If the service was slow, tip 10%. If service was ok, tip 15%. If service was great, tip 20%.
Keep in mind if the service is slow it is not always the servers fault. If you ever plan to return then the service was good enough that you should tip something. If the service was so poor that you plan not to tip, then it was also poor enough that you should notify management.

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="8"
            android:text="Total Bill Amount :"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/total_amount"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="$100"
            android:inputType="numberDecimal" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="8"
            android:text="Tip (%) :"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/percentage"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="15%"
            android:inputType="numberDecimal" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="8"
            android:text="Number of people :"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/people"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="2"
            android:inputType="phone" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#b6eaf9"
        android:orientation="horizontal"
        android:padding="2dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="7"
            android:text="Total per person :"
            android:textSize="25dp" />

        <TextView
            android:id="@+id/total_per_person"
            android:layout_width="fill_parent"

            android:layout_height="wrap_content"
            android:textSize="20dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#b6eaf9"
        android:orientation="horizontal"
        android:padding="2dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="7"
            android:text="Total to Pay:"
            android:textSize="25dp" />

        <TextView
            android:id="@+id/total_to_pay"
            android:layout_width="fill_parent"

            android:layout_height="wrap_content"
            android:textSize="20dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#b6eaf9"
        android:orientation="horizontal"
        android:padding="2dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="7"
            android:text="Tip Amount:"
            android:textSize="25dp" />

        <TextView
            android:id="@+id/tip_amount"
            android:layout_width="fill_parent"

            android:layout_height="wrap_content"
            android:textSize="20dp" />
    </LinearLayout>

    <Button
        android:id="@+id/calcu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Calculate" />

</LinearLayout>

MainActivity.java
package in.kantapp.example19;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class MainActivity extends AppCompatActivity
{

    EditText total_amount,percent,people;
    TextView total_per_person,total_to_pay,tip_amount;
    Double tpp,ttp,ta;

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

        total_amount= (EditText) findViewById(R.id.total_amount);
        percent= (EditText) findViewById(R.id.percentage);
        people= (EditText) findViewById(R.id.people);


        total_per_person= (TextView) findViewById(R.id.total_per_person);
        total_to_pay= (TextView) findViewById(R.id.total_to_pay);
        tip_amount= (TextView) findViewById(R.id.tip_amount);

        Button calcu= (Button) findViewById(R.id.calcu);

        calcu.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                if (total_amount.getText().toString().equals(""))
                {
                    Toast.makeText(MainActivity.this, "Enter Amount", Toast.LENGTH_SHORT).show();
                }
                else if (percent.getText().toString().equals(""))
                {
                    Toast.makeText(MainActivity.this, "Enter Percent", Toast.LENGTH_SHORT).show();
                }
                else if (people.getText().toString().equals(""))
                {
                    Toast.makeText(MainActivity.this, "Enter total People", Toast.LENGTH_SHORT).show();
                }
                else
                {

                    try
                    {
                        double a = Double.parseDouble(total_amount.getText().toString());
                        double b = Double.parseDouble(percent.getText().toString());
                        double c = Double.parseDouble(people.getText().toString());

                        ta = (a*b) / 100;
                        ttp=ta+a;
                        tpp=ttp/c;

                        total_per_person.setText(String.valueOf(new DecimalFormat("##.##").format(tpp)));
                        total_to_pay.setText(String.valueOf(new DecimalFormat("##.##").format(ttp)));
                        tip_amount.setText(String.valueOf(new DecimalFormat("##.##").format(ta)));
                    }
                    catch(NumberFormatException e)
                    {
                        Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });

    }
}

No comments:

Post a Comment