Golang Program to Get Details About the Car Using Its Number Plate and Engine Number


In this article, the Golang program is designed to recover details of  a car utilizing its number plate and engine number. It allows users to input the car's data and bring important details such as owner name, registration details, and contact data. By giving  inputs, clients can rapidly get the data they require about a particular car.Here we are going to use three different methods: GetCarDetailsByPlateAndEngine, using validateinput  and using the retrievecardetails function along with examples to elaborate the concept.

Syntax

func GetCarDetailsByPlateAndEngine(plateNumber string, engineNumber string) (*CarDetails, error)

the GetCarDetailsByPlateAndEngine function takes two string parameters (plateNumber and engineNumber), and it returns a pointer to a CarDetails struct and an error.

func ValidateInput(plateNumber string, engineNumber string) error

the ValidateInput function takes two string parameters (plateNumber and engineNumber), and it returns an error. The function is responsible for validating the input values and checking if they meet certain criteria or conditions. If any validation fails, it can return an appropriate error indicating the validation failure.

func RetrieveCarDetails(plate number string, engineNumber string) (*CarDetails, error)

The *CarDetails return type suggests that the function retrieves car details and returns a pointer to a CarDetails struct. The CarDetails struct likely contains information such as the car's model, color, year, and other relevant details.

The error return type indicates that the function may encounter an error during the retrieval process. If an error occurs, the function can return an appropriate error value to indicate the failure.

Algorithm

  • Define the necessary data structures, such as a struct to represent a car with fields like number plate, engine number, make, model, owner name, and other relevant details.

  • Implement a function to prompt the user to input the car's number plate and engine number, and return them as strings.

  • Create a function to validate the inputted number plate and engine number. Implement the necessary validation logic, such as checking for the correct format or verifying against a database of registered cars.

  • Develop a function that retrieves the details of the car based on the number plate and engine number provided. This function can utilize external APIs, databases, or any other data source to fetch the car details.

  • Write a function to display the car details received from the previous step. Format the output as desired, such as printing the car's make, model, owner name, and other relevant information.

  • In the main function, call the function to prompt the user for the car's number plate and engine number.

  • Invoke the validation function to ensure the inputted details are valid. If they are valid, call the function to retrieve the car details. Finally, display the car details using the display function.

Example 1

In this code, the GetCarDetailsByPlateAndEngine function takes the plateNumber and engineNumber as parameters and returns a pointer to a CarDetails struct along with an error. The function simulates retrieving car details based on the provided plate number and engine number.

package main

import (
   "errors"
   "fmt"
)

type CarDetails struct {
   Model        string
   Color        string
   Year         int
   Registration string
   Engine       string
}

func GetCarDetailsByPlateAndEngine(plateNumber string, engineNumber string) (*CarDetails, error) {

   car := &CarDetails{
      Model:        "Tesla Model 3",
      Color:        "Red",
      Year:         2022,
      Registration: plateNumber,
      Engine:       engineNumber,
   }

   if car.Registration == "" || car.Engine == "" {
      return nil, errors.New("car details not found")
   }

   return car, nil
}

func main() {
   plateNumber := "ABC123"
   engineNumber := "XYZ789"

   carDetails, err := GetCarDetailsByPlateAndEngine(plateNumber, engineNumber)
   if err != nil {
      fmt.Println("Failed to retrieve car details:", err)
      return
   }

   fmt.Println("Car Details:")
   fmt.Println("Model:", carDetails.Model)
   fmt.Println("Color:", carDetails.Color)
   fmt.Println("Year:", carDetails.Year)
   fmt.Println("Registration:", carDetails.Registration)
   fmt.Println("Engine:", carDetails.Engine)
}

Output

Car Details:
Model: Tesla Model 3
Color: Red
Year: 2022
Registration: ABC123
Engine: XYZ789

Example 2

In the below code example, the ValidateInput function takes the plateNumber and engineNumber as parameters and returns an error. It performs input validation by checking if the plate number and engine number are empty. Additional validation logic can be added as per the requirements of the program.

package main

import (
   "errors"
   "fmt"
)

func ValidateInput(plateNumber string, engineNumber string) error {

   if plateNumber == "" {
      return errors.New("plate number is required")
   }

   if engineNumber == "" {
      return errors.New("engine number is required")
   }

   return nil
}

func main() {
   plateNumber := "ABC123"
   engineNumber := "XYZ789"

   err := ValidateInput(plateNumber, engineNumber)
   if err != nil {
      fmt.Println("Invalid input:", err)
      return
   }

   fmt.Println("Input validation successful!")
}

Output

Input validation successful!

Example 3

n the below code example, the RetrieveCarDetails function takes the plateNumber and engineNumber as parameters and returns a pointer to CarDetails struct and an error. It performs the retrieval of car details by checking if the given plate number and engine number match the details in the database or any other data source. If the details are found, it creates a new instance of the CarDetails struct and populates it with the retrieved values. If the details are not found, it returns an error indicating that the car details were not found.

package main

import (
   "errors"
   "fmt"
)

type CarDetails struct {
   PlateNumber  string
   EngineNumber string
}

func RetrieveCarDetails(plateNumber string, engineNumber string) (*CarDetails, error) {

   if plateNumber == "ABC123" && engineNumber == "XYZ789" {
      car := &CarDetails{
         PlateNumber:  plateNumber,
         EngineNumber: engineNumber,
      }
      return car, nil
   }

   return nil, errors.New("car details not found")
}

func main() {
   plateNumber := "ABC123"
   engineNumber := "XYZ789"

   carDetails, err := RetrieveCarDetails(plateNumber, engineNumber)
   if err != nil {
      fmt.Println("Error:", err)
      return
   }

   fmt.Println("Car Details:")
   fmt.Println("Plate Number:", carDetails.PlateNumber)
   fmt.Println("Engine Number:", carDetails.EngineNumber)
}

Output

Car Details:
Plate Number: ABC123
Engine Number: XYZ789

Conclusion

This program disentangles the method of recovering car subtle elements based on the number plate and motor number. By giving exact inputs, clients can get imperative data on almost a particular car, such as the owner's title and contact subtle elements. It improves proficiency and comfort when managing car-related questions or exchanges.

Updated on: 20-Jul-2023

44 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements