Advertisement
Ligh7_of_H3av3n

wdawd

Feb 18th, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. String vehiclesInputData = scanner.nextLine().trim();
  2.         String[] vehicles = vehiclesInputData.split(">>");
  3.  
  4.         double totalTaxCollected = 0;
  5.  
  6.         for (String vehicle : vehicles) {
  7.             String[] vehicleInfo = vehicle.trim().split("\\s+");
  8.             if (vehicleInfo.length != 3) {
  9.                 System.out.println("Invalid input format: " + vehicle);
  10.                 continue;
  11.             }
  12.  
  13.             String type = vehicleInfo[0];
  14.             int yearsTax;
  15.             int kilometersTravel;
  16.             try {
  17.                 yearsTax = Integer.parseInt(vehicleInfo[1]);
  18.                 kilometersTravel = Integer.parseInt(vehicleInfo[2]);
  19.             } catch (NumberFormatException e) {
  20.                 System.out.println("Invalid input format: " + vehicle);
  21.                 continue;
  22.             }
  23.  
  24.             if (yearsTax < 0 || kilometersTravel < 0) {
  25.                 System.out.println("Invalid input format: " + vehicle);
  26.                 continue;
  27.             }
  28.  
  29.             double tax = calculateTax(type, yearsTax, kilometersTravel);
  30.             tax = Math.max(0, tax);
  31.             totalTaxCollected += tax;
  32.             System.out.printf("A %s car will pay %.2f euros in taxes.%n", type, tax);
  33.         }
  34.  
  35.         System.out.printf("The National Revenue Agency will collect %.2f euros in taxes.", totalTaxCollected);
  36.     }
  37.  
  38.     private static double calculateTax(String type, int years, int kilometers) {
  39.         switch (type) {
  40.             case "family":
  41.                 return Math.max(0, Math.round(50 - (years * 5) + ((double) kilometers / 3000) * 12 * 100.0) / 100.0);
  42.             case "heavyDuty":
  43.                 return Math.max(0, Math.round(80 - (years * 8) + ((double) kilometers / 9000) * 14 * 100.0) / 100.0);
  44.             case "sports":
  45.                 return Math.max(0, Math.round(100 - (years * 9) + ((double) kilometers / 2000) * 18 * 100.0) / 100.0);
  46.             default:
  47.                 System.out.printf("Invalid car type: %s.%n", type);
  48.                 return 0;
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement