Last active
May 8, 2022 12:05
-
-
Save DavidBernalGonzalez/aaa400939147f66b0d5e331faab9ba57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.main; | |
public class Employee { | |
// Atributes of object Employee | |
private int id_user = 0; | |
private String name = ""; | |
private String surname = ""; | |
// Constructor with All atributes | |
public Employee(int id_user, String name, String surname) { | |
this.id_user = id_user; | |
this.name = name; | |
this.surname = surname; | |
} | |
//Getters | |
public int getId_user() { | |
return id_user; | |
} | |
public String getName() { | |
return name; | |
} | |
public String getSurname() { | |
return surname; | |
} | |
//Setters | |
public void setId_user(int id_user) { | |
this.id_user = id_user; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public void setSurname(String surname) { | |
this.surname = surname; | |
} | |
@Override | |
public String toString() { | |
return "Employee [id_user=" + id_user + ", name=" + name + ", surname=" + surname + "]"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.main; | |
public class EmployeeBuilder { | |
// Atributes for the Builder of object Employee | |
private int id_user = 0; | |
private String name = ""; | |
private String surname = ""; | |
// Constructor empty | |
public EmployeeBuilder() {} | |
//Employee constructor | |
public Employee build() { | |
return new Employee(id_user, name, surname); | |
} | |
//Setters EmployeeBuilder with return this | |
public EmployeeBuilder setId_user(int id_user) { | |
this.id_user = id_user; | |
return this; | |
} | |
public EmployeeBuilder setName(String name) { | |
this.name = name; | |
return this; | |
} | |
public EmployeeBuilder setSurname(String surname) { | |
this.surname = surname; | |
return this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.main; | |
public class Main { | |
public static void main(String[] args) { | |
Employee employee = new EmployeeBuilder() | |
.setId_user(1).setName("James") | |
.setSurname("Gosling") | |
.build(); | |
System.out.println(employee.toString()); | |
Employee employee2 = new EmployeeBuilder() | |
.setId_user(2).setName("Java") | |
.setSurname("desde0.com") | |
.build(); | |
System.out.println(employee2.toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment