-1

i have a situation where i have to read xml files where i get three elements like this

2019-03-19,null,null
2016-11-30,null,null
2016-10-14,null,null
2016-09-30,null,null
2016-09-30,1,YEARS
2016-09-30,3,MONTHS
2016-09-30,4,MONTHS

I have to store all three items on some data structure and apply my logic like below I have to find the max of last item and then for that i have to find the max of second item then for that i have to find the max of first element of more than one is present .

Please suggest me some idea

Sudarshan kumar
  • 1,503
  • 4
  • 36
  • 83

2 Answers2

0

Create a single object like below that can hold all three data elements and is also capable of handling a "null" value for the quantity and term length values. You may want to have the constructor convert the String date (2019-03-19) into a real date object or you could handle that before object creation. Then add these objects to a data structure (i.e. list, etc) that you can use to manage and organize them.

public class ListElement {
    public Date date;
    public Integer qty;
    public String termLength;

    public ListElement(Date d, Integer q, String t) {
        this.date = d;
        this.qty = q;
        this.termLength = t
    }

    // getter methods
    public Date getDate() {
        return this.date;
    }

    public Integer getQty() {
        return this.qty;
    }

    public String getTermLength() {
        return this.termLength;
    }

    public toString() {
         return System.out.println(this.date + "::" + 
                                   this.qty + "::" + 
                                   this.termLength)
    }
}
mba12
  • 2,702
  • 6
  • 37
  • 56
0

You can create an enum if you have some predefined terms:

enum Term {
    AGES, YEARS, MONTHS, WEEKS, DAYS, HOURS, MINUTES, SECONDS;
}

And use it in your class with other two types as:

public class MyObjects {
    private Date date;
    private Integer quantity;
    private Term term;

    public MyObjects(Date date, Integer quantity, Term term) {
        this.date = date;
        this.quantity = quantity;
        this.term = term;
    }

    // getters, setters
}

Then define the constructor that accepts these 3 arguments and use it while processing XML file.

DimaSan
  • 12,264
  • 11
  • 65
  • 75
  • 1
    Looks like the date value is coming from the processed xml file and is not the current system date. The enum idea is great assuming the terms are a small finite set which they likely are. – mba12 Jan 19 '17 at 15:23