2016-10-05 38 views
0

我開發銀行應用驗證一個ID號碼的方法。這是一個學校任務(Java系統開發),這是我的第一年,我仍然是一個新手。我需要幫助的方法的目的是根據不同的細節驗證ID號碼。創建,根據某些具體

我已經使用這個code的一部分,但修改了它。現在,當我將ID號碼寫入文本框時,什麼也沒有發生。我正在使用卡片佈局。該方法位於「註冊」面板上。這個想法是,如果ID號碼(和密碼)是正確的,應該能夠在下一個面板上登錄。

我爲控制檯開發的代碼,但我試圖讓它與jFrame一起工作。我認爲這在瑞典是一個很常見的學校任務,我不認爲說更多的學生可能會使用我可能會在這裏得到的幫助是徒勞的。

public class BankFrame extends javax.swing.JFrame { 
    public BankFrame() {  
     initComponents();  
    } //This is the method that I have problems with: 

    private void registerBtnActionPerformed(java.awt.event.ActionEvent evt) { 
     // TODO add your handling code here: 
     Register customer = new Register(password); 
if (password.equals("")){ 
     outReg.setText("Enter a password as required."); 
    } 
} 
void CorrectId() { 

     //panel 
    } 

    public void CorrectId(String id) { 
     if (validateNumbers(id)) { 
      RegPanel.setVisible(false);   // I am using CardLayout. This method is in the RegPanel. 
      LoginPanel.setVisible(true);   //This shoul allow the user to change to the Login panel if the id is valideted by the method. 
      base.removeAll(); 
      base.add(LoginPanel); 
      revalidate(); 
      repaint(); 
     } else { 
      outReg.setText("Enter a valid ID-number before proceeding"); 
      while (true) { 
       id = (regIdIn.getText()); 
       id = id.replaceAll("-", "").trim();  // A swedish ID number looks like this 890123-0000. I have to remove - to be able to run the method validateNumbers. 

       if (id.equals("")) {      // A warning that should be written on the outReg label. 
        outReg.setText("Enter your id as required."); 
        break; 
       } 
      } 
     } 
    } 

    public boolean validateNumbers(String nm) { //new String so that "id" keeps it's values. 

     int numberSize = nm.length(); 
     if (numberSize != 10) { // Warning if the ID-number has too little or too many numbers. 
      outReg.setText("Only 10 numbers is allowed!"); 
      return false; 
     } else if (nm.matches("\\d+")) { 
      return true; 
     } else {      //Warning if there is letters in the ID-number. 
      outReg.setText("You are kidding... I hope! "); 
      return false; 
     } 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 
    } 

} 
+0

你有沒有叫correctId()的任何地方,我認爲應該在registerBtnActionPerformed – Gaur93

+0

被稱爲謝謝!我按照你的建議編輯了代碼。問題的一部分解決了,但程序一直工作到縮進。 –

+0

你還沒有在任何地方調用correctId – Gaur93

回答

0

問題已解決。感謝您的幫助!我在這裏發佈的解決方案:

Login newUser = new Login(savedId,password); 

password = (regPasswordIn.getText()); 


    if (password.equals("")){ 
    outReg.setText("Enter a password as required."); 

}else{ 
     CorrectId(); 
    } 

}

public void CorrectId() { 
    id = (regIdIn.getText()); //Ta in id 
    id = id.replaceAll("-","").trim(); 
    if (id.equals("")){ 
     outReg.setText("Enter your id as required.");   
    }else{ 
    if (validateNumbers(id)){ 

    RegPanel.setVisible(false);   
    LoginPanel.setVisible(true); 
    base.removeAll(); 
    base.add(LoginPanel); 
    revalidate(); 
    repaint(); 

    savedId=id; 




    }else{ 
     outReg.setText("Enter a valid ID-number before proceeding"); //Varningstext som även kommer upp 

    } 
    } 
} 

public boolean validateNumbers(String nm) { //ny String nm för att inte köra över id. 
    System.out.println("Hej"+nm.length());//debugg 
    boolean returnvar; 


    numberSize = nm.length(); 
    if (numberSize != 10) { // Felmeddelande ifall att fel antal siffror är ifyllda. 
     outReg.setText("Only 10 numbers is allowed!"); 
     returnvar= false;    
    } else {      
     returnvar= true; 
    } 
    return returnvar; 
}           


private void infoBtnActionPerformed(java.awt.event.ActionEvent evt) {           
    // TODO add your handling code here: 


    //Genom desssa olika Randomgeneratorer tar jag fram ett unikt kontnummer. 

    //Clearingnummer : 
while (true){ 
    Random clearingNum = new Random(); 
    int clrRange = 8999-7000; //Formel för swedbanks clearing nummer 
    int clr = clearingNum.nextInt(clrRange) + 70000; 

// Sista siffran i clearingnumret  
    Random clrDiget = new Random(); 
    int clrDrange = 9 - 1; 
    int clrD = clrDiget.nextInt(clrDrange)+1; 

//Siffrona i själva kontonumret  
    Random actNum = new Random(); 
    int actRange = 999999999-100000000; //Formel för swedbaks kontonummer 
    int act = actNum.nextInt(actRange) + 100000000; 

//Sista siffran i kontonumret  
    Random actDiget = new Random(); 
    int actDrange = 9 - 1 + 1; 
    int actD = actDiget.nextInt(actDrange) + 1; 

    String a = ""+clr; //Konverterar int från randomgeneratorerna till String. 
    String b = ""+clrD; 
    String c = ""+act; 
    String d = ""+actD; 

    String accountNumber = a + "-" +b + "," +c +"-" +d; //Sätter ihop kontonumrets olika delar i korrektformat. 
    accountNumberOut.setText(accountNumber); //Skriver ut kontinummret. 
    break; 
} 




    Customer accountNumber = new Customer(name,phone,email);   //Deklarerar nytt konto. Lagrat värden i konstruktorn! 


    firstName = (firstNameIn.getText()); 
    surName = (surNameIn.getText()); 
    name = firstName + "" + surName; 

    phone = (phoneIn.getText()); 
    email = (emailIn.getText()); 

     out.setText("Customer: " +name+ "Phone: " +phone+ "Email: " +email); 




}