1

My java program will allow the content of a csv file to be filtered based on a user defined logical operation. Data will be read from the content of two column csv file

left    right
5       10
2       6

and user will provide a logical condition operation on the command line referencing left and right. The program should filter to content of the csv files based on the outcome of the logical operation.

IF ((left < right) AND (left !=10)) THEN 5 ELSE right 

I plan to represent the operation types as an enum

enum Operation
    >,
    <,
    ==,
    !=

 public boolean evaluate(int left,int right);

which would support an evaluate method. The specific logic for the operation would be implemented within the specific enum types.

A Condition interface would support methods to access the left operand, operation and right operand

interface Condition
{
    public int getLeft();
    public Operation();
    public int getRight();
}

For a AND case I would just maintain a list of Conditions and iterate over the list to ensure that all conditions are true. But how show i represent the IF THEN ELSE key words?

Liv
  • 6,006
  • 1
  • 22
  • 29
emeraldjava
  • 10,894
  • 26
  • 97
  • 170

2 Answers2

0

Following your interface convention:

interface Conditional {
    public Condition Condition();
    public int TrueValue();
    public int FalseValue();
}

I would use classes and instead of referring to ints, I would refer to an Expression class too.

Marcelo
  • 11,218
  • 1
  • 37
  • 51
0

How about rather than taking that approach you use something like Velocity? You basically read all the numbers in left in an array and all the numbers in right in a different array and pass these arrays to the Velocity context then let the user type a Velocity style if conditional and let Velocity take care of the rest ?

Liv
  • 6,006
  • 1
  • 22
  • 29