Created
May 1, 2014 23:45
-
-
Save DarkLotus/fca8472e8d7d3523be2d to your computer and use it in GitHub Desktop.
Assignment 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * James Kidd | |
| * Student ID: S3316990 | |
| */ | |
| import java.io.PrintStream; | |
| import java.util.Scanner; | |
| public class Stage3 | |
| { | |
| // Tax brackets, and base rates | |
| static final double TAX_ONE_MAX = 10000; | |
| static final double TAX_TWO_MIN = 10000; | |
| static final double TAX_TWO_MAX = 40000; | |
| static final double TAX_TWO_BASE = 0.0; | |
| static final double TAX_TWO = 0.15; | |
| static final double TAX_THREE_MIN = 40000; | |
| static final double TAX_THREE_MAX = 80000; | |
| static final double TAX_THREE_BASE = 4500; | |
| static final double TAX_THREE = 0.30; | |
| static final double TAX_FOUR_MIN = 80000; | |
| static final double TAX_FOUR_MAX = 120000; | |
| static final double TAX_FOUR_BASE = 16500; | |
| static final double TAX_FOUR = 0.40; | |
| static final double TAX_FIVE_MIN = 120000; | |
| static final double TAX_FIVE_MAX = Double.MAX_VALUE; | |
| static final double TAX_FIVE_BASE = 32500; | |
| static final double TAX_FIVE = 0.45; | |
| static final double MEDICARE_LEVY = 0.015; | |
| // Formating strings for output | |
| private static final String FORMAT_SF = "%-25s $ %10.2f\n"; | |
| private static final String FORMAT_SS = "%-25s %s\n"; | |
| // Helpers to save typing, simplify code and keep under 80chars. | |
| private static Scanner _in; | |
| private static PrintStream _out = System.out; | |
| public static void main(String[] args) | |
| { | |
| // Initialize variables and inputs. | |
| _in = new Scanner(System.in); | |
| String financialYear, name, taxFileNumber; | |
| double income = 0.0, bankInterest = 0.0; | |
| double superContribution = 0.0, claimedDeductions = 0.0; | |
| double totalDeductions, totalIncome; | |
| // Read in all required inputs | |
| _out.println("*** Final Income Tax Calculator ***"); | |
| _out.print("Enter Name:"); | |
| name = _in.nextLine(); | |
| _out.print("Enter Tax File Number (TFN):"); | |
| taxFileNumber = _in.nextLine(); | |
| _out.print("Enter financial year:"); | |
| financialYear = _in.nextLine(); | |
| // Input prompt loop for menu system | |
| while (true) | |
| { | |
| displayMenu(); | |
| String menuChoice = _in.nextLine(); | |
| if (menuChoice == null || menuChoice.length() == 0) | |
| continue; | |
| menuChoice = menuChoice.toUpperCase(); | |
| if (menuChoice.charAt(0) == 'X') | |
| break; | |
| switch (menuChoice.charAt(0)) | |
| { | |
| case 'A': | |
| _out.printf("Enter assesable income for period %s:", | |
| financialYear); | |
| income += readDouble(); | |
| break; | |
| case 'B': | |
| _out.print("Enter Bank interest accurued:"); | |
| bankInterest += readDouble(); | |
| break; | |
| case 'C': | |
| _out.printf("Enter pre-tax superannuation contribution for period %s:", | |
| financialYear); | |
| superContribution += readDouble(); | |
| break; | |
| case 'D': | |
| _out.printf("Enter claimable deduction(s) for period %s:", | |
| financialYear); | |
| claimedDeductions += readDouble(); | |
| break; | |
| case 'X': | |
| break; | |
| default: | |
| _out.println("Unexpected menu choice"); | |
| break; | |
| } | |
| } | |
| // Calculate our total income, deductions and payable tax. | |
| totalDeductions = (claimedDeductions + superContribution); | |
| totalIncome = (income + bankInterest); | |
| double taxableIncome = totalIncome - totalDeductions; | |
| double payableTax = 0.0; | |
| if (taxableIncome <= TAX_ONE_MAX) | |
| { | |
| } | |
| else if (taxableIncome <= TAX_TWO_MAX) | |
| { | |
| payableTax = (taxableIncome - TAX_TWO_MIN) * TAX_TWO + TAX_TWO_BASE; | |
| } | |
| else if (taxableIncome <= TAX_THREE_MAX) | |
| { | |
| payableTax = | |
| (taxableIncome - TAX_THREE_MIN) * TAX_THREE + TAX_THREE_BASE; | |
| } | |
| else if (taxableIncome <= TAX_FOUR_MAX) | |
| { | |
| payableTax = (taxableIncome - TAX_FOUR_MIN) * TAX_FOUR + TAX_FOUR_BASE; | |
| } | |
| else if (taxableIncome <= TAX_FIVE_MAX) | |
| { | |
| payableTax = (taxableIncome - TAX_FIVE_MIN) * TAX_FIVE + TAX_FIVE_BASE; | |
| } | |
| // Check for Medicare levy and calculate if needed. Prompt for Tax agents | |
| // name. | |
| boolean hasHealthcare; | |
| String taxAgentName; | |
| double payableMedicareLevy = 0.0; | |
| _out.print("Does the taxpayer currently have private health insurance? (Y/N):"); | |
| hasHealthcare = readTextualBoolean(); | |
| if (!hasHealthcare) | |
| payableMedicareLevy = taxableIncome * MEDICARE_LEVY; | |
| _out.print("Enter tax agent name:"); | |
| taxAgentName = _in.nextLine(); | |
| // Output the results. | |
| _out.printf("%35s", "*** Final Tax Statement ***\n\n"); | |
| _out.printf(FORMAT_SS, "Name:", name); | |
| _out.printf(FORMAT_SS, "Tax File Number:", taxFileNumber); | |
| _out.println(String.format(FORMAT_SS, "Financial Year:", financialYear)); | |
| _out.printf(FORMAT_SF, "Assesable Income:", totalIncome); | |
| _out.printf(FORMAT_SF, "Minus Tax Offsets:", -totalDeductions); | |
| _out.println(String.format(FORMAT_SF, "Taxable Income:", taxableIncome)); | |
| _out.printf(FORMAT_SF, "Tax Payable:", payableTax); | |
| if (!hasHealthcare) | |
| _out.printf(FORMAT_SF, "Medicare levy:", | |
| payableMedicareLevy); | |
| _out.printf("Tax Agent: %s\n", taxAgentName); | |
| } | |
| private static void displayMenu() | |
| { | |
| _out.println("--------------"); | |
| _out.println("*** Taxation Data Entry System *** "); | |
| _out.println("A - Add assessable income"); | |
| _out.println("B - Add interest accrued from bank account"); | |
| _out.println("C - Add pre-tax superannuation contribution"); | |
| _out.println("D - Add claimable deduction"); | |
| _out.println("X - Exit and compile final taxation statement"); | |
| _out.print("Enter your selection:"); | |
| } | |
| /** | |
| * Helper Method to read in a double and handle removal of dollar signs. | |
| * | |
| * @return Double value without leading $ | |
| */ | |
| private static double readDouble() | |
| { | |
| String result = _in.nextLine(); | |
| double res = Double.parseDouble(result.replace("$", "")); | |
| return res; | |
| } | |
| /** | |
| * Helper Method to read Y/N as a boolean value. | |
| * | |
| * @return true for Y/y else false. | |
| */ | |
| private static boolean readTextualBoolean() | |
| { | |
| String res = _in.nextLine(); | |
| boolean result = (res.charAt(0) == 'Y' || res.charAt(0) == 'y'); | |
| return result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment