import java.util.Scanner; /** * This is the final version of TaxHelper. This class illustrates how * private methods can be used to * * (1) Keep your methods short and concise * (2) Keep your code organized * (3) Reduce repeated code. Repeated code tempts us to copy-and-paste * which is one of the easiest ways to propagate errors. */ public class BestTaxHelper{ private Scanner scan; /** * Creates a new tax helper */ public BestTaxHelper(){ scan = new Scanner(System.in); printWelcomeMessage(); } /** * Helps the user determine whether or not they need to file taxes */ public void determineFilingStatus(){ boolean isYes = getUserResponse("Are you single? (y/n)"); // Single if(isYes){ isYes = getUserResponse("Are you under 65 years of age?"); if(isYes){ askIncomeStatus(8450.0); } else{ askIncomeStatus(9700.0); } } // Married else{ isYes = getUserResponse("Are you and your spouse both over 65?"); if(isYes){ askIncomeStatus(23000); } else{ isYes = getUserResponse("Are you and your spouse both under 65?"); if(isYes){ askIncomeStatus(20600); } else{ askIncomeStatus(21800); } } } } /*================================================================ * Below are the private helper methods *================================================================*/ /** * Prints a welcome message to the user */ private void printWelcomeMessage(){ System.out.println("======================================="); System.out.println("Welcome to the Tax Center!"); System.out.println("We offer non-professional tax help."); System.out.println("Use at your own risk!"); System.out.println(); } /** * This method prints a yes-no question to the screen and then gets * the user's response * @param question a yes-no question * @return true if the answer to the question is yes and false otherwise */ private boolean getUserResponse(String question){ System.out.println(question); String input = scan.nextLine(); return input.equals("y"); } /** * Prints the final decision * @param mustFile true if the user must file and false otherwise */ private void printDecision(boolean mustFile){ if(mustFile){ System.out.println("Yes, you must file taxes."); } else{ System.out.println("No, you do not need to file taxes"); } } /** * Asks the user about their gross income. Based on the response, * a final decision is made. * @param incomeAmount the gross income */ private void askIncomeStatus(double incomeAmount){ boolean isYes = getUserResponse("Is your gross income less than " + incomeAmount); if(isYes){ printDecision(false); } else{ printDecision(true); } } }