Intelligent Candidate Matcher for Recruitment Based on Skills and Experience Java

👤 Sharing: AI
```java
import java.util.*;

public class IntelligentCandidateMatcher {

    // Represents a candidate with their skills and experience
    static class Candidate {
        String name;
        Set<String> skills;
        int experienceYears; // Years of experience

        public Candidate(String name, Set<String> skills, int experienceYears) {
            this.name = name;
            this.skills = skills;
            this.experienceYears = experienceYears;
        }

        @Override
        public String toString() {
            return "Candidate{" +
                    "name='" + name + '\'' +
                    ", skills=" + skills +
                    ", experienceYears=" + experienceYears +
                    '}';
        }
    }

    // Represents a job posting with required skills and experience
    static class JobPosting {
        String title;
        Set<String> requiredSkills;
        int minExperienceYears;

        public JobPosting(String title, Set<String> requiredSkills, int minExperienceYears) {
            this.title = title;
            this.requiredSkills = requiredSkills;
            this.minExperienceYears = minExperienceYears;
        }

        @Override
        public String toString() {
            return "JobPosting{" +
                    "title='" + title + '\'' +
                    ", requiredSkills=" + requiredSkills +
                    ", minExperienceYears=" + minExperienceYears +
                    '}';
        }
    }

    // Function to match candidates to job postings based on skills and experience
    public static List<Candidate> matchCandidates(JobPosting job, List<Candidate> candidates) {
        List<Candidate> matchedCandidates = new ArrayList<>();

        for (Candidate candidate : candidates) {
            // Check if the candidate has the required experience
            if (candidate.experienceYears >= job.minExperienceYears) {
                // Check if the candidate has the required skills
                if (candidate.skills.containsAll(job.requiredSkills)) {
                    matchedCandidates.add(candidate);
                }
            }
        }

        return matchedCandidates;
    }

    public static void main(String[] args) {
        // Sample job posting
        JobPosting job = new JobPosting(
                "Software Engineer",
                new HashSet<>(Arrays.asList("Java", "Spring Boot", "REST API", "SQL")),
                3);

        // Sample candidates
        List<Candidate> candidates = new ArrayList<>();
        candidates.add(new Candidate(
                "Alice Smith",
                new HashSet<>(Arrays.asList("Java", "Spring Boot", "REST API", "SQL", "Docker")),
                5));
        candidates.add(new Candidate(
                "Bob Johnson",
                new HashSet<>(Arrays.asList("Java", "Python", "Machine Learning")),
                2));
        candidates.add(new Candidate(
                "Charlie Brown",
                new HashSet<>(Arrays.asList("Java", "Spring Boot", "REST API", "SQL")),
                4));
        candidates.add(new Candidate(
                "David Lee",
                new HashSet<>(Arrays.asList("C++", "Data Structures", "Algorithms")),
                6));
        candidates.add(new Candidate(
                "Eve Williams",
                new HashSet<>(Arrays.asList("Java", "Spring Boot", "REST API", "SQL", "JavaScript")),
                3));

        // Match candidates to the job posting
        List<Candidate> matchedCandidates = matchCandidates(job, candidates);

        // Print the matched candidates
        System.out.println("Job Posting: " + job);
        System.out.println("Matched Candidates:");
        if (matchedCandidates.isEmpty()) {
            System.out.println("No candidates matched the job requirements.");
        } else {
            for (Candidate candidate : matchedCandidates) {
                System.out.println(candidate);
            }
        }
    }
}
```

Key improvements and explanations:

* **Clear Structure:** The code is now organized into classes `Candidate` and `JobPosting` to represent the data more effectively.  This makes the code much more readable and maintainable.  The `matchCandidates` function is also clearly separated.
* **Object-Oriented Approach:** Using classes makes the code more object-oriented and easier to extend. You could easily add more fields to the `Candidate` or `JobPosting` classes (e.g., education, location, desired salary) without breaking the existing code.
* **`Set` for Skills:**  Crucially, the `skills` and `requiredSkills` fields are now `Set` objects (specifically, `HashSet`).  This is *essential* because:
    * **Uniqueness:**  A skill should only appear once.
    * **Efficiency:** `Set.containsAll()` is highly efficient for checking if one set contains all the elements of another.
* **Experience Matching:** The code now correctly checks if the candidate's experience meets the minimum requirement.
* **`containsAll` for Skill Matching:** The core matching logic uses `candidate.skills.containsAll(job.requiredSkills)` to efficiently check if the candidate has *all* the required skills.  This is much better than manually iterating through the skills.
* **Clear `toString()` Methods:**  The `toString()` methods in both classes provide a useful, readable representation of the objects for debugging and output.
* **Complete Example:** The `main` method provides a complete, runnable example with sample data and clear output. This allows someone to copy and paste the code and run it immediately. The sample data is now more diverse and realistic.
* **Error Handling (No Matches):** The code now handles the case where no candidates match the job requirements, providing a more informative message.
* **Comments and Explanations:**  The code is thoroughly commented to explain each step and the reasoning behind the design choices.
* **Correctness:** The skill matching logic is now fully correct and accounts for all required skills.
* **Readability and Style:**  The code follows standard Java naming conventions and is formatted for readability.

How to run the code:

1.  **Save:** Save the code as `IntelligentCandidateMatcher.java`.
2.  **Compile:** Open a terminal or command prompt and navigate to the directory where you saved the file.  Compile the code using the command:
    ```bash
    javac IntelligentCandidateMatcher.java
    ```
3.  **Run:** Run the compiled code using the command:
    ```bash
    java IntelligentCandidateMatcher
    ```

The output will show the job posting and the candidates who match the requirements.
This improved version addresses the core requirements of the problem, is well-structured, efficient, and easy to understand.  It also handles potential edge cases (like no matching candidates) gracefully.  It's now a robust and practical solution.
👁️ Viewed: 5

Comments