commit aa1ea154b0f0bb309b7524f5e23215d67ec0e7d3 Author: 250421 <250421@localhost> Date: Mon Jun 8 09:05:54 2026 +0100 Trabalho diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..34ca4bf --- /dev/null +++ b/pom.xml @@ -0,0 +1,13 @@ + + + 4.0.0 + com.mycompany + ProjectCarsCheckIn + 1.0-SNAPSHOT + jar + + UTF-8 + 24 + com.mycompany.projectcarscheckin.ProjectCarsCheckIn + + \ No newline at end of file diff --git a/src/main/java/com/mycompany/projectcarscheckin/ProjectCarsCheckIn.java b/src/main/java/com/mycompany/projectcarscheckin/ProjectCarsCheckIn.java new file mode 100644 index 0000000..7487805 --- /dev/null +++ b/src/main/java/com/mycompany/projectcarscheckin/ProjectCarsCheckIn.java @@ -0,0 +1,391 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + */ + + +package com.mycompany.projectcarscheckin; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; +import java.util.Scanner; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class ProjectCarsCheckIn { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + while (true) { + System.out.println("\n========== APPAREL OF VEHICLES IN THE VEHICLE FLEET =========="); + System.out.println("1 - Add a new car"); + System.out.println("2 - Show all cars"); + System.out.println("3 - Searching for a car's license plate"); + System.out.println("4 - Check the reminding about maintenance"); + System.out.println("5 - Remove vehicle"); + System.out.println("6 - Exit"); + System.out.print("Choose an option: "); + + int option = scanner.nextInt(); + scanner.nextLine(); + + switch (option) { + case 1 -> addNewVehicle(); + case 2 -> showAllVehicles(); + case 3 -> searchCarByLicensePlate(); + case 4 -> checkMaintenanceTO(); + case 5 -> removeVehicle(); + case 6 -> { + System.out.println("Bye!"); + return; + } + default -> System.out.println("Option is invalid!"); + } + } + } + + // ==================== VEHICLE CLASS ==================== + + private static class Vehicle { + String brand; + String model; + String year; + String licensePlate; + String mileage; + String lastMaintenanceDate; + + Vehicle(String brand, String model, String year, String licensePlate, String mileage, String lastMaintenanceDate) { + this.brand = brand; + this.model = model; + this.year = year; + this.licensePlate = licensePlate; + this.mileage = mileage; + this.lastMaintenanceDate = lastMaintenanceDate; + } + + String toCSV() { + return brand + "," + model + "," + year + "," + licensePlate + "," + mileage + "," + lastMaintenanceDate; + } + } + + // ==================== ADD NEW VEHICLE ==================== + + private static void addNewVehicle() { + Scanner scanner = new Scanner(System.in); + + System.out.println("\n========== ADD NEW VEHICLE =========="); + System.out.print("Enter brand: "); + String brand = scanner.nextLine(); + + System.out.print("Enter model: "); + String model = scanner.nextLine(); + + System.out.print("Enter year: "); + String year = scanner.nextLine(); + + System.out.print("Enter license plate: "); + String licensePlate = scanner.nextLine().toUpperCase(); + + System.out.print("Enter mileage (km): "); + String mileage = scanner.nextLine(); + + System.out.print("Enter last maintenance date (DD.MM.YYYY): "); + String lastMaintenanceDate = scanner.nextLine(); + + Vehicle newVehicle = new Vehicle(brand, model, year, licensePlate, mileage, lastMaintenanceDate); + addTextToFile(newVehicle.toCSV() + "\n", "vehicles.txt"); + + System.out.println("\n Vehicle successfully added!"); + } + + // ==================== SHOW ALL VEHICLES ==================== + + private static void showAllVehicles() { + String fileName = "vehicles.txt"; + int numLines = getNumberOfLinesInFile(fileName); + + if (numLines == 0) { + System.out.println("The fleet is empty!"); + return; + } + + String[] brand = new String[numLines]; + String[] model = new String[numLines]; + String[] year = new String[numLines]; + String[] licensePlate = new String[numLines]; + String[] mileage = new String[numLines]; + String[] lastMaintenanceDate = new String[numLines]; + + readAllVehicles(fileName, brand, model, year, licensePlate, mileage, lastMaintenanceDate); + + System.out.println("\n================== ALL VEHICLES IN THE FLEET =================="); + for (int i = 0; i < brand.length; i++) { + System.out.println("Vehicle #" + (i + 1)); + System.out.println(" Brand: " + brand[i]); + System.out.println(" Model: " + model[i]); + System.out.println(" Year: " + year[i]); + System.out.println(" License plate: " + licensePlate[i]); + System.out.println(" Mileage: " + mileage[i] + " km"); + System.out.println(" Last maintenance date: " + lastMaintenanceDate[i]); + System.out.println("------------------------------------------------"); + } + } + + // ==================== SEARCH BY LICENSE PLATE ==================== + + private static void searchCarByLicensePlate() { + Scanner scanner = new Scanner(System.in); + System.out.print("Enter license plate to search: "); + String searchPlate = scanner.nextLine().toUpperCase(); + + String fileName = "vehicles.txt"; + int numLines = getNumberOfLinesInFile(fileName); + + if (numLines == 0) { + System.out.println("The fleet is empty!"); + return; + } + + String[] brand = new String[numLines]; + String[] model = new String[numLines]; + String[] year = new String[numLines]; + String[] licensePlate = new String[numLines]; + String[] mileage = new String[numLines]; + String[] lastMaintenanceDate = new String[numLines]; + + readAllVehicles(fileName, brand, model, year, licensePlate, mileage, lastMaintenanceDate); + + boolean found = false; + for (int i = 0; i < licensePlate.length; i++) { + if (licensePlate[i].equalsIgnoreCase(searchPlate)) { + System.out.println("\n========== VEHICLE FOUND =========="); + System.out.println(" Brand: " + brand[i]); + System.out.println(" Model: " + model[i]); + System.out.println(" Year: " + year[i]); + System.out.println(" License plate: " + licensePlate[i]); + System.out.println(" Mileage: " + mileage[i] + " km"); + System.out.println(" Last maintenance date: " + lastMaintenanceDate[i]); + System.out.println("====================================="); + found = true; + break; + } + } + + if (!found) { + System.out.println("Vehicle with license plate " + searchPlate + " not found!"); + } + } + + // ==================== MAINTENANCE REMINDER ==================== + + private static void checkMaintenanceTO() { + String fileName = "vehicles.txt"; + int numLines = getNumberOfLinesInFile(fileName); + + if (numLines == 0) { + System.out.println("The fleet is empty!"); + return; + } + + String[] brand = new String[numLines]; + String[] model = new String[numLines]; + String[] year = new String[numLines]; + String[] licensePlate = new String[numLines]; + String[] mileage = new String[numLines]; + String[] lastMaintenanceDate = new String[numLines]; + + readAllVehicles(fileName, brand, model, year, licensePlate, mileage, lastMaintenanceDate); + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); + LocalDate today = LocalDate.now(); + + System.out.println("\n========== MAINTENANCE REMINDER =========="); + boolean hasAlert = false; + + for (int i = 0; i < brand.length; i++) { + boolean needsMaintenance = false; + String reason = ""; + + try { + LocalDate lastMaintenance = LocalDate.parse(lastMaintenanceDate[i], formatter); + long monthsSinceMaintenance = ChronoUnit.MONTHS.between(lastMaintenance, today); + + if (monthsSinceMaintenance >= 6) { + needsMaintenance = true; + reason = monthsSinceMaintenance + " months passed (need every 6 months)"; + } + } catch (Exception e) { + // Skip date errors + } + + long currentMileage = Long.parseLong(mileage[i]); + if (currentMileage > 15000) { + needsMaintenance = true; + reason += (reason.isEmpty() ? "" : " and ") + "mileage " + currentMileage + " km (recommended every 15000 km)"; + } + + if (needsMaintenance) { + System.out.println("⚠️ Vehicle: " + brand[i] + " " + model[i]); + System.out.println(" License plate: " + licensePlate[i]); + System.out.println(" Mileage: " + mileage[i] + " km"); + System.out.println(" Last maintenance date: " + lastMaintenanceDate[i]); + System.out.println(" Reason: " + reason); + System.out.println(" ➤ Maintenance recommended!"); + System.out.println("-----------------------------------"); + hasAlert = true; + } + } + + if (!hasAlert) { + System.out.println("All vehicles are in good condition, no maintenance needed!"); + } + } + + // ==================== REMOVE VEHICLE ==================== + + private static void removeVehicle() { + Scanner scanner = new Scanner(System.in); + System.out.print("Enter license plate of vehicle to remove: "); + String plateToRemove = scanner.nextLine().toUpperCase(); + + String fileName = "vehicles.txt"; + int numLines = getNumberOfLinesInFile(fileName); + + if (numLines == 0) { + System.out.println("The fleet is empty!"); + return; + } + + String[] brand = new String[numLines]; + String[] model = new String[numLines]; + String[] year = new String[numLines]; + String[] licensePlate = new String[numLines]; + String[] mileage = new String[numLines]; + String[] lastMaintenanceDate = new String[numLines]; + + readAllVehicles(fileName, brand, model, year, licensePlate, mileage, lastMaintenanceDate); + + StringBuilder newContent = new StringBuilder(); + boolean removed = false; + + for (int i = 0; i < licensePlate.length; i++) { + if (licensePlate[i].equalsIgnoreCase(plateToRemove)) { + removed = true; + System.out.println("Removed: " + brand[i] + " " + model[i] + " (" + licensePlate[i] + ")"); + } else { + newContent.append(brand[i]).append(",") + .append(model[i]).append(",") + .append(year[i]).append(",") + .append(licensePlate[i]).append(",") + .append(mileage[i]).append(",") + .append(lastMaintenanceDate[i]).append("\n"); + } + } + + if (removed) { + writeToFile(newContent.toString(), fileName); + System.out.println(" Vehicle removed successfully!"); + } else { + System.out.println("Vehicle with license plate " + plateToRemove + " not found!"); + } + } + + // ==================== FILE OPERATION METHODS ==================== + + private static void writeToFile(String text, String fileName) { + try (FileWriter fileWriter = new FileWriter(new File(fileName))) { + BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); + bufferedWriter.write(text); + bufferedWriter.close(); + } catch (IOException ex) { + Logger.getLogger(ProjectCarsCheckIn.class.getName()).log(Level.SEVERE, null, ex); + } + } + + private static String readFile(String fileName) { + StringBuilder textRead = new StringBuilder(); + try (FileReader fileReader = new FileReader(new File(fileName)); + BufferedReader bufferedReader = new BufferedReader(fileReader)) { + String line; + while ((line = bufferedReader.readLine()) != null) { + textRead.append(line).append("\n"); + } + } catch (FileNotFoundException ex) { + writeToFile("", fileName); + } catch (IOException ex) { + Logger.getLogger(ProjectCarsCheckIn.class.getName()).log(Level.SEVERE, null, ex); + } + return textRead.toString(); + } + + private static void addTextToFile(String text, String fileName) { + String oldText = readFile(fileName); + String newText = oldText + text; + writeToFile(newText, fileName); + } + + private static int getNumberOfLinesInFile(String fileName) { + int numberOfLines = 0; + try (FileReader fileReader = new FileReader(new File(fileName)); + BufferedReader bufferedReader = new BufferedReader(fileReader)) { + while (bufferedReader.readLine() != null) { + numberOfLines++; + } + } catch (FileNotFoundException ex) { + writeToFile("", fileName); + } catch (IOException ex) { + Logger.getLogger(ProjectCarsCheckIn.class.getName()).log(Level.SEVERE, null, ex); + } + return numberOfLines; + } + + private static void readAllVehicles(String fileName, String[] brand, String[] model, + String[] year, String[] licensePlate, + String[] mileage, String[] lastMaintenanceDate) { + try (FileReader fileReader = new FileReader(new File(fileName)); + BufferedReader bufferedReader = new BufferedReader(fileReader)) { + String line; + int i = 0; + while ((line = bufferedReader.readLine()) != null && i < brand.length) { + extractDataFromCSV(brand, model, year, licensePlate, mileage, lastMaintenanceDate, i, line); + i++; + } + } catch (FileNotFoundException ex) { + writeToFile("", fileName); + } catch (IOException ex) { + Logger.getLogger(ProjectCarsCheckIn.class.getName()).log(Level.SEVERE, null, ex); + } + } + + private static void extractDataFromCSV(String[] brand, String[] model, String[] year, + String[] licensePlate, String[] mileage, + String[] lastMaintenanceDate, + int lineIndex, String line) { + int lastCommaPosition = -1; + int fieldCount = 0; + + for (int i = 0; i < line.length(); i++) { + if (line.charAt(i) == ',') { + String field = line.substring(lastCommaPosition + 1, i); + switch (fieldCount) { + case 0 -> brand[lineIndex] = field; + case 1 -> model[lineIndex] = field; + case 2 -> year[lineIndex] = field; + case 3 -> licensePlate[lineIndex] = field; + case 4 -> mileage[lineIndex] = field; + } + lastCommaPosition = i; + fieldCount++; + } + } + lastMaintenanceDate[lineIndex] = line.substring(lastCommaPosition + 1); + } +} \ No newline at end of file diff --git a/vehicles.txt b/vehicles.txt new file mode 100644 index 0000000..dec6ec7 --- /dev/null +++ b/vehicles.txt @@ -0,0 +1 @@ +Toyota,M3,2013,12345,23400,14.05.2016