Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.88 KB | None | 0 0
  1.  
  2.  
  3. Activity
  4.  
  5. package ca.roumani.taxcalc;
  6.  
  7. import android.graphics.Typeface;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.EditText;
  13. import android.widget.TextView;
  14.  
  15. public class TaxActivity extends AppCompatActivity
  16. {
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState)
  20. {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_tax);
  23. }
  24. public void buttonClicked(View v)
  25. {
  26.  
  27. View incomeView = findViewById(R.id.incomebox);
  28. EditText incomeEdit = (EditText) incomeView;
  29. String incomeString = incomeEdit.getText().toString();
  30. TaxModel model = new TaxModel(incomeString);
  31.  
  32. View outputBox = findViewById(R.id.output);
  33. ViewGroup output = (ViewGroup) outputBox;
  34.  
  35. output.removeAllViews();
  36.  
  37.  
  38. TextView taxLabel = new TextView(this);
  39. taxLabel.setText("Income Tax:");
  40. output.addView(taxLabel);
  41.  
  42. TextView taxAnswer = new TextView(this);
  43. taxAnswer.setTypeface(null, Typeface.BOLD);
  44. String answer1 = model.getTax();
  45. taxAnswer.setText(answer1);
  46. output.addView(taxAnswer);
  47.  
  48.  
  49. TextView avgLabel = new TextView(this);
  50. avgLabel.setText("Average Rate:");
  51. output.addView(avgLabel);
  52.  
  53. TextView averageAnswer = new TextView(this);
  54. averageAnswer.setTypeface(null, Typeface.BOLD);
  55. String answer2 = model.getAverageRate();
  56. averageAnswer.setText(answer2);
  57. output.addView(averageAnswer);
  58.  
  59. TextView mrgLabel = new TextView(this);
  60. mrgLabel.setText("Marginal Rate:");
  61. output.addView(mrgLabel);
  62.  
  63. TextView mrgAnswer = new TextView(this);
  64. mrgAnswer.setTypeface(null, Typeface.BOLD);
  65. String answer3 = model.getMarginalRate();
  66. mrgAnswer.setText(answer3);
  67. output.addView(mrgAnswer);
  68.  
  69.  
  70. TextView cppLabel = new TextView(this);
  71. cppLabel.setText("CPP:");
  72. output.addView(cppLabel);
  73.  
  74. TextView cppAnswer = new TextView(this);
  75. cppAnswer.setTypeface(null, Typeface.BOLD);
  76. String answer4 = model.getCPP();
  77. cppAnswer.setText(answer4);
  78. output.addView(cppAnswer);
  79.  
  80.  
  81. TextView eiLabel = new TextView(this);
  82. eiLabel.setText("EI:");
  83. output.addView(eiLabel);
  84.  
  85. TextView eiAnswer = new TextView(this);
  86. eiAnswer.setTypeface(null, Typeface.BOLD);
  87. String answer5 = model.getEI();
  88. eiAnswer.setText(answer5);
  89. output.addView(eiAnswer);
  90. }
  91.  
  92.  
  93.  
  94. }
  95.  
  96.  
  97.  
  98.  
  99. Model
  100.  
  101. package ca.roumani.taxcalc;
  102.  
  103.  
  104. public class TaxModel
  105. {
  106. private double income;
  107. private double tax, average, marginal, cpp, ei;
  108.  
  109. public static final double BRACKET_0 = 11475.0;
  110. public static final double BRACKET_1 = 33808.0;
  111. public static final double BRACKET_2 = 40895.0;
  112. public static final double BRACKET_3 = 63823.0;
  113. public static final double RATE_1 = 22.79 / 100.0;
  114. public static final double RATE_2 = 33.23 / 100.0;
  115. public static final double RATE_3 = 45.93 / 100.0;
  116. public static final double RATE_4 = 52.75 / 100.0;
  117.  
  118. public static final double EI_RATE = 1.63/100.0;
  119. public static final double EI_MAX = 836.19;
  120.  
  121. public static final double CPP_RATE = 4.95/100.0;
  122. public static final double CPP_MAX = 2564.10;
  123. public static final double CPP_EXEMPT = 3500.0;
  124.  
  125. public TaxModel(String p)
  126. {
  127. this.income = Double.parseDouble(p);
  128. }
  129.  
  130. public void setIncome(double i)
  131. {
  132. this.income = i;
  133. }
  134.  
  135. public String getTax()
  136. {
  137. if (income <= BRACKET_0)
  138. {
  139. tax = 0;
  140. String tax_2 = String.format("%,.2f",tax);
  141. return tax_2;
  142. } else if (income > BRACKET_0 && income <= 45283)
  143. {
  144. tax = ((income - BRACKET_0) * 0.2279);
  145. String tax_2 = String.format("%,.2f",tax);
  146. return tax_2;
  147. } else if (income > 45283 && income <= 86178)
  148. {
  149. tax = (((income - BRACKET_0) - BRACKET_1) * RATE_2) + (BRACKET_1 * RATE_1);
  150. String tax_2 = String.format("%,.2f",tax);
  151. return tax_2;
  152. } else if ((income > 86178 && income <= 150003))
  153. {
  154. tax = (((income - BRACKET_0) - BRACKET_2) * RATE_3) + (BRACKET_2 * RATE_1);
  155. String tax_2 = String.format("%,.2f",tax);
  156. return tax_2;
  157. } else
  158. {
  159. tax = (((income - BRACKET_0) - BRACKET_3) * RATE_4) + (BRACKET_3 * RATE_1);
  160. String tax_2 = String.format("%,.2f",tax);
  161. return tax_2;
  162. }
  163. }
  164.  
  165. public String getAverageRate()
  166. {
  167. average = (tax / income) * 100;
  168. if(income == 0)
  169. {
  170. average = 0;
  171. }
  172. String average2 = String.format("%.0f", average);
  173. return average2 + "%";
  174.  
  175. }
  176.  
  177. public String getMarginalRate()
  178. {
  179. if (income <= BRACKET_0)
  180. {
  181. marginal = 0;
  182. String marginal_2 = String.format("%.0f", marginal);
  183. return marginal_2 + "%";
  184. } else if (income > BRACKET_0 && income <= 45283)
  185. {
  186. marginal = 22.79;
  187. String marginal_2 = String.format("%.0f", marginal);
  188. return marginal_2 + "%";
  189. } else if (income > 45283 && income <= 86178)
  190. {
  191. marginal = 33.23;
  192. String marginal_2 = String.format("%.0f", marginal);
  193. return marginal_2 + "%";
  194. } else if ((income > 86178 && income <= 150003))
  195. {
  196. marginal = 45.93;
  197. String marginal_2 = String.format("%.0f", marginal);
  198. return marginal_2 + "%";
  199. } else {
  200. marginal = 52.75;
  201. String marginal_2 = String.format("%.0f", marginal);
  202. return marginal_2 + "%";
  203. }
  204. }
  205.  
  206. public String getCPP()
  207. {
  208. cpp = (income - CPP_EXEMPT) * CPP_RATE;
  209. if(cpp >= CPP_MAX)
  210. {
  211. cpp = CPP_MAX;
  212. String cpp2 = String.format("%,.0f", cpp);
  213. return cpp2;
  214. } else {
  215. String cpp2 = String.format("%,.0f", cpp);
  216. return cpp2; }
  217. }
  218.  
  219. public String getEI()
  220. {
  221. ei = income * EI_RATE;
  222. if(ei > EI_MAX)
  223. {
  224. ei = EI_MAX;
  225. String ei_2 = String.format("%,.0f", ei);
  226. return ei_2;
  227. } else {
  228. String ei_2 = String.format("%,.0f", ei);
  229. return ei_2; }
  230. }
  231. }
  232.  
  233.  
  234. XML
  235.  
  236. <?xml version="1.0" encoding="utf-8"?>
  237. <RelativeLayout
  238. xmlns:android="http://schemas.android.com/apk/res/android"
  239. xmlns:tools="http://schemas.android.com/tools"
  240. android:layout_width="match_parent"
  241. android:layout_height="match_parent"
  242. android:paddingBottom="@dimen/activity_vertical_margin"
  243. android:paddingLeft="@dimen/activity_horizontal_margin"
  244. android:paddingRight="@dimen/activity_horizontal_margin"
  245. android:paddingTop="@dimen/activity_vertical_margin"
  246. tools:context="ca.roumani.taxcalc.TaxActivity">
  247.  
  248. <TextView
  249. android:layout_width="wrap_content"
  250. android:layout_height="wrap_content"
  251. android:text="Annual Income"
  252. android:id="@+id/textView"
  253. android:layout_alignParentTop="true"
  254. android:layout_alignParentLeft="true"
  255. android:layout_alignParentStart="true"/>
  256.  
  257. <EditText
  258. android:layout_width="wrap_content"
  259. android:layout_height="wrap_content"
  260. android:inputType="numberDecimal"
  261. android:ems="10"
  262. android:id="@+id/incomebox"
  263. android:layout_below="@+id/textView"
  264. android:layout_alignParentLeft="true"
  265. android:layout_alignParentStart="true"
  266. android:layout_alignParentRight="true"
  267. android:layout_alignParentEnd="true"/>
  268.  
  269. <Button
  270. android:layout_width="wrap_content"
  271. android:layout_height="wrap_content"
  272. android:text="Compute Tax"
  273. android:id="@+id/button"
  274. android:layout_below="@+id/incomebox"
  275. android:layout_alignParentRight="true"
  276. android:layout_alignParentEnd="true"
  277. android:layout_alignParentLeft="true"
  278. android:layout_alignParentStart="true"
  279. android:onClick="buttonClicked"/>
  280.  
  281. <LinearLayout
  282. android:orientation="vertical"
  283. android:layout_width="match_parent"
  284. android:layout_height="match_parent"
  285. android:layout_centerHorizontal="true"
  286. android:layout_below="@+id/button"
  287. android:id="@+id/output">
  288. </LinearLayout>
  289. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement