Solved

Core Java. Building method that invoke another methods.

Posted on 2016-08-05
2
41 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
Comment Utility
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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
java set up 1 44
Which is the latest version of Apache Tomcatl 3 35
Python Assistance 7 30
solarwind tftp server 2 29
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now