Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String vehiclesInputData = scanner.nextLine().trim();
- String[] vehicles = vehiclesInputData.split(">>");
- double totalTaxCollected = 0;
- for (String vehicle : vehicles) {
- String[] vehicleInfo = vehicle.trim().split("\\s+");
- if (vehicleInfo.length != 3) {
- System.out.println("Invalid input format: " + vehicle);
- continue;
- }
- String type = vehicleInfo[0];
- int yearsTax;
- int kilometersTravel;
- try {
- yearsTax = Integer.parseInt(vehicleInfo[1]);
- kilometersTravel = Integer.parseInt(vehicleInfo[2]);
- } catch (NumberFormatException e) {
- System.out.println("Invalid input format: " + vehicle);
- continue;
- }
- if (yearsTax < 0 || kilometersTravel < 0) {
- System.out.println("Invalid input format: " + vehicle);
- continue;
- }
- double tax = calculateTax(type, yearsTax, kilometersTravel);
- tax = Math.max(0, tax);
- totalTaxCollected += tax;
- System.out.printf("A %s car will pay %.2f euros in taxes.%n", type, tax);
- }
- System.out.printf("The National Revenue Agency will collect %.2f euros in taxes.", totalTaxCollected);
- }
- private static double calculateTax(String type, int years, int kilometers) {
- switch (type) {
- case "family":
- return Math.max(0, Math.round(50 - (years * 5) + ((double) kilometers / 3000) * 12 * 100.0) / 100.0);
- case "heavyDuty":
- return Math.max(0, Math.round(80 - (years * 8) + ((double) kilometers / 9000) * 14 * 100.0) / 100.0);
- case "sports":
- return Math.max(0, Math.round(100 - (years * 9) + ((double) kilometers / 2000) * 18 * 100.0) / 100.0);
- default:
- System.out.printf("Invalid car type: %s.%n", type);
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement