Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Activity
- package ca.roumani.taxcalc;
- import android.graphics.Typeface;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.EditText;
- import android.widget.TextView;
- public class TaxActivity extends AppCompatActivity
- {
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_tax);
- }
- public void buttonClicked(View v)
- {
- View incomeView = findViewById(R.id.incomebox);
- EditText incomeEdit = (EditText) incomeView;
- String incomeString = incomeEdit.getText().toString();
- TaxModel model = new TaxModel(incomeString);
- View outputBox = findViewById(R.id.output);
- ViewGroup output = (ViewGroup) outputBox;
- output.removeAllViews();
- TextView taxLabel = new TextView(this);
- taxLabel.setText("Income Tax:");
- output.addView(taxLabel);
- TextView taxAnswer = new TextView(this);
- taxAnswer.setTypeface(null, Typeface.BOLD);
- String answer1 = model.getTax();
- taxAnswer.setText(answer1);
- output.addView(taxAnswer);
- TextView avgLabel = new TextView(this);
- avgLabel.setText("Average Rate:");
- output.addView(avgLabel);
- TextView averageAnswer = new TextView(this);
- averageAnswer.setTypeface(null, Typeface.BOLD);
- String answer2 = model.getAverageRate();
- averageAnswer.setText(answer2);
- output.addView(averageAnswer);
- TextView mrgLabel = new TextView(this);
- mrgLabel.setText("Marginal Rate:");
- output.addView(mrgLabel);
- TextView mrgAnswer = new TextView(this);
- mrgAnswer.setTypeface(null, Typeface.BOLD);
- String answer3 = model.getMarginalRate();
- mrgAnswer.setText(answer3);
- output.addView(mrgAnswer);
- TextView cppLabel = new TextView(this);
- cppLabel.setText("CPP:");
- output.addView(cppLabel);
- TextView cppAnswer = new TextView(this);
- cppAnswer.setTypeface(null, Typeface.BOLD);
- String answer4 = model.getCPP();
- cppAnswer.setText(answer4);
- output.addView(cppAnswer);
- TextView eiLabel = new TextView(this);
- eiLabel.setText("EI:");
- output.addView(eiLabel);
- TextView eiAnswer = new TextView(this);
- eiAnswer.setTypeface(null, Typeface.BOLD);
- String answer5 = model.getEI();
- eiAnswer.setText(answer5);
- output.addView(eiAnswer);
- }
- }
- Model
- package ca.roumani.taxcalc;
- public class TaxModel
- {
- private double income;
- private double tax, average, marginal, cpp, ei;
- public static final double BRACKET_0 = 11475.0;
- public static final double BRACKET_1 = 33808.0;
- public static final double BRACKET_2 = 40895.0;
- public static final double BRACKET_3 = 63823.0;
- public static final double RATE_1 = 22.79 / 100.0;
- public static final double RATE_2 = 33.23 / 100.0;
- public static final double RATE_3 = 45.93 / 100.0;
- public static final double RATE_4 = 52.75 / 100.0;
- public static final double EI_RATE = 1.63/100.0;
- public static final double EI_MAX = 836.19;
- public static final double CPP_RATE = 4.95/100.0;
- public static final double CPP_MAX = 2564.10;
- public static final double CPP_EXEMPT = 3500.0;
- public TaxModel(String p)
- {
- this.income = Double.parseDouble(p);
- }
- public void setIncome(double i)
- {
- this.income = i;
- }
- public String getTax()
- {
- if (income <= BRACKET_0)
- {
- tax = 0;
- String tax_2 = String.format("%,.2f",tax);
- return tax_2;
- } else if (income > BRACKET_0 && income <= 45283)
- {
- tax = ((income - BRACKET_0) * 0.2279);
- String tax_2 = String.format("%,.2f",tax);
- return tax_2;
- } else if (income > 45283 && income <= 86178)
- {
- tax = (((income - BRACKET_0) - BRACKET_1) * RATE_2) + (BRACKET_1 * RATE_1);
- String tax_2 = String.format("%,.2f",tax);
- return tax_2;
- } else if ((income > 86178 && income <= 150003))
- {
- tax = (((income - BRACKET_0) - BRACKET_2) * RATE_3) + (BRACKET_2 * RATE_1);
- String tax_2 = String.format("%,.2f",tax);
- return tax_2;
- } else
- {
- tax = (((income - BRACKET_0) - BRACKET_3) * RATE_4) + (BRACKET_3 * RATE_1);
- String tax_2 = String.format("%,.2f",tax);
- return tax_2;
- }
- }
- public String getAverageRate()
- {
- average = (tax / income) * 100;
- if(income == 0)
- {
- average = 0;
- }
- String average2 = String.format("%.0f", average);
- return average2 + "%";
- }
- public String getMarginalRate()
- {
- if (income <= BRACKET_0)
- {
- marginal = 0;
- String marginal_2 = String.format("%.0f", marginal);
- return marginal_2 + "%";
- } else if (income > BRACKET_0 && income <= 45283)
- {
- marginal = 22.79;
- String marginal_2 = String.format("%.0f", marginal);
- return marginal_2 + "%";
- } else if (income > 45283 && income <= 86178)
- {
- marginal = 33.23;
- String marginal_2 = String.format("%.0f", marginal);
- return marginal_2 + "%";
- } else if ((income > 86178 && income <= 150003))
- {
- marginal = 45.93;
- String marginal_2 = String.format("%.0f", marginal);
- return marginal_2 + "%";
- } else {
- marginal = 52.75;
- String marginal_2 = String.format("%.0f", marginal);
- return marginal_2 + "%";
- }
- }
- public String getCPP()
- {
- cpp = (income - CPP_EXEMPT) * CPP_RATE;
- if(cpp >= CPP_MAX)
- {
- cpp = CPP_MAX;
- String cpp2 = String.format("%,.0f", cpp);
- return cpp2;
- } else {
- String cpp2 = String.format("%,.0f", cpp);
- return cpp2; }
- }
- public String getEI()
- {
- ei = income * EI_RATE;
- if(ei > EI_MAX)
- {
- ei = EI_MAX;
- String ei_2 = String.format("%,.0f", ei);
- return ei_2;
- } else {
- String ei_2 = String.format("%,.0f", ei);
- return ei_2; }
- }
- }
- XML
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- 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: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="ca.roumani.taxcalc.TaxActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Annual Income"
- android:id="@+id/textView"
- android:layout_alignParentTop="true"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"/>
- <EditText
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:inputType="numberDecimal"
- android:ems="10"
- android:id="@+id/incomebox"
- android:layout_below="@+id/textView"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:layout_alignParentRight="true"
- android:layout_alignParentEnd="true"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Compute Tax"
- android:id="@+id/button"
- android:layout_below="@+id/incomebox"
- android:layout_alignParentRight="true"
- android:layout_alignParentEnd="true"
- android:layout_alignParentLeft="true"
- android:layout_alignParentStart="true"
- android:onClick="buttonClicked"/>
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_centerHorizontal="true"
- android:layout_below="@+id/button"
- android:id="@+id/output">
- </LinearLayout>
- </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement