Java银行计划

时间:2016-11-05 18:27:47

标签: java

我想就以下问题寻求帮助。我试图用Java构建一个简单的银行系统。我们的想法是使用当前帐户创建新客户。打开客户后,也可以创建储蓄账户。 客户必须提供护照ID。使用护照ID,程序会检查客户是否存在于数据库中。

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

问题是当创建客户并将其添加到数据库(ArrayList)时,如果我创建新客户并且键入护照ID已经存在,则程序仍会返回false值。即使数据库中的护照值和新护照值相同。

以下是代码:

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

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

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

问题来自你比较护照的方式。而不是:

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

使用它:

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

答案 1 :(得分:0)

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