Ramirez_RD

Samson_Func_Vat

Sep 6th, 2025 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1. import java.util.*;
  2. public class Samson_Func_Vat {
  3.     private static final double VAT_RATE = 0.12;
  4.     public static double calculateVatAmount(double price) {
  5.         if (price < 0) {
  6.             System.err.println("Warning: Price cannot be negative. Returning 0.0 for VAT.");
  7.             return 0.0;
  8.         }
  9.         return price * VAT_RATE;
  10.     }
  11.     public static void errorMsg(){
  12.         System.out.println("Error: Invalid Input. Please enter a number.");
  13.     }
  14.     public static void main(String[] args) {
  15.         try (Scanner scan = new Scanner(System.in)) {
  16.             System.out.println("--- Samson VAT Calculator ---");
  17.             System.out.print("Please enter the price: ");
  18.             while (true) {
  19.                 try {
  20.                     double price = scan.nextDouble();
  21.                     double vatAmount = calculateVatAmount(price);
  22.                     System.out.printf("The VAT amount for the price of %.2f is: %.2f%n", price, vatAmount);
  23.                     break;
  24.                 } catch (InputMismatchException e) {
  25.                     errorMsg();
  26.                     scan.nextLine();
  27.                     System.out.print("Please enter the price: ");
  28.                 }
  29.             }
  30.         } catch (Exception e) {
  31.             System.err.println("An unexpected error occurred: " + e.getMessage());
  32.         }
  33.         System.out.println("--- Program finished ---");
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment