如何在其他方法中检查语句

时间:2019-06-11 10:40:00

标签: java

我正在使用while循环检查输入,然后重复执行直到满足相关条件(注释掉的代码)为止。但是由于我有很多输入,所以我决定为此创建一个方法,并且它可以完美地工作(未注释代码)。问题是当我想输入一个不应该大于currentnumber的{​​{1}}时。旧方法可以解决此问题,但是现在我无法将该语句传递给boughtNumber方法。我该如何进行这项工作,或者用另一种更好的方法代替它。不知道Java是否支持“断言”,我可以在这里使用它吗。

MyMethod

inputCheck

带有inputCheck方法的类

public void save() throws IOException{
        String name = "";
        String boughtNumber = "", currentNumber = "";
        String buyPrice = "", sellPrice = "";

        name = Functions.inputCheck("Unesite naziv artikla:\t\t", "string", true, false);
        boughtNumber = Functions.inputCheck("Unesite nabavnu kolicinu:\t", "int", true, false);
        currentNumber = Functions.inputCheck("Unesite trenutnu kolicinu:\t", "int", true, Integer.parseInt(currentNumber) > Integer.parseInt(boughtNumber));
        buyPrice = Functions.inputCheck("Unesite nabavnu cijenu:\t\t", "float", true, false);
        sellPrice = Functions.inputCheck("Unesite prodajnu cijenu:\t", "float", true, false);

//      while(name == null || patternString.matcher(name).matches()==false) {
//          System.out.print("Unesite naziv artikla:\t\t");
//          name = input.nextLine();            
//      }
//      while(boughtNumber == null || patternInt.matcher(boughtNumber).matches()==false) {
//          System.out.print("Unesite nabavnu kolicinu:\t");
//          boughtNumber = input.nextLine();
//      }
//      
//      while(currentNumber == null || patternInt.matcher(currentNumber).matches()==false || Integer.parseInt(currentNumber) > Integer.parseInt(boughtNumber)) {    
//          System.out.print("Unesite trenutnu kolicinu:\t");
//          currentNumber = input.nextLine();
//      }
//      
//      while(buyPrice == null || patternFloat.matcher(buyPrice).matches()==false) {
//          System.out.print("Unesite nabavnu cijenu:\t\t");
//          buyPrice = input.nextLine();
//      }
//      
//      while(sellPrice == null || patternFloat.matcher(sellPrice).matches()==false) {
//          System.out.print("Unesite prodajnu cijenu:\t");
//          sellPrice = input.nextLine();   
//      }

我可以澄清一下是否不清楚。

1 个答案:

答案 0 :(得分:2)

您可能想要研究谓词。

谓词是用于指定某个输入是否正确并可以链接的函数

例如:

final static private Pattern patternInt = Pattern.compile("[0-9]+$");

    static Predicate isNumber = input -> patternInt.matcher((String)input).matches();

    public static void main(String[] args) {

        System.out.println();

        Predicate numberOver50 = isNumber.and(input -> Integer.parseInt((String)input)>50);

        test(numberOver50,"50");
        test(numberOver50,"51");

    }
    static void test(Predicate test,String input){
        System.out.println(test.test(input));
    }

重写此Functions类,将进行以下更改:

static Scanner input = new Scanner(System.in);
    final static private Predicate<String> patternNotBlank = input -> Pattern.compile("\\S+").matcher(input).matches();
    final static private Predicate<String> patternOIB = input -> Pattern.compile("^[0-9]{11}$").matcher( input).matches();
    final static private Predicate<String> patternInt = input -> Pattern.compile("[0-9]+$").matcher( input).matches();
    final static private Predicate<String> patternString = input -> Pattern.compile("[A-Z][a-z]*").matcher(input).matches();
    final static private Predicate<String> patternFloat = input -> Pattern.compile("^[0-9]+([.]?[0-9]+|[0-9]*)").matcher(input).matches();

    public static String inputCheck(String print, String _pattern, Predicate andStatement, Predicate orStatement ) {
        Predicate<String> typeTest = patternFloat;
        if (andStatement==null){
            andStatement = input -> true;// a passthrough, always true
        }
        if (orStatement==null){
            orStatement = input -> false;// a passthrough, always true
        }

        switch(_pattern) {
            case "int":
                typeTest = patternInt;
                break;
            case "float":
                typeTest = patternFloat;
                break;
            case "string":
                typeTest = patternString;
                break;
            case "oib":
                typeTest = patternOIB;
                break;
            case "notblank":
                typeTest = patternNotBlank;
                break;
        }

        String string;
        do {
            System.out.print(print);
            string = input.nextLine();
        } while (andStatement.and(typeTest.negate()).or(orStatement).test(string));

        return string;
    }

现在,您可以在声明函数时添加额外的测试:

Predicate NOEND = input -> true;
Preducate NOOR = input -> false;
...

currentNumber = Functions.inputCheck("Unesite trenutnu kolicinu:\t", "int",NOEND, input->{return Integer.parseInt((String)input)> Integer.parseInt(boughtNumber)};

请注意,函数中使用的变量必须为'final'(有效的final,但不一定与final中预定义的类型相同!)

该函数也将在编写时定义,因此,此时必须填写buyNumber,如果您将顺序更改为先获取currentNumber,然后是buyedNumber,则该顺序将无效,因为buyNumber尚未填写,并且对最终结果无效(将在方法中稍后更改!)

编辑:通过定义谓词类,不需要额外的转换。 (更改了示例代码以显示此内容。)