使用Java以OO方式表示逻辑操作 [英] Representing Logical Operations in an OO manner with Java

查看:183
本文介绍了使用Java以OO方式表示逻辑操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的java程序将允许基于用户定义的逻辑操作过滤csv文件的内容。将从两列csv文件的内容中读取数据

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

,用户将在命令行上引用左和右提供逻辑条件操作。程序应根据逻辑操作的结果过滤csv文件的内容。

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);

这将支持evaluate方法。

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();
}

对于AND案例,我只是维护一个条件列表,列表以确保所有条件都为真。但是如何显示我代表IF THEN ELSE关键词?

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?

推荐答案

如何而不是采取这种方法,你基本上读取一个数组中 left 中的所有数字,并在不同的数组中读取 right 中的所有数字,数组到Velocity上下文,然后让用户键入一个Velocity风格如果条件和让Velocity照顾其余?

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 ?

这篇关于使用Java以OO方式表示逻辑操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆