Advertisement
Ligh7_of_H3av3n

Tax Calculator

Feb 18th, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. package MidExam;
  2.  
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class TaxCalculator {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.  
  11.  
  12.         String vehiclesInput = scanner.nextLine();
  13.  
  14.  
  15.         String[] vehicles = vehiclesInput.split(">>");
  16.  
  17.  
  18.         double totalTaxCollected = 0;
  19.  
  20.  
  21.         for (String vehicleData : vehicles) {
  22.  
  23.             String[] vehicleInfo = vehicleData.split("\\s+");
  24.             String type = vehicleInfo[0];
  25.             int years = Integer.parseInt(vehicleInfo[1]);
  26.             int kilometers = Integer.parseInt(vehicleInfo[2]);
  27.  
  28.  
  29.             double tax = calculateTax(type, years, kilometers);
  30.  
  31.  
  32.             if (tax >= 0) {
  33.                 System.out.printf("A %s car will pay %.2f euros in taxes.%n", type, tax);
  34.                 totalTaxCollected += tax;
  35.             } else {
  36.                 System.out.println("Invalid car type.");
  37.             }
  38.         }
  39.  
  40.  
  41.         System.out.printf("The National Revenue Agency will collect %.2f euros in taxes.", totalTaxCollected);
  42.     }
  43.  
  44.  
  45.     private static double calculateTax(String type, int years, int kilometers) {
  46.         switch (type) {
  47.             case "family":
  48.                 return Math.max(0, 50 - (years * 5) + (kilometers / 3000) * 12);
  49.             case "heavyDuty":
  50.                 return Math.max(0, 80 - (years * 8) + (kilometers / 9000) * 14);
  51.             case "sports":
  52.                 return Math.max(0, 100 - (years * 9) + (kilometers / 2000) * 18);
  53.             default:
  54.                 return -1;
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement