import java.util.Scanner; /** * This is the 2nd 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 BetterTaxHelper{ private Scanner scan; /** * Creates a new tax helper */ public BetterTaxHelper(){ 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?"); // Under 65 years old if(isYes){ isYes = getUserResponse("Is your gross income less than $8450?"); // Less than income threshold if(isYes){ printDecision(false); } // More than income threshold else{ printDecision(true); } } // Over 65 years old else{ isYes = getUserResponse("Is your gross income less than $9700?"); // Less than income threshold if(isYes){ printDecision(false); } // More than income threshold else{ printDecision(true); } } } // Married else{ isYes = getUserResponse("Are you and your spouse both over 65?"); // Over 65 if(isYes){ isYes = getUserResponse("Is your combined gross income less than $23,000?"); // Less than income threshold if(isYes){ printDecision(false); } // More than income threshold else{ printDecision(true); } } // At least one person is under 65 else{ isYes = getUserResponse("Are you and your spouse both under 65?"); // Both under 65 if(isYes){ isYes = getUserResponse("Is your combined gross income less than $20,600?"); // Less than income threshold if(isYes){ printDecision(false); } // More than income threshold else{ printDecision(true); } } // One person over 65 and one person under 65 else{ isYes = getUserResponse("Is your combined gross income less than $21,800?"); // Less than income threshold if(isYes){ printDecision(false); } // More than income threshold else{ printDecision(true); } } } } } /*================================================================ * 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 reads * the user's response * @param question a yes-no question * @return true if the isYes to the question is yes and false otherwise */ private boolean getUserResponse(String question){ System.out.println(question); String input = scan.nextLine(); boolean isYes = input.equals("y"); return isYes; } /** * 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"); } } }