New paste Repaste Download
import java.util.*;
class Militant {
    int id;
    String name;
    String category;
    double experience;
    // Constructor
    Militant(int id, String name, String category, double experience) {
        this.id = id;
        this.name = name;
        this.category = category;
        this.experience = experience;
    }
}
public class Solution {
    // Method to search a militant by ID
    public static Militant searchById(List<Militant> militants, int id) {
        for (Militant m : militants) {
            if (m.id == id) {
                return m;
            }
        }
        return null;
    }
    // Method to find militants with less than 2 years of experience
    public static List<Militant> findLessExperience(List<Militant> militants) {
        List<Militant> result = new ArrayList<>();
        for (Militant m : militants) {
            if (m.experience < 2) {
                result.add(m);
            }
        }
        return result;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // Input: Number of militants
        int n = sc.nextInt();
        List<Militant> militants = new ArrayList<>();
        // Input: Militant details
        for (int i = 0; i < n; i++) {
            int id = sc.nextInt();
            sc.nextLine(); // Clear buffer
            String name = sc.nextLine();
            String category = sc.nextLine();
            double experience = sc.nextDouble();
            militants.add(new Militant(id, name, category, experience));
        }
        // Input: Militant ID to search
        int searchId = sc.nextInt();
        // Search by ID
        Militant found = searchById(militants, searchId);
        if (found != null) {
            System.out.println("Militant found:");
            System.out.println("ID: " + found.id);
            System.out.println("Name: " + found.name);
            System.out.println("Category: " + found.category);
            System.out.println("Experience: " + found.experience);
        } else {
            System.out.println("No militant found with the given ID.");
        }
        // Find militants with less than 2 years of experience
        List<Militant> lessExperience = findLessExperience(militants);
        if (!lessExperience.isEmpty()) {
            System.out.println("Militants with less than 2 years of experience:");
            for (Militant m : lessExperience) {
                System.out.println("ID: " + m.id + ", Name: " + m.name + ", Category: " + m.category + ", Experience: " + m.experience);
            }
        } else {
            System.out.println("No militants found with less than 2 years of experience.");
        }
        sc.close();
    }
}
Filename: None. Size: 3kb. View raw, , hex, or download this file.

This paste expires on 2024-12-23 04:53:22.187231. Pasted through web.