Avatar of SunnyX
SunnyX
 asked on

Core Java. Building method that invoke another methods.

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

Java EEJava

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck