Solved

Core Java. Building method that invoke another methods.

Posted on 2016-08-05
2
83 Views
Last Modified: 2016-08-08
Let say there is two classes that make search throught employee database.  ( see below )
I perform the test and in order to everything is working I need to invoice static loadDefaultEmployeesList() method in my DataBase class.

However, I don't wanna do this I would like to have opportunity to do something like this : Database.loadDefaultEmployeesList().searchByWorkExperience(3).toString()
Database.loadDefaultEmployeesList().searchSubstringOfFullName("Pa").toString()
So what should I change in my code in order that it will be possible to invoke method ( searchByWorkExperience and others )  inside method loadDefaultEmployeesList() ?
Thx in advance !
Database.loadDefaultEmployeesList(); 
       
       System.out.println("TEST I. Information about Employees who works more than 3 years. \nYour test result is : ");
       
        String expectedResult = "[Jack  Dorsy Address :USA, CA, San-Franciscostart date ( yyyy/mm ) :2008/11,"
				        		+ " Elon  Musk Address :USA, CA, Los_Angelesstart date ( yyyy/mm ) :2013/8, "
				        		+ "Pavel M. Durov Address :Russia, Saint-Petersburgstart date ( yyyy/mm ) :2012/12]";
       
       Assert.assertEquals(expectedResult, Database.searchByWorkExperience(3).toString()  );

Open in new window


class Employee { 	// Class Employee 
	 
    
	private String firstName;
    private String middleName;
    private String familyName;
    private String address;
    private int yearOfstart;
    private int monthOfstart;
   
      
    
    // Second constructor ( in case of if middle name's absents )
   protected Employee (	   
    		String firstName,
    		String familyName,
            // missing middle name of an employee
            String address,
            int yearOfstart,
            int monthOfstart) 
    {
    	this.familyName 	= familyName;
        this.firstName 		= firstName;
        this.address 		= address;
        this.yearOfstart 	= yearOfstart;
        this.monthOfstart 	= monthOfstart;
    }
    
   protected  Employee (
    		
    		String firstName,
  		  	String middleName,
  		  	String familyName,
            String address,
            int yearOfstart,
            int monthOfstart)
    {
	   this(firstName, 
			familyName, 
			// missing middle name of an employee     
			address,
			yearOfstart,
			monthOfstart) ;
	   this.middleName = middleName;
		
    }
    
    // The constructor of the class // ends 

    
    //  Implementation of getters methods  // Start 
    protected String getFirstName() {
        return firstName;
    }
    
    protected String getMiddleName() {
        if (middleName == null) {
            return "";
        } else {
            return middleName;
        }
    }
    
    protected String getFamilyName() {
        return familyName;
    }

    private String getAddress() {
        return address;
    }


    private int getYearOfStart() {
        return yearOfstart;
    }


    private int getMonthOfStart() {
        return monthOfstart;
    }
    // Implementation of getters methods //  ends  
    

    //  method for calculation of beginning of a career in a company
    
    protected int getYearsOfWork () {
        Calendar c = Calendar.getInstance();
        if ( (monthOfstart - 1) <= c.get(Calendar.MONTH) ) {
            return c.get(Calendar.YEAR) - yearOfstart;
        } else {
            return c.get(Calendar.YEAR) - yearOfstart - 1;
        }
    }

    protected void printRequestedInformation() {
    	System.out.printf ( "\nFull name : %s " ,this.getFirstName()) ;
    	System.out.printf ( "%s " ,this.getMiddleName()) ;
    	System.out.printf ( "%s " ,this.getFamilyName()) ;
    	System.out.printf ( "\nAddress : %s" ,this.getAddress()) ; 
    	System.out.printf ( "\nstart date ( yyyy/mm ) : %s" ,this.getYearOfStart()) ;
    	System.out.printf ( " / %s" ,this.getMonthOfStart()) ;
    	System.out.println();
      }
    
    protected void printFullName() {
    	System.out.printf ( "\nFull name : %s " ,this.getFirstName()) ;
    	System.out.printf ( "%s " ,this.getMiddleName()) ;
    	System.out.printf ( "%s " ,this.getFamilyName()) ;
    	System.out.println();
      }
    
   
    
    protected String getFullName() {
    	return this.getFirstName() + " " + this.getMiddleName() + " " + this.getFamilyName(); 
         }
    
    protected String getRequestedInformation() {
    	
    	return this.getFullName() + " " + "Address :" + this.getAddress() 
    	+ "start date ( yyyy/mm ) :" + this.getYearOfStart() + "/" +this.getMonthOfStart() ; 
      }

}	// Class Employers //  ends 
 
 
 
//********** ************************************************************
//********** ************************************************************
//********** ************************************************************
//********** ************************************************************
//********** ************************************************************
//********** ************************************************************
 
 
	 class Database {
		 
		 
	private static Employee employee0 =	new Employee("Jack", "Dorsy","USA, CA, San-Francisco", 2008, 11);				// index 0 
	private static Employee employee1 = 	new Employee("Marat", "Obama","USA, DC, Washington", 2016, 11);					// index 1
	private static Employee employee2 = 	new Employee("Larry", "Page","USA, CA, San-Francisco", 2015, 10);				// index 2
	private static Employee employee3 =	new Employee("Sergey ","Brin","USA, CA, San-Francisco", 2014, 10);				// index 3
	private static Employee employee4 =	new Employee("Elon", "Musk","USA, CA, Los_Angeles", 2013, 8);					// index 4	
	private static Employee employee5 =	new Employee("Pavel","M.", "Durov","Russia, Saint-Petersburg", 2012, 12);		// index 5
	private static Employee employee6 = 	new Employee("Dmitriy", "V.", "Medvedev","Russia", 2015, 10);					// index 6 
	private static Employee employee7 =	new Employee("Ivan","D.", "Shutko","Russia, Kaliningrad", 2015, 3);				// index 7
	private static Employee employee8 =	new Employee("Dmitriy","Ivanovich", "Mihalenko","Russia, Samara", 2015, 3);		// index 8
	private static Employee employee9 = 	new Employee("Barack", "Obama","USA, DC, Washington", 2016, 12);				// index 9
	private static Employee employee10 = 	new Employee("Pavel", "Erevanov","Germany, B, Berlin", 2016, 10);				// index 10
	private static Employee employee11 =	new Employee("Kirill ","Prohorov","USA, CA, San-Francisco", 2016, 10);			// index 11
	private static Employee employee12 =	new Employee("Mihail", "Bratko","USA, CA, Los_Angeles", 2016, 8);					// index 12			
	private static Employee employee13 = 	new Employee("Mihail","M.", "Erevanov","Russia, Saint-Petersburg", 2016, 1);		// index 13
	private static Employee employee14 = 	new Employee("Petor", "V.", "Kurganov","Ukrain ,Kiev", 2015, 2);					// index 14
	private static Employee employee15 =	new Employee("Pavel","D.", "Shutko","Russia, Varkuta", 2015, 3);				// index 15
	private static Employee employee16 =	new Employee("Dmitriy","P.", "Mihalenko","Russia, Kurgan", 2015, 3);			// index 16
	private static Employee employee17 =	new Employee("Larry", "Shutko","USA, CA, San-Francisco", 2016, 10);				// index 17
	private static Employee employee18 = 	new Employee("Sergey ","Brin","USA, CA, San-Francisco", 2016, 10);				// index 18
	private static Employee employee19 =	new Employee("Pavel", "Musk","USA, CA, Los_Angeles", 2016, 8);					// index 19		
	
	
		 
	  
	static private ArrayList<Employee> DatabaseOfEmployers = new ArrayList<>();
	
	protected static void addEmployeeToDataBase(Employee emp){
		DatabaseOfEmployers.add(emp);
	}
	
	protected static void  loadDefaultEmployeesList(){
	addEmployeeToDataBase(employee0);
	addEmployeeToDataBase(employee1);	
	addEmployeeToDataBase(employee2);
	addEmployeeToDataBase(employee3);
	addEmployeeToDataBase(employee4);
	addEmployeeToDataBase(employee5);
	addEmployeeToDataBase(employee6);
	addEmployeeToDataBase(employee7);
	addEmployeeToDataBase(employee8);
	addEmployeeToDataBase(employee9);
	addEmployeeToDataBase(employee10);
	addEmployeeToDataBase(employee11);
	addEmployeeToDataBase(employee12);
	addEmployeeToDataBase(employee13);
	addEmployeeToDataBase(employee14);
	addEmployeeToDataBase(employee15);
	addEmployeeToDataBase(employee16);
	addEmployeeToDataBase(employee17);
	addEmployeeToDataBase(employee18);
	addEmployeeToDataBase(employee19);
   
	}
	
	protected static ArrayList<Employee> getDataBase(){
	  return DatabaseOfEmployers;
    }
	

	
	protected static void printSearchRequest( int numberOfyears) {
		
        for (Employee item : DatabaseOfEmployers) {
            if (item.getYearsOfWork() >= numberOfyears) {           	
            	item.printRequestedInformation();
            }
        }
    }
	
	protected static ArrayList<String> searchByWorkExperience( int numberOfyears) {
		ArrayList<String> resultsOfsearch = new ArrayList<>(); 
        for (Employee item : DatabaseOfEmployers) {
            if (item.getYearsOfWork() >= numberOfyears) {           	
            	resultsOfsearch.add(item.getRequestedInformation()) ;
            }
        }
        return resultsOfsearch;
    }
	    

	
	protected static void printSearchFullName(String str){
		
        for (Employee item :DatabaseOfEmployers ) {
            if (item.getFirstName().toLowerCase().contains(str.toLowerCase()) || item.getFamilyName().toLowerCase().contains(str.toLowerCase()) ||
                    (item.getMiddleName() != null && item.getMiddleName().toLowerCase().contains(str.toLowerCase()))) {
            	item.printFullName();
            }
        }
    }
        
	   protected static ArrayList<String> searchSubstringOfFullName(String str){
        	ArrayList<String> resultsOfsearch = new ArrayList<>(); 
            for (Employee item :DatabaseOfEmployers ) {
                if (item.getFirstName().toLowerCase().contains(str.toLowerCase()) || item.getFamilyName().toLowerCase().contains(str.toLowerCase()) ||
                        (item.getMiddleName() != null && item.getMiddleName().toLowerCase().contains(str.toLowerCase()))) {
                	 resultsOfsearch.add(item.getFullName()) ;
                }
            }
            return resultsOfsearch ;
    }
	    
	}

Open in new window

0
Comment
Question by:SunnyX
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 41744363
I would like to have opportunity to do something like this : Database.loadDefaultEmployeesList().searchByWorkExperience(3).toString()
Database.loadDefaultEmployeesList().searchSubstringOfFullName("Pa").toString()
You can only chain method calls like that when each invocation returns the same type and of course it has to be appropriate to return that type. An example is StringBuilder in its append method, amongst others.

In your case, the methods you mention cannot all return the same type
1

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question