从另一个类JAVA调用验证

时间:2014-12-04 03:25:00

标签: java class validation joptionpane stringtokenizer

我正在开发一个有5个班级的项目。其中一个是验证类。我验证了控件类中的数据,但教授希望它在一个单独的类中。 这是我的代码示例。用户在令牌中输入信息。运行正常,但在控制器类中,我应该“调用”具有自己的类的验证。我该怎么编码呢?

while (tkCustomer.hasMoreTokens()){

    //store each token in the corresponding variable
    //Make sure we format integers
    //variablename = tkCustomer.nextToken();
        firstName = tkCustomer.nextToken( );
        lastName = tkCustomer.nextToken ( ); 
        phone = tkCustomer.nextToken ( );
        nbrVehicle = Integer.parseInt(tkCustomer.nextToken( ));
        nbrTanks = Integer.parseInt(tkCustomer.nextToken( ));
    }

    //validate each data entered here
        int firstNameLength = firstName.length();

        if(firstNameLength == 0){

            String errorMessage = "Please Enter a Valid First Name";
            JOptionPane.showMessageDialog(null, errorMessage, "Invalid Name", JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }else{
            int lastNameLength = lastName.length();

            if(lastNameLength == 0){

                String errorMesssage = "Please Enter a Valid Last Name";
                JOptionPane.showMessageDialog(null, errorMesssage, "Invalid Name", JOptionPane.ERROR_MESSAGE);
                System.exit(0);
        }else{
              int phoneLength = phone.length();

              if(phoneLength != 10) {
                  String errorMesssage = "Please Enter a Valid Phone Number";
                  JOptionPane.showMessageDialog(null, errorMesssage, "Invalid Phone Number", JOptionPane.ERROR_MESSAGE);
                  System.exit(0);
        }else{
            if(nbrVehicle < 1 || nbrVehicle > 10) {
                String errorMesssage = "Please Enter a Number Between 1 & 10 for Your Order of Vehicles";
                JOptionPane.showMessageDialog(null, errorMesssage, "Invalid Vehicle Order", JOptionPane.ERROR_MESSAGE);
                System.exit(0);
        }else{
             if(nbrTanks != 2 && nbrTanks != 4 && nbrTanks != 8 && nbrTanks != 10 && nbrTanks != 15 && nbrTanks != 20){

                 String errorMesssage = "Please Enter Either 2, 4, 8, 10, 15, or 20 for Your Order of Tanks";
                 JOptionPane.showMessageDialog(null, errorMesssage, "Invalid Number of Tanks", JOptionPane.ERROR_MESSAGE);
                 System.exit(0);

             }
        }
        }
        }
        }

此外,一旦用户选择了给予他们的特定选项,程序就会崩溃。控件类的完整代码如下。 DUring第一轮tokenizer项目程序运行正常。在第二轮之后程序崩溃,给我这个错误:

  

线程“main”java.util.NoSuchElementException中的异常       at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)       在project3example.HayloFactoryController.main(HayloFactoryController.java:126)

我已尝试过“while has more token”方法,但只是将程序发送到无限循环,只能使用停止按钮终止。

这是控件类的完整代码:

package project3example;

import java.util.StringTokenizer;

导入javax.swing.JOptionPane;

公共类HayloFactoryController {

/**
 * @param args
 */
public static void main(String[] args) {

    //create variables to hold information collected from the user

    String firstName = null;
    String lastName = null;
    String phone = "";
    int nbrTanks = 0;
    int nbrVehicle = 0;
    double total = 0;

    do{

        //collect the data entered by the user in 
        //variables
    String customerMessage = "Please enter the following separated by spaces"
            + "\n\n"
            + "- Customer First Name\n"
            + "- Customer Last Name\n"
            + "- Customer Phone\n"
            + "- Number of Vehicles\n"
            + "- Number of Tanks"
            + "\n\n"
            + "Example: Homer Simpson 9094559384 5 8\n\n"
            ;

        StringTokenizer tkCustomer = new StringTokenizer(
                JOptionPane.showInputDialog(customerMessage));

    while (tkCustomer.hasMoreTokens()){

    //store each token in the corresponding variable
    //Make sure we format integers
    //variablename = tkCustomer.nextToken();
        firstName = tkCustomer.nextToken( );
        lastName = tkCustomer.nextToken ( ); 
        phone = tkCustomer.nextToken ( );
        nbrVehicle = Integer.parseInt(tkCustomer.nextToken( ));
        nbrTanks = Integer.parseInt(tkCustomer.nextToken( ));
    }

    //validate each data entered here
        int firstNameLength = firstName.length();

        if(firstNameLength == 0){

            String errorMessage = "Please Enter a Valid First Name";
            JOptionPane.showMessageDialog(null, errorMessage, "Invalid Name", JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }else{
            int lastNameLength = lastName.length();

            if(lastNameLength == 0){

                String errorMesssage = "Please Enter a Valid Last Name";
                JOptionPane.showMessageDialog(null, errorMesssage, "Invalid Name", JOptionPane.ERROR_MESSAGE);
                System.exit(0);
        }else{
              int phoneLength = phone.length();

              if(phoneLength != 10) {
                  String errorMesssage = "Please Enter a Valid Phone Number";
                  JOptionPane.showMessageDialog(null, errorMesssage, "Invalid Phone Number", JOptionPane.ERROR_MESSAGE);
                  System.exit(0);
        }else{
            if(nbrVehicle < 1 || nbrVehicle > 10) {
                String errorMesssage = "Please Enter a Number Between 1 & 10 for Your Order of Vehicles";
                JOptionPane.showMessageDialog(null, errorMesssage, "Invalid Vehicle Order", JOptionPane.ERROR_MESSAGE);
                System.exit(0);
        }else{
             if(nbrTanks != 2 && nbrTanks != 4 && nbrTanks != 8 && nbrTanks != 10 && nbrTanks != 15 && nbrTanks != 20){

                 String errorMesssage = "Please Enter Either 2, 4, 8, 10, 15, or 20 for Your Order of Tanks";
                 JOptionPane.showMessageDialog(null, errorMesssage, "Invalid Number of Tanks", JOptionPane.ERROR_MESSAGE);
                 System.exit(0);

             }
        }
        }
        }
        }




    //Create a customer Object
    //CustomerObject variableName = new CustomerObject(arguments)
        HayloCustomer customer = new HayloCustomer (firstName, lastName, phone, nbrVehicle, nbrTanks, total);

    //Present the customer with a choice of vehicles
        String[]  choices = {"EV-EX 4", "EV-EX 6", "EV-DX 9", "EV-DX 12", "EV-SX 13"};

    int response = JOptionPane.showOptionDialog(
            null                                        // center over parent
            , "Select a Vehicle type and Number of Fuel Cells"   // message
            , "Vehicle & Fuel Cell Selection"               // title in title bar
            , JOptionPane.YES_NO_OPTION                     // Option type
            , JOptionPane.PLAIN_MESSAGE                     // messageType
            , null                                          // icon
            , choices                                       // Options
            , "APS 24"                                      // initial value
    );
    //get the selection from the customer
        StringTokenizer tkVehicle= new StringTokenizer(choices[response]);

    //populate the vehicle variables
        String vehicleType;
        int nbrCells;
        int costVehicle;
        int tankCost; 

    //while (tkVehicle.hasMoreTokens());
        vehicleType = tkVehicle.nextToken();
        nbrCells = Integer.parseInt(tkVehicle.nextToken());
        costVehicle = Integer.parseInt(tkVehicle.nextToken());
        tankCost = Integer.parseInt(tkVehicle.nextToken());

    //Create our vehicle object
    //VehicleObject variableName = new VehicleObject(arguments)
        HayloVehicle vehicleFactory = new HayloVehicle(vehicleType, nbrCells, costVehicle, tankCost);

    //Create our factory object
    //FactoryObject factoryVariableName = new FactoryObject(customerObjectVariable, vehicleObjectVariable);
        HayloFactory factory = new HayloFactory(customer, vehicleFactory);

    //ask the object to process the order
    //factoryVariableName.process();

        customer.toString();
        vehicleFactory.toString();
        factory.process();
        factory.toString();

    //write code below to display the result for each order
    JOptionPane.showMessageDialog(null, factory.getSummary());

}while (JOptionPane.showConfirmDialog(null, "Enter More Orders?") == JOptionPane.YES_OPTION);

    //write code below to display the summary for all the orders
    JOptionPane.showMessageDialog(null, HayloFactory.salesSummary()); 
}

}

非常感谢任何帮助!初学者,在线等级!

1 个答案:

答案 0 :(得分:0)

最简单的方法是在一个方法中写出一个带有验证逻辑的类,该方法将一个客户对象(填充字符串标记生成器中的字段)作为参数并将其传递给方法。验证可以在方法中完成,并指定一些错误代码/成功代码以从验证返回并显示错误或成功(如果适用)。还有很多其他的设计方法,但这可能是最快的。