Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Core Java. Envelop for simple data base that uses ArrayLIst container ( Enum may be )

Posted on 2016-07-27
2
Medium Priority
?
68 Views
Last Modified: 2016-07-30
I would like to make a simple project that modulate the database request - response action. The presented below code work fine. However, I would like to make some envelop for my employee database in order to avoid to place it in main function. I thought about Enum class but encounter in some compile error. Moreover, I don't even know does it possible to make Enum work in this way. If not please give me an idea what fit best for my purpose. Thanks in advance dear Experts !

package TaskCh;
import java.util.ArrayList;
import java.util.Calendar;

/**

 */

//********** ************************************************************
 class SearchEngine {

   

    public void searchRequest(ArrayList<Employee> db, int numberOfyears) {
        for (Employee item : db) {
            if (item.getYearsOfWork() >= numberOfyears) {           	
            	item.getRequestedInformation();
            }
        }
    }

}
//********** ************************************************************

//********** ************************************************************

 class Employee { 	// Class Employee // start 
	 
    private String firstName;
    private String middleName;
    private String familyName;
    private String address;
    private int yearOfstart;
    private int monthOfstart;


    // The constructor of the class // starts
    Employee (String firstName,
    		  String middleName,
    		  String familyName,
              String address,
              int yearOfstart,
              int monthOfstart) {
        this.familyName 	= familyName;
        this.firstName 		= firstName;
        this.middleName 	= middleName;
        this.address 		= address;
        this.yearOfstart 	= yearOfstart;
        this.monthOfstart 	= monthOfstart;
    }
    
    // Second constructor ( in case of if middle name's absents )
    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;
    }
    
    // The constructor of the class // ends 

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

    

    public String getAddress() {
        return address;
    }


    public int getYearOfStart() {
        return yearOfstart;
    }


    public int getMonthOfStart() {
        return monthOfstart;
    }
    
   

    // Implementation of getters methods //  ends  


    // Business logic // Start 
    
    //  method for calculation of beginning of a career in a company
    
    public 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;
        }
    }
    
    public void getRequestedInformation() {
    	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("\n\n *** next employee");
      }
    // Business logic // ends  
    
}	// Class Employers //  ends 
// ********** ************************************************************

// 	 enum EmpoyeeDataBase {
//	  
// 	ArrayList<Employee> empoyeeDataBase = new ArrayList<>();
// 	
// 	 empoyeeDataBase.add(new Employee("Jack", "Dorsy","USA, CA, San-Francisco", 2008, 11));
//     empoyeeDataBase.add(new Employee("Barack", "Obama","USA, DC, Washington", 2016, 12));
//     empoyeeDataBase.add(new Employee("Larry ", "Page","USA, CA, San-Francisco", 2015, 10));
//     empoyeeDataBase.add(new Employee("Sergey ","Brin","USA, CA, San-Francisco", 2014, 10));
//     empoyeeDataBase.add(new Employee("Elon", "Musk","USA, CA, Los_Angeles", 2013, 8));		// Works almost 3 years 
//     empoyeeDataBase.add(new Employee("Pavel","M.", "Durov","Russia, Saint-Petersburg", 2012, 12));
//     empoyeeDataBase.add(new Employee("Dmitriy", "V.", "Medvedev","Russia", 2015, 10));
//	  
//  }
 
 

public class TaskCh {
    public static void main(String[] args) {

        // ArrayList of Employee type
        ArrayList<Employee> empoyeeDataBase = new ArrayList<>();

        // Fill ArrayList
        empoyeeDataBase.add(new Employee("Jack", "Dorsy","USA, CA, San-Francisco", 2008, 11));
        empoyeeDataBase.add(new Employee("Barack", "Obama","USA, DC, Washington", 2016, 12));
        empoyeeDataBase.add(new Employee("Larry ", "Page","USA, CA, San-Francisco", 2015, 10));
        empoyeeDataBase.add(new Employee("Sergey ","Brin","USA, CA, San-Francisco", 2014, 10));
        empoyeeDataBase.add(new Employee("Elon", "Musk","USA, CA, Los_Angeles", 2013, 8));		// Works almost 3 years 
        empoyeeDataBase.add(new Employee("Pavel","M.", "Durov","Ukraine, Kiev", 2012, 12));
        empoyeeDataBase.add(new Employee("Dmitriy", "V.", "Medvedev","Ukraine , Lviv", 2015, 10));

        // For each in "a" (print -> not less than 3 years of work)
        System.out.println("Information about Employees who works more than 3 years : ");
        SearchEngine yearRequest = new SearchEngine();
        yearRequest.searchRequest(empoyeeDataBase, 3);
        
        
   
    }
}

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

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 41731556
Since you're learning still, see
http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

This will show you how enum has no connection at all with your goal - but you need to know about them anyway
1
 
LVL 9

Accepted Solution

by:
Moussa Mokhtari earned 1800 total points
ID: 41731669
I think the best way to do it is to create a class not enum
that class contains a method that when you call it from main it will add your employees for you .
package TaskCh;
import java.util.ArrayList;
import java.util.Calendar;


//********** ************************************************************
 class SearchEngine {

    public void searchRequest(ArrayList<Employee> db, int numberOfyears) {
        for (Employee item : db) {
            if (item.getYearsOfWork() >= numberOfyears) {           	
            	item.getRequestedInformation();
            }
        }
    }

}
//********** ************************************************************


//********** ************************************************************

 class Employee { 	// Class Employee // start 
	 
    private String firstName;
    private String middleName;
    private String familyName;
    private String address;
    private int yearOfstart;
    private int monthOfstart;


    // The constructor of the class // starts
    Employee (String firstName,
    		  String middleName,
    		  String familyName,
              String address,
              int yearOfstart,
              int monthOfstart) {
        this.familyName 	= familyName;
        this.firstName 		= firstName;
        this.middleName 	= middleName;
        this.address 		= address;
        this.yearOfstart 	= yearOfstart;
        this.monthOfstart 	= monthOfstart;
    }
    
    // Second constructor ( in case of if middle name's absents )
    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;
    }
    
    // The constructor of the class // ends 

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

    

    public String getAddress() {
        return address;
    }


    public int getYearOfStart() {
        return yearOfstart;
    }


    public int getMonthOfStart() {
        return monthOfstart;
    }
    
   

    // Implementation of getters methods //  ends  


    // Business logic // Start 
    
    //  method for calculation of beginning of a career in a company
    
    public 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;
        }
    }
    
    public void getRequestedInformation() {
    	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("\n\n *** next employee");
      }
    // Business logic // ends  
    

}	// Class Employers //  ends 
// ********** ************************************************************

	 class EmpoyeeDataBase {
	  
	ArrayList<Employee> empoyeeDataBase = new ArrayList<>();
	
	public void addEmployee(Employee emp){
		empoyeeDataBase.add(emp);
	}
	public void addDefaultList(){
	empoyeeDataBase.add(new Employee("Jack", "Dorsy","USA, CA, San-Francisco", 2008, 11));
    empoyeeDataBase.add(new Employee("Barack", "Obama","USA, DC, Washington", 2016, 12));
    empoyeeDataBase.add(new Employee("Larry ", "Page","USA, CA, San-Francisco", 2015, 10));
    empoyeeDataBase.add(new Employee("Sergey ","Brin","USA, CA, San-Francisco", 2014, 10));
    empoyeeDataBase.add(new Employee("Elon", "Musk","USA, CA, Los_Angeles", 2013, 8));		// Works almost 3 years 
    empoyeeDataBase.add(new Employee("Pavel","M.", "Durov","Russia, Saint-Petersburg", 2012, 12));
    empoyeeDataBase.add(new Employee("Dmitriy", "V.", "Medvedev","Russia", 2015, 10));
	}
	
	public ArrayList<Employee> getDataBase(){
	  return empoyeeDataBase;
    }
	}
 
 

public class TaskCh {
    public static void main(String[] args) {

      EmpoyeeDataBase employeedb = new EmpoyeeDataBase();
      employeedb.addDefaultList();
     
        // For each in "a" (print -> not less than 3 years of work)
       System.out.println("Information about Employees who works more than 3 years : ");
       SearchEngine yearRequest = new SearchEngine();
       yearRequest.searchRequest(employeedb.getDataBase(), 3);
        
        
   
    }
}

Open in new window

1

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

670 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