2014-12-04 3 views
0

5 개의 클래스가있는 프로젝트에서 작업 중입니다. 그 중 하나가 유효성 검사 클래스입니다. 컨트롤 클래스의 데이터를 검증했지만 교수는 별도의 클래스에서 데이터를 원합니다. 여기 내 코드 샘플입니다. 사용자가 토큰에 정보를 입력합니다. 잘 실행되지만 컨트롤러 클래스에서 자체 클래스를 가진 유효성 검사를 "호출"하기로되어 있습니다. 이 코드를 어떻게 작성해야합니까?다른 클래스의 호출 유효성 검사 JAVA

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

      } 
     } 
     } 
     } 
     } 

또한 사용자가 지정한 특정 옵션을 선택하면 프로그램이 충돌합니다. 컨트롤 클래스의 전체 코드는 다음과 같습니다. 프로그램이 잘 실행되는 토크 나이저 항목의 첫 번째 라운드를 수행하십시오. 두 번째 라운드 후 프로그램이 나에게 오류를 제공 충돌이 :

Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(StringTokenizer.java:349) at project3example.HayloFactoryController.main(HayloFactoryController.java:126)

내가 시도하는 방법 "토큰을 가지고있는 동안"하지만 그건 그냥 무한 루프에 프로그램을 전송 만 정지 버튼으로 종료 될 수 있습니다. 여기

은 컨트롤 클래스의 전체 코드입니다 :

package project3example; 

수입 java.util.StringTokenizer의;

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

은}

은 어떤 도움이 크게 감사합니다! 초보자, 라인에서 학년과 함께!

답변

0

가장 쉬운 방법은 인수로 고객 객체 (String Tokenizer에서 필드를 채 웁니다)를 메소드에 전달하는 메소드에 유효성 검증 로직을 사용하여 클래스를 작성하는 것입니다. 유효성 검사는 방법에서 수행 할 수 있으며 오류 코드/성공 코드를 지정하여 유효성 검사에서 복귀하고 해당 오류 또는 성공을 표시 할 수 있습니다. 이것을 디자인하는 다른 많은 방법들이 있지만 이것은 아마도 가장 빠를 것입니다.