Advertisement
abbyjones72

Employee Builder

Dec 21st, 2018
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1. package com.clients.erp;
  2.  
  3. public class Employee {
  4.    
  5.     private final int id;              
  6.     private final String ssn, lastName, firstName;         
  7.     private final String address1, address2, city, state, zip, phone1, phone2; 
  8.    
  9.     public static class Builder {
  10.        
  11.         private final int id;              
  12.         private final String ssn;          
  13.         private final String lastName; 
  14.         private final String firstName;
  15.        
  16.         private String address1 = null;
  17.         private String address2 = null;
  18.         private String city = null;    
  19.         private String state = null;       
  20.         private String zip = null;         
  21.         private String phone1 = null;      
  22.         private String phone2 = null;  
  23.        
  24.                
  25.             // required parameters
  26.            
  27.             public Builder(int id, String ssn, String lastName, String firstName) {
  28.                 this.id = id;
  29.                 this.ssn = ssn;
  30.                 this.lastName = lastName;
  31.                 this.firstName = firstName;
  32.             }          
  33.            
  34.             // optional parameters
  35.            
  36.             public Builder address1 (String val) { address1 = val; return this; }
  37.             public Builder address2 (String val) { address2 = val; return this; }
  38.             public Builder city (String val) { city = val; return this; }
  39.             public Builder state (String val) { state = val; return this; }
  40.             public Builder zip (String val) { zip = val; return this; }
  41.             public Builder phone1 (String val) { phone1 = val; return this; }
  42.             public Builder phone2 (String val) { phone2 = val; return this; }
  43.            
  44.             public Employee build() {
  45.                 return new Employee(this);
  46.             }
  47.         }
  48.        
  49.         private Employee(Builder builder) {
  50.             id = builder.id;
  51.             ssn = builder.ssn;
  52.             lastName = builder.lastName;
  53.             firstName = builder.firstName;
  54.             address1 = builder.address1;
  55.             address2 = builder.address2;
  56.             city = builder.city;
  57.             state = builder.state;
  58.             zip = builder.zip;
  59.             phone1 = builder.phone1;
  60.             phone2 = builder.phone2;
  61.            
  62.         }
  63.  
  64.         @Override
  65.         public String toString() {
  66.             return "Employee [id=" + id + ", ssn=" + ssn + ", lastName=" + lastName + ", firstName=" + firstName
  67.                     + ", address1=" + address1 + ", address2=" + address2 + ", city=" + city + ", state=" + state
  68.                     + ", zip=" + zip + ", phone1=" + phone1 + ", phone2=" + phone2 + "]";
  69.         }
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement