Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Samson_Func_Vat {
- private static final double VAT_RATE = 0.12;
- public static double calculateVatAmount(double price) {
- if (price < 0) {
- System.err.println("Warning: Price cannot be negative. Returning 0.0 for VAT.");
- return 0.0;
- }
- return price * VAT_RATE;
- }
- public static void errorMsg(){
- System.out.println("Error: Invalid Input. Please enter a number.");
- }
- public static void main(String[] args) {
- try (Scanner scan = new Scanner(System.in)) {
- System.out.println("--- Samson VAT Calculator ---");
- System.out.print("Please enter the price: ");
- while (true) {
- try {
- double price = scan.nextDouble();
- double vatAmount = calculateVatAmount(price);
- System.out.printf("The VAT amount for the price of %.2f is: %.2f%n", price, vatAmount);
- break;
- } catch (InputMismatchException e) {
- errorMsg();
- scan.nextLine();
- System.out.print("Please enter the price: ");
- }
- }
- } catch (Exception e) {
- System.err.println("An unexpected error occurred: " + e.getMessage());
- }
- System.out.println("--- Program finished ---");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment