2012-11-22 13 views
0

我的代码是从.txt读取行并解析这些输入。用线路输入捕捉错误/异常的好设计是什么?

输入在这种形式<operation> <category> <name> <price> <quantity> <weight> <optional field1> <optional field2>

电子有2个选项字段是它十分脆弱(F/NF)和状态它运到,杂货有1个可选字段易腐(P/NP)。这里的线的几个例子输入

insert clothing shirt 20.50 1 1

insert electronics PS3 300 1 5.2 F NM

insert groceries cabbage 2.00 5 1 NP

legalLine()方法来处理输入

class sampleClass{ 
      public static void isLegalLine(String lineInput){ 
    Scanner s = new Scanner(lineInput); 
    String operation = null; 
    String category = null; 
    String name = null; 
    float price = 0; 
    int quantity = 0; 
    float weight = 0; 
    String opt1 = null; 
    String opt2 = null; 
    try{ 
     operation = s.next(); 
     category = s.next(); 
     name = s.next(); 
     price = Float.parseFloat(s.next()); 
     quantity = Integer.parseInt(s.next()); 
     weight = Float.parseFloat(s.next()); 
     if (!operation.equalsIgnoreCase("insert") 
       && !operation.equalsIgnoreCase("search") 
       && !operation.equalsIgnoreCase("delete") 
       && !operation.equalsIgnoreCase("update") 
       && !operation.equalsIgnoreCase("print")) { 
      System.out.println("invalid operation"); 
     } 
     // more validations for variables category, name, price, quantity, weight goes here 
     if(category.equalsIgnoreCase("electronics")){ 
      try{ 
       opt1 = s.next(); 
       opt1 = s.next(); 
      }catch(Exception e){ 

      } 
     }else if(category.equalsIgnoreCase("groceries")){ 
      opt1 = s.next(); 
     } 
    }catch (Exception e){ 
     //general catch for now 
     e.getMessage(); 
    } 
} 


      public static void main(String[] args) 
     { 
       FileReader freader = new FileReader(args[0]); 
       BufferedReader bfrReader = new BufferedReader(freader); 
       isLegalLine(bfrReader.readLine()); 


       //program does calculation for total price of input 
} 

看到错误的即时通讯思想..我经历了将每个令牌设置为各自的变量的所有麻烦在isLegalLine()中,我怎样才能将这些信息传递回main()?这是处理输入线路错误的好设计吗?

+0

我期望'isLegalLine'方法返回一个布尔值(不是void)。 –

回答

1

因为你需要对它们进行处理,以及,一个很好的解决方案,我看到的是这样的:

创建三个子类的产品类:服装,电子产品,杂货

然后,让你的函数

public static Product isLegalLine(String lineInput) 

解析行后返回产品。

+0

这似乎是一个很好的解决方案。每个产品子类都有自己的'isLegalLine()'方法吗? – user133466

+0

我在想它只是一个容器,不管你关心什么:每个类别,名称,价格,数量,重量和每个产品子类的字段都有自己的字段来表示额外的信息。 isLegalLine()现在真的变成了parseLine()。每个产品都可以自己实现你打算在main()中做的任何事情。 –

+0

uhm ...即时通讯仍然不完全确定如何实现这... :( – user133466

1

我想,最好是逐行读取文件。然后,您将对每行应用正则表达式,因此您将获得参数 - 强制参数和可选参数。

正则表达式将是类似的东西:

([\w\d\.]*)\s*([\w\d\.]*)\s*([\w\d\.]*)\s*([\w\d\.]*)\s*([\w\d\.]*)\s*([\w\d\.]*)\s*([\w\d\.]*)\s*([\w\d\.]*)\s* 

在括号中的每一个表情都会对应一个参数。 的代码的示例是:

String pattern = "([\\w\\d\\.]*)\\s*([\\w\\d\\.]*)\\s*([\\w\\d\\.]*)\\s*([\\w\\d\\.]*)\\s*([\\w\\d\\.]*)\\s*([\\w\\d\\.]*)\\s*([\\w\\d\\.]*)\\s*([\\w\\d\\.]*)\\s*"; 
Pattern p = Pattern.compile(pattern); 
for(String str : strings) { 
    Matcher m = p.matcher(str); 
    m.find(); 
    if(m.matches()) { 
    String operation = m.group(1); 
    String category = m.group(2); 
    ... 
    float weight = Float.parseFloat(m.group(6)); 
    String opt1 = m.group(7); 
    String opt2 = m.group(8); 
    } 
} 

然后,你只检查可选参数是否存在(或不存在),用于操作和如果必要的话产生错误。 您可以通过返回值将任何信息传递回main(...)方法。返回值可以是任何你想要的。