2016-11-05 69 views
-2

我想问以下问题的帮助。我试图用Java构建一个简单的银行系统。这个想法是用一个活期帐户创建新客户。打开客户后,也可以创建储蓄账户。 客户必须提供护照身份证。通过护照ID程序检查客户是否存在于数据库中。Java银行业务计划

到目前为止,我有2类银行和客户,2表格主要和新客户形式。

问题是,当客户被创建并添加到数据库(ArrayList),如果我创建一个新的客户,并输入护照ID已经存在程序仍然返回一个错误的值。即使数据库中的护照值和新护照值相同。

下面是代码:

Bank.java

import java.util.ArrayList; 

public class Bank { 
    //variables 
    private ArrayList<Customer> customers = new ArrayList<Customer>(); //holds the customers of bank 
    private double interestRate=2.5; 
    private double chargeFee=0.5; 

    //check if the customer exist in the database by using passport ID 
    public boolean passportExists(String pID){ 
     for(Customer c : customers){ 
      if(c.getPassport() == pID){ 
       return true; 
      } 
     System.out.println(c.getPassport() + " = "+pID); 
     } 
     return false; 
    } 

    //display customers array 
    public void DisplayCustomers(){ 
     for(Customer c : customers){ 
     System.out.println("name: "+c.getName()+" , passport: "+ c.getPassport()); 
     } 
    } 

    //add new customer to the customers array 
    public void addCustomer(Customer customer) { 
     customers.add(customer); 
    } 

    //get number of customers stored in the customers array 
    public int getNumberOfCustomers(){ 
     return customers.size(); 
    } 

Customer.java

import java.util.*; 

public class Customer { 
    //variables 
    private String firstName, lastName, passportID; 

//constructor 
    Customer(String cFName, String cLName, String cpID){ 
     firstName=cFName; 
     lastName = cLName; 
     passportID = cpID; 
    } 

//get functions 
public String getName() { return firstName+" "+lastName; } 
public String getPassport() { return passportID; } 

} 

主要形式

import java.util.*; 
import javax.swing.*; 

public class main extends javax.swing.JFrame { 
    private Bank bank; 

    public main() { 
     initComponents(); 
     setLocationRelativeTo(null); 
     bank = new Bank(); 
    } 

    private void jMenu2MouseClicked(java.awt.event.MouseEvent evt) {          
     newcustomerform nform = new newcustomerform(this,true,bank);  
     nform.setVisible(true); 
    } 

newcustomerform

import javax.swing.JOptionPane; 
import java.util.*; 

public class newcustomerform extends javax.swing.JDialog { 
    //declare classes and variables 
    private Bank bank; 
    private Customer customer; 

    public newcustomerform(java.awt.Frame parent, boolean modal,Bank bank) { 
     super(parent, modal); 
     initComponents(); 
     setLocationRelativeTo(parent); 
     this.bank = bank; 
    } 

private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {           
     //declare variables 
     StringBuilder warnings = new StringBuilder(); //warnings array 
     String firstName = "", lastName = "", passportID = ""; 

     //get values from the fields 
     if (fNameInput.getText().isEmpty()) { warnings.append("First Name\n");} 
     else { firstName = fNameInput.getText(); } 

     if (lNameInput.getText().isEmpty()) { warnings.append("Last Name\n");} 
     else { lastName = lNameInput.getText(); } 

     if (pIDInput.getText().isEmpty()) { warnings.append("Passport ID\n");} 
     else { passportID = pIDInput.getText(); } 

     //display warning 
     if (warnings.length() > 0) { 
      JOptionPane.showMessageDialog(this, "Required: \n"+warnings.toString(), "Input Warnings", JOptionPane.WARNING_MESSAGE); 
     } 
     else{ 
      //check if the bank has any customer 
      //if the bank has customer 
      if (bank.getNumberOfCustomers()!=0){ 
       //check if the customer exist by using passport id 
       //if customer does not exist 
       if (bank.passportExists(passportID)==false){ 
       System.out.println("does not exist"); 
        customer = new Customer(firstName, lastName, passportID); //save new customer 
        bank.addCustomer(customer); //add new customers to the customers array 
        this.dispose(); //close form    
       } 
       //if customer exist 
       else{ 
       System.out.println("exist");    
       }  
      } 
      //if the bank does not have customer 
      else{ 
       customer = new Customer(firstName, lastName, passportID); 
       bank.addCustomer(customer); 
       this.dispose(); 
      } 
      //display info 
      bank.DisplayCustomers(); 
      System.out.println("Number of customers: "+bank.getNumberOfCustomers()); 
     } 
    } 

我得到的结果是: enter image description here

这两个号码是相同的,但我得到了“假”的回应。它应该是“真实的”。

感谢您的帮助!

+3

您正在以错误的方式比较字符串。 – ItamarG3

+0

是的,Itamar Green是正确的,你的代码风格很糟糕(例如:newcustomerform),阅读http://www.oracle.com/technetwork/java/codeconventions-150003.pdf –

+0

嗨。感谢您的快速回复!我刚开始使用Java,说实话,我没有使用其他语言检查基本的东西,所有这些都使用==。 – bontoo

回答

0

问题来自您比较护照的方式。取而代之的是:

if(c.getPassport() == pID){ 
    return true; 
} 

使用本:

if(c.getPassport().equals(pID)){ 
    return true; 
} 
0

与Java equals,不==比较字符串。您应该使用if (c.getPassport().equals(pID))