Skip to content
Advertisement

Java Income Tax Calculator need to add prompt to add more customers

I have a java program that calculates users’ income taxes, with some help the project now runs correctly, but I am supposed to add an option for the user to process additional customers once they have finished with a customer by entering ‘y’ into a prompt. I need the prompt to say: “Process another customer? (y/n)?” Since the user could also hit “n” this prompt will also have to have a command for both ‘y’ and ‘n’ how and where do I do this and where do I need to put the prompt in my code? here is my code:

import java.util.Scanner;

public class TaxCalculator {
    public static void main(String[] args) {
        final double RATE1 = 0.20;
        final double RATE2 = 0.25;
        final double RATE3 = 0.10;
        final double RATE4 = 0.15;
        final double RATE5 = 0.30;
        final double RATE1_SINGLE_LIMIT = 0;
        final double RATE2_MARRIED_LIMIT = 0;
        final double RATE3_COHABITATING_LIMIT = 20000;
        final double RATE4_COHABITATING_LIMIT = 50000;
        double tax = 0;

        //Enter Income
        Scanner in = new Scanner(System.in);
        System.out.print("Please enter your income: ");
        double income = in.nextDouble();

        System.out.print("Please enter 's' for single, 'm' for married, or 'c' for cohabitating: ");
        String maritalStatus = in.next();

        //Calculate Taxes

        if (maritalStatus.equals("s") && income > RATE1_SINGLE_LIMIT) {
            tax = RATE1 * income;
        }else if (maritalStatus.equals("m") && income > RATE2_MARRIED_LIMIT) {
            tax = RATE2 * income;
        }else if (maritalStatus.equals("c") && income <= RATE3_COHABITATING_LIMIT) {
            tax = RATE3 * income;
        }else if (maritalStatus.equals("c") && income <= RATE4_COHABITATING_LIMIT) {
            tax = RATE4 * income;
        } else {
            tax= RATE5 * income;
        }

        System.out.print("Your tax is: " + tax);
    }
}

Advertisement

Answer

Make use of a do...while loop.
The while and do-while Statements | Java Documentation

import java.util.Scanner;

public class TaxCalculator {
    static void calculate() {

        final double RATE1 = 0.20;
        final double RATE2 = 0.25;
        final double RATE3 = 0.10;
        final double RATE4 = 0.15;
        final double RATE5 = 0.30;
        final double RATE1_SINGLE_LIMIT = 0;
        final double RATE2_MARRIED_LIMIT = 0;
        final double RATE3_COHABITATING_LIMIT = 20000;
        final double RATE4_COHABITATING_LIMIT = 50000;
        double tax = 0;
        Scanner in = new Scanner(System.in);
        //Enter Income
        System.out.print("Please enter your income: ");
        double income = in.nextDouble();
        in.nextLine();

        System.out.print("Please enter 's' for single, 'm' for married, or 'c' for cohabitating: ");
        String maritalStatus = in.next();
        in.nextLine();

        //Calculate Taxes

        if (maritalStatus.equals("s") && income > RATE1_SINGLE_LIMIT) {
            tax = RATE1 * income;
        } else if (maritalStatus.equals("m") && income > RATE2_MARRIED_LIMIT) {
            tax = RATE2 * income;
        } else if (maritalStatus.equals("c") && income <= RATE3_COHABITATING_LIMIT) {
            tax = RATE3 * income;
        } else if (maritalStatus.equals("c") && income <= RATE4_COHABITATING_LIMIT) {
            tax = RATE4 * income;
        } else {
            tax = RATE5 * income;
        }

        System.out.print("Your tax is: " + tax);

    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String newResponse = "";
        do {
            calculate();
            System.out.println();
            System.out.println("Process another response?. Please enter 'y' for yes, or 'n' for no: ");
            newResponse = in.next();
            in.nextLine();
        } while (newResponse.equals("y"));

    }
}
Advertisement