UK Vat Calculator – Java Program

What it does:

A program which prompts the user to enter a number where it will then find out how much VAT will be added onto the item depending on its price. It will then give a new total with VAT added on. At the end of the program it will prompt the user to see if they would like to use the program again or if they would like to exit. This is done by a do-while loop. The program also features InputMismatchException to ensure that if the user enters a character by accident it will tell them what the issue was, allowing them to retry without the program crashing.

Code:


public class UKVatCalculator
{
    private static Scanner keyboard = new Scanner(System.in);
    private static double number = 0;

    private static double amount()
    {
       boolean complete =true;
       while (complete)
       {
          System.out.println("Please enter the amount");

          try
          {
             number = keyboard.nextDouble();
             complete = false;
          }//try

          catch(InputMismatchException IME)
          {
             System.out.println("Whoops, you didn't enter a number.");
             keyboard.next();

          }//catch

       }//while

        //returning the value
        return number;
    }//amount

    private static void calculations()
    {
        double taxTotal, total;

        //Calculation
        taxTotal = number*0.20%100;

        total = number + taxTotal;

        System.out.println("The total tax to be added is £" + taxTotal);
        System.out.println(" ");
        System.out.println("The total with tax will be £" + total);
        System.out.println(" ");
    }//calculations()

    public static void main(String[] args)
    {

        int runAgain;

        System.out.println("Welcome to the UK tax calculator!");

        do
        {
            //calling methods
            amount();
            calculations();

            //Prompt to see if user would like to run again
            System.out.println("Would you like to run the program again?");
            System.out.println("Enter 1 for Yes");
            System.out.println("Enter 2 for No");
            runAgain = keyboard.nextInt();
        }//do

        while (runAgain!=2);
    }//main
}//class

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s