0

I am trying to create my own personal leave tracking system, in which I will be able to calculate how many days a person what absent, and for what reason.

The data structure I want to use for this is that of Map<String, List<Map<LeaveType, Integer>>>

I've created a LeaveType enum which looks something like this:

enum

public enum LeaveType {
    casual(5), sick(5), earned(10);

    private int leave_days;

    private LeaveType(int days){
        this.leave_days = days;
    }

    public int getNoOfDays(){
        return this.leave_days;
    }
}

As you can see, the system will use three types of leaves, viz., casual, sick, and earned.

I'm extracting data off a local table I've created in MySQL. Here's the schema description:

table schema

+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| first_name  | varchar(50) | YES  |     | NULL    |       |
| second_name | varchar(50) | YES  |     | NULL    |       |
| from_date   | date        | YES  |     | NULL    |       |
| to_date     | date        | YES  |     | NULL    |       |
| reason      | varchar(15) | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+

As you can see, I will be extracting the first name, last name, the date when the leave was taken, the date till the leave was taken, and the type of leave.

The structure I'm using to extract this is the following:

EmpBean.java

import java.util.Comparator;
import java.util.Date;

public class Empbean{
    private String first_name;
    private String last_name;
    private Date fromDate;
    private Date toDate;
    private LeaveType reason;


    public Empbean(String fname, String lname, Date from, Date to, LeaveType reason){
        this.first_name = fname;
        this.last_name = lname;
        this.fromDate = from;
        this.toDate = to;
        this.reason = reason;
   }

    public String getFirst_name() {
        return first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public Date getFromDate() {
        return fromDate;
    }

    public Date getToDate() {
        return toDate;
    }

    public LeaveType getReason() {
        return reason;
    }

    @Override
    public int hashCode() {
        //use the classic 31-17 hashcode override
        int result = 17;
        result = 31 * result + first_name.hashCode();
        result = 31 * result + last_name.hashCode();
        result = 31 * result + fromDate.hashCode();
        result = 31 * result + toDate.hashCode();
        result = 31 * result + reason.hashCode();
    
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if(obj!= null && obj instanceof Empbean){
            Empbean other = (Empbean) obj;
            //this and other
            if((this.getFirst_name().equals(other.getFirst_name())&&
                (this.getLast_name().equals(other.getLast_name())))){
                return true;
            }
        }
        return false;
    }

    @Override
    public String toString() {
        return new String("First Name: "+this.getFirst_name()+", Last Name: "+this.getLast_name()+ "Leave from: "+this.getFromDate()+", Leave to: "+this.getToDate()+"+ "Reason: "+this.getReason());
    }
}

After populating the data successfully, I have a method that takes, as arguments, a String, and the list of EmpBeans. This method should:

  • Calculate the no. of days between the dates
  • Map that value to an enum
  • Create a list of all such maps
  • Map this list to the search name
  • Return the map

The return type I'm aiming for is Map<String, List<LeaveType, Integer>>.

Here's the method I'm using to do this:

code

@Override
public Map<String, List<Map<LeaveType, Integer>>> getAllLeaveRecords(
        List<Empbean> map, String name) throws NameNotFoundException{
    
        //sentinal
        boolean hit = false;
    
        //start from inner
        Map<LeaveType, Integer> innerMostData = null;
        List<Map<LeaveType, Integer>> secondLastData = null;
        Map<String, List<Map<LeaveType, Integer>>> outermostData = null;
    
        //first, iterate over the entire employee array, and find the ones which match the input
        for(int i = 0;i<map.size();i++){
            //if present
            if(map.get(i).getFirst_name().equalsIgnoreCase(name) || map.get(i).getLast_name().equalsIgnoreCase(name)){
                hit = true;
            
                //extract required data
                leave = map.get(i).getReason(); //THIS --- (1)
            
                /*
                 * calculation of no. of days
                 * 
                 * */
            
                java.util.Date from = map.get(i).getFromDate();
                java.util.Date to = map.get(i).getToDate();
                long difference = to.getTime() - from.getTime();
                int no_of_days_in_between = (int)difference/(24*60*60*1000);    //AND THIS --- (2)
            
                if(innerMostData == null){
                    innerMostData = new HashMap<LeaveType, Integer>();
                }
            
                Integer current = innerMostData.get(leave);
                if(current == null){
                    current = 0;
                }
            
                innerMostData.put(leave, current + no_of_days_in_between);
            
                if(secondLastData == null){
                    secondLastData = new ArrayList<Map<LeaveType,Integer>>();
                }
            
                secondLastData.add(innerMostData);
            
                if(outermostData == null){
                    outermostData = new HashMap<String, List<Map<LeaveType,Integer>>>();
                }
                outermostData.put(name, secondLastData);
            }
        }
    
        if(hit == false){   //if not found throughout
            throw new NameNotFoundException();
        }
    
        return outermostData;
}

The output I'm aiming for is something along the lines of this:

{SomeName=[{LeaveType1, total_days_of_leave}, {leaveType2, total_days_of_leave}, ...]}

Upon running the method on a dataset with a search name 'rahul', I'm getting the following output:

{rahul=[{sick=-7, earned=6}, {sick=-7, earned=6}, {sick=-7, earned=6}, {sick=-7, earned=6}, {sick=-7, earned=6}]}

Can someone please tell me where I'm going wrong?

1
  • Aside from the duplication, I'm not sure if you also want to know other things: (1) is the formatting relevant, and (2) are you also wondering about the negative numbers? For the latter, I suspect you have entries with to_date before from_date in your table... Sep 26, 2016 at 14:32

1 Answer 1

0

In each iteration of your loop, you add() a new element to the end of your list secondLastData. As you only create one innerMostData this will always be a reference to the same object. So essentially, the secondLastData can be left out; you should add the innerMostData directly to the outerMostData.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.