Improve company productivity with a Business Account.Sign Up

x
?
Solved

Core Java. Building method that invoke another methods.

Posted on 2016-08-05
2
Medium Priority
?
126 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
1 Comment
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In this post we will learn different types of Android Layout and some basics of an Android App.
A solution for Fortify Path Manipulation.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

602 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