0

I am trying to experiment and learn BuilderPattern,reading from json file at the same time. My goal is to create object using the data I get from the json file. This is how the json file looks like:

[{
        "firstName": "Git",
        "lastName": "Hub",
        "website": "howtodoinjava.com"
    },
    {
        "firstName": "Brian",
        "lastName": "Schultz"
    }
]

Class1: Employee Class

public class Employee {

    private String firstName; // required
    private String lastName; // required
    private String website; // optional

    //Only has setters
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    //no public constructor. So the only way to get a Employee object is through the EmployeeBuilder class.
    private Employee(EmployeeBuilder builder) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.website = website;
    }

    public static class EmployeeBuilder {

        private String firstName; // required
        private String lastName; // required
        private String website; // optional

        public EmployeeBuilder() throws IOException {
        }

        public EmployeeBuilder requiredFirstName(String firstName) {
            this.firstName = firstName;
            return this;
        }

        public EmployeeBuilder requiredLastName(String lastName) {
            this.lastName = lastName;
            return this;
        }

        public EmployeeBuilder optionalWebsite(String website) {
            this.website = website;
            return this;
        }

        //Return the finally constructed User object
        public Employee build() {
            Employee emp = new Employee(this);
            return emp;
        }

    }
}

Class2: User Class

public class User {

    String firstName;
    String lastName;
    String website;

    //Using Getters to get values from Json
    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getWebsite() {
        return website;
    }
}

3 Main class

public class Main {

    public static void main(String[] args) throws IOException {

        ObjectMapper object = new ObjectMapper();

        User[] user = object.readValue(new File("C:\\MyTemp\\jackson.json"), User[].class);
        //Employee employee= new Employee.EmployeeBuilder().requiredFirstName(person.getFirstName()).requiredLastName(person.getLastName()).optionalWebsite(person.getWebsite()).build();

        List<User> emp1 = object.readValue("C:\\MyTemp\\jackson.json", new TypeReference<List<User>>() {});
        emp1.stream().forEach(x -> System.out.println(x));

        /*for (User person : user) {
            Employee employee= new Employee.EmployeeBuilder().requiredFirstName(person.getFirstName()).build();
            System.out.println(employee);
        } */
    }
}

Problem: When I run this code all I get are the 1st names. Git and Brian. I am not getting the last names or website.

Can someone please suggest what am I missing? Thanks in advance for your time.

Sheikh Rahman
  • 895
  • 3
  • 14
  • 29
  • Is that really how your main method looks? Please make sure you post correct code. – Joakim Danielson Jul 07 '19 at 08:03
  • If you really want to learn the builder pattern then I suggest you make your builder class into a separate standalone class rather than some inner class. Right now you have messed up the Employee constructor for one thing – Joakim Danielson Jul 07 '19 at 08:08
  • Sorry but there are a lot of things that do not make sense in your code. e.g. why do you pass the `builder` to the `Employee` ctor but don't use it... in fact youre using arguments that are not even passed. Also why would you expect your code to return the lastname and website? you need to implement `toString` for that. – Jan Jul 07 '19 at 08:08
  • 1
    And for the builder-pattern use: `Effective Java` by Joshua Bloch page 10 (Item 2). It is a really good book and I really recommend you to read it! – Jan Jul 07 '19 at 08:11
  • Thanks, I have looked up a few builder pattern examples and they are different from each other. I wasn't sure which one is the correct explanation. – Sheikh Rahman Jul 07 '19 at 08:16

0 Answers0