summaryrefslogtreecommitdiff
path: root/src/Q10/TaxReturn.java
blob: 7fbd48bc84b480b20d77fc80f7d7621b3c6b5330 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
 * Assignment 1, COMP268 Class: TaxReturn.java
 *
 * @description Represents a contact from an address book.
 * @author: mo khan Student ID: 3431709
 * @date May 6, 2019
 * @version 1.0
 */
package Q10;

import java.util.Scanner;

public class TaxReturn {
  private double income;
  private int status;
  private int children;

  /**
   * Constructs a TaxReturn object for a given income and marital status, and computes the tax.
   *
   * @param income the taxpayer income
   * @param status either SINGLE or MARRIED
   */
  public TaxReturn(double income, int status) {
    this(income, status, 0);
  }

  /**
   * Constructs a TaxReturn object for a given income and marital status and, and # of children.
   *
   * @param income the taxpayer income
   * @param status either SINGLE or MARRIED
   * @param children the number of children
   */
  public TaxReturn(double income, int status, int children) {
    this.income = income;
    this.status = status;
    this.children = children;
    if (this.isSingle()) this.income -= children * 5000;
  }

  /**
   * Returns the calculated taxes to pay.
   *
   * @return the amount of tax to pay.
   */
  public double getTax() {
    double tax = 0;

    if (isSingle()) {
      if (income <= SINGLE_BRACKET1) tax = RATE1 * income;
      else if (income <= SINGLE_BRACKET2)
        tax = RATE1 * SINGLE_BRACKET1 + RATE2 * (income - SINGLE_BRACKET1);
      else
        tax =
            RATE1 * SINGLE_BRACKET1
                + RATE2 * (SINGLE_BRACKET2 - SINGLE_BRACKET1)
                + RATE3 * (income - SINGLE_BRACKET2);

      if (income > 249999.0) tax += (income - 150000) * 0.25;
    } else if (isMarried()) {
      if (income <= MARRIED_BRACKET1) tax = RATE1 * income;
      else if (income <= MARRIED_BRACKET2)
        tax = RATE1 * MARRIED_BRACKET1 + RATE2 * (income - MARRIED_BRACKET1);
      else
        tax =
            RATE1 * MARRIED_BRACKET1
                + RATE2 * (MARRIED_BRACKET2 - MARRIED_BRACKET1)
                + RATE3 * (income - MARRIED_BRACKET2);

      if (income > 349999.0) tax += (income - 200000) * 0.35;
    } else {
      tax = income * 0.33;
    }
    return tax;
  }

  public static final int SINGLE = 1;
  public static final int MARRIED = 2;

  public static final double RATE1 = 0.15;
  public static final double RATE2 = 0.28;
  public static final double RATE3 = 0.31;

  public static final double SINGLE_BRACKET1 = 21450;
  public static final double SINGLE_BRACKET2 = 51900;

  public static final double MARRIED_BRACKET1 = 35800;
  public static final double MARRIED_BRACKET2 = 86500;

  private boolean isSingle() {
    return this.status == SINGLE;
  }

  private boolean isMarried() {
    return this.status == MARRIED;
  }

  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.print("Please enter your income: ");
    double income = in.nextDouble();

    System.out.print("Enter S (single) or M (married): ");
    String input = in.next();
    int status = 0;

    if (input.equalsIgnoreCase("S")) status = TaxReturn.SINGLE;
    else if (input.equalsIgnoreCase("M")) status = TaxReturn.MARRIED;
    else {
      System.out.println("Bad input.");
      return;
    }
    System.out.print("Enter # of children: ");
    int children = in.nextInt();

    TaxReturn aTaxReturn = new TaxReturn(income, status, children);
    System.out.println("The tax is " + aTaxReturn.getTax());
  }
}