跳到主要內容

[Mock Project] 工程師搜尋機

寫一個小系統來配對出符合條件的工程師。

工程師的屬性分別有:

  • programmingLanguages: ProgrammingLanguage[]
  • Location: String
  • Experience Year: int

public class Engineer {
    String name;
    ProgrammingLanguage[] programmingLanguages;
    String location;
    int experienceYear;

    public Engineer(String n, ProgrammingLanguage[] lngs, String l, int y) {
        name = n;
        programmingLanguages = lngs;
        location = l;
        experienceYear = y;
    }
}

準備一個過濾器的介面


import java.util.List;

@FunctionalInterface
public interface EngineerMatcher<X extends Engineer> {
    List<X> match(List<X> engineers);
}

資料庫跟過濾器們


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class PlayfulEngineer {
    static List<Engineer> engineers = new ArrayList<>();

    static {
        Engineer ravi = new Engineer("Ravi",
                new ProgrammingLanguage[]{ProgrammingLanguage.Java, ProgrammingLanguage.Ruby},
                "Taipei", 5);
        Engineer renee = new Engineer("Renee",
                new ProgrammingLanguage[]{ProgrammingLanguage.Python, ProgrammingLanguage.Ruby},
                "Tainan", 2);
        Engineer rudy = new Engineer("Rudy",
                new ProgrammingLanguage[]{ProgrammingLanguage.Scala, ProgrammingLanguage.Golang},
                "New York", 10);
        Engineer daniel = new Engineer("Daniel",
                new ProgrammingLanguage[]{ProgrammingLanguage.Python},
                "Taipei", 1);

        engineers.add(ravi);
        engineers.add(renee);
        engineers.add(rudy);
        engineers.add(daniel);
    }

    public static void main(String[] args) {
        EngineerMatcher<Engineer> rubyMatcher = new EngineerMatcher<Engineer>() {
            @Override
            public List<Engineer> match(List<Engineer> engineers) {
                return engineers.stream()
                        .filter(e -> Arrays.stream(e.programmingLanguages).anyMatch(l -> l == ProgrammingLanguage.Ruby))
                        .collect(Collectors.toList());
            }
        };

        EngineerMatcher<Engineer> taipeiMatcher = new EngineerMatcher<Engineer>() {
            @Override
            public List<Engineer> match(List<Engineer> engineers) {
                return engineers.stream()
                        .filter(e -> e.location.equals("Taipei"))
                        .collect(Collectors.toList());
            }
        };

        System.out.println("Engineers DB:");
        engineers.stream().forEach(e -> System.out.println(e.name));
        List<Engineer> rubists = rubyMatcher.match(engineers);
        System.out.println("Rubists:");
        rubists.stream().forEach(e -> System.out.println(e.name));
        List<Engineer> rubistsAtTaipei = taipeiMatcher.match(rubists);
        System.out.println("Rubists at Taipei:");
        rubistsAtTaipei.stream().forEach(e -> System.out.println(e.name));
        List<Engineer> engineersAtTaipei = taipeiMatcher.match(engineers);
        System.out.println("Engineers at Taipei:");
        engineersAtTaipei.stream().forEach(e -> System.out.println(e.name));
    }

}

留言