Golang Program to Get Details About the Car Owner


In this article, we point to get details about the owner of a car. The program will ask the user to input the car enlistment number, and it'll recover the related information, such as the owner's title, address, and contact data. This program can be valuable in scenarios where car ownership data has to be rapidly accessed.Here we are going to use four different methods:PromptUserInput(), ValidateInput(registrationNumber string) bool, RetrieveOwnerDetails(registrationNumber string) (string, string, string), DisplayOwnerDetails(name, address, contact string) along with examples to elaborate the concept.

Syntax

func PromptUserInput() string

The Syntax func PromptUserInput() string defines a function named PromptUserInput that takes no arguments and returns a value of type string. This function can be called to prompt the user for input and expects a string response.

func ValidateInput(registrationNumber string) bool

The Syntax func ValidateInput(registrationNumber string) bool defines a function named ValidateInput that takes a parameter registrationNumber of type string and returns a value of type bool. This function is responsible for validating the input provided as the registration number and determines whether it is valid or not, returning a boolean value indicating the validation result.

func RetrieveOwnerDetails(registrationNumber string) (string, string, string)

The Syntax func RetrieveOwnerDetails(registrationNumber string) (string, string, string) defines a function named RetrieveOwnerDetails that takes a parameter registrationNumber of type string. The function returns three values of type string, representing the owner details associated with the given registration number. These three values typically include the owner's name, address, and contact information.

func DisplayOwnerDetails(name, address, contact string)

The Syntax func DisplayOwnerDetails(name, address, contact string) defines a function named DisplayOwnerDetails that takes three parameters name, address, and contact, all of type string. This function is responsible for displaying the owner details, such as the name, address, and contact information, in a suitable format or output.

Algorithm

  • Start by defining the necessary data structures, such as a struct to represent the car owner with fields like name, address, and contact details.

  • Implement a function to prompt the user to input the car registration number and return it as a string.

  • Create a function to validate the inputted registration number by implementing the necessary validation logic, such as checking the format or querying a database.

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

  • Write a function to display the owner details received from the previous step. Format the output as desired, such as printing the owner's name, address, and contact information.

  • In the main function, call the function to prompt the user for the car registration number.

  • Invoke the validation function to ensure the inputted registration number is valid. If it is valid, call the function to retrieve the owner details. Finally, display the owner details using the display function.

Example 1

In this code, the PromptUserInput() function is defined, which prompts the user to enter the car registration number and reads the input from the standard input using a bufio.Reader. The function then returns the entered input as a string.

In the main() function, we call PromptUserInput() to obtain the registration number from the user. You can add additional code after that to process the registration number and retrieve the car owner details as required.

package main

import (
   "bufio"
   "fmt"
   "os"
)

type CarOwner struct {
   Name    string
   Address string
   Contact string
}

func PromptUserInput() string {
   fmt.Print("Enter the car registration number: ")
   reader := bufio.NewReader(os.Stdin)
   input, _ := reader.ReadString('\n')
   return input
}

func GetCarOwnerDetails(registrationNumber string) CarOwner {
   owner := CarOwner{
      Name:    "John Doe",
      Address: "123 Main Street",
      Contact: "johndoe@example.com",
   }
   return owner
}

func main() {
   registrationNumber := PromptUserInput()
   fmt.Println("Entered registration number:", registrationNumber)

   owner := GetCarOwnerDetails(registrationNumber)

   fmt.Println("Car Owner Details:")
   fmt.Println("Name:", owner.Name)
   fmt.Println("Address:", owner.Address)
   fmt.Println("Contact:", owner.Contact)
}

Output

Enter the car registration number: Entered registration number: 
Car Owner Details:
Name: John Doe
Address: 123 Main Street
Contact: johndoe@example.com

Example 2

This Go program checks if a registration number fits the desired format using a regular expression pattern, returning true if it is legitimate and false otherwise. In this code, the ValidateInput() function takes a registrationNumber string as a parameter and returns a boolean value indicating whether the input is valid or not. The code then calls the ValidateInput() function and stores the result in the isValid variable.

package main

import (
   "fmt"
   "regexp"
)

func ValidateInput(registrationNumber string) bool {
   pattern := `^[A-Z]{2}\d{2}[A-Z]{1,2}\d{4}$`
   valid := regexp.MustCompile(pattern).MatchString(registrationNumber)
   return valid
}

func main() {
   registrationNumber := "AB12CD3456" // Replace with the actual registration number

   isValid := ValidateInput(registrationNumber)

   if isValid {
      fmt.Println("Registration number is valid.")
   } else {
      fmt.Println("Invalid registration number.")
   }
}

Output

Registration number is valid.

Example 3

In the below code, the RetrieveOwnerDetails() function takes a registrationNumber string as a parameter and returns three strings - name, address, and contact, representing the car owner's details.Inside the RetrieveOwnerDetails() function, you can replace the logic with your actual implementation to retrieve the car owner details based on the registration number. In the example code, the function simply assigns placeholder values for demonstration purposes.

package main

import (
   "fmt"
)

func RetrieveOwnerDetails(registrationNumber string) (string, string, string) {
   name := "John Doe"
   address := "123 Main Street"
   contact := "+1-123-456-7890"
   return name, address, contact
}

func main() {
   registrationNumber := "AB12CD3456"

   name, address, contact := RetrieveOwnerDetails(registrationNumber)

   fmt.Println("Car Owner Details:")
   fmt.Println("Name:", name)
   fmt.Println("Address:", address)
   fmt.Println("Contact:", contact)
}

Output

Car Owner Details:
Name: John Doe
Address: 123 Main Street
Contact: +1-123-456-7890

Example

In the below code example, the DisplayOwnerDetails() function takes three strings as parameters: name, address, and contact. It is responsible for displaying the car owner details.

Inside the DisplayOwnerDetails() function, it uses fmt.Println() statements to print the car owner details, including their name, address, and contact information.

package main

import (
   "fmt"
)

func PromptUserInput() string {
   var input string
   fmt.Print("Enter the car registration number: ")
   fmt.Scanln(&input)
   return input
}

func ValidateInput(registrationNumber string) bool {
   return true
}

func RetrieveOwnerDetails(registrationNumber string) (string, string, string) {
   name := "John Doe"
   address := "123 Main Street, City"
   contact := "john.doe@example.com"
   return name, address, contact
}

func DisplayOwnerDetails(name, address, contact string) {
   fmt.Println("Car Owner Details:")
   fmt.Println("Name:", name)
   fmt.Println("Address:", address)
   fmt.Println("Contact:", contact)
}

func main() {
   registrationNumber := PromptUserInput()

   if ValidateInput(registrationNumber) {
      name, address, contact := RetrieveOwnerDetails(registrationNumber)

      DisplayOwnerDetails(name, address, contact)
   } else {
      fmt.Println("Invalid registration number!")
   }
}

Output

Enter the car registration number: Car Owner Details:
Name: John Doe
Address: 123 Main Street, City
Contact: john.doe@example.com

Conclusion

This Go program permits us to recover details about the owner  of a car by contributing the car registration number. By interfacing with a database or outside API, ready to bring the significant data and display it to the client. This program can be expanded to consolidate extra usefulness, such as joining a bigger framework for overseeing car possession records or improving the client interface for a more user-friendly involvement.

Updated on: 20-Jul-2023

25 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements