Solved

Core Java. Building method that invoke another methods.

Posted on 2016-08-05
2
63 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
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 214
Weekend adv creator 3 54
tomcat startup error 5 106
Crystal Reports Licensing Questions 4 37
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

830 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