jeudi 14 janvier 2021

Java Switch/Case alternative to update a Map

I am trying to figure out if there is a better and cleaner way to implement the following code and replace the switch/case statements. I appreciate your help and comments.

class Student {

    public String name;
    public Map<String, String> properties;

}

class MainClass {

    public void doAction(List<Student> students) {

        for (Student student : students) {

            switch (student.name) {
            case "Bob":
                student.properties.put("key1", "value1");
                student.properties.put("key2", "value2");
                break;

            case "Sophie":
                student.properties.put("key3", "value3");
                student.properties.put("key4", "value4");
                break;

            case "Natalie":
                student.properties.put("key5", "value5");
                student.properties.put("key6", "value6");
                break;
            }
        }

    }

}

Aucun commentaire:

Enregistrer un commentaire