-2

I would like to ask help for the following issue. Im trying to build a simple banking system in Java. The idea is to create new customer with a current account. After by opening a customer is possible to create saving accounts as well. The customer has to provide passport id. With the passport id the program checks if the customer exist in the database or not.

So far i have 2 classes Bank and Customer and 2 forms main and newcustomerform.

The problem is when a customer is created and added to the database(ArrayList) if i create a new customer and type a passport id already exists the program still comes back with a false value. even if the passport value in the database and the new passport value are the same.

Here are the codes:

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

}

main form

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

i get the result: enter image description here

The two numbers are the same but i got "false" response. It should be "true".

Thanks for any help!

bontoo
  • 137
  • 1
  • 19
  • 3
    You are comparing string in the wrong way. – ItamarG3 Nov 05 '16 at 18:29
  • Yep, Itamar Green is right and your code style very bad (for example: newcustomerform), read http://www.oracle.com/technetwork/java/codeconventions-150003.pdf – Alexey Nikitenko Nov 05 '16 at 18:35
  • Hi. Thanks for the quick replies! I have just started to use Java and to be honest i did not checked the basics stuffs as im using other languages and all of them uses ==. – bontoo Nov 05 '16 at 18:42
  • I would have never thought that this small thing could be an issue. Been trying to solve this for hours. Anyway thanks again!!!!! – bontoo Nov 05 '16 at 18:44
  • For some reason i cannot mark any answer as a good solution. The site does not allow me to do that. :-( – bontoo Nov 05 '16 at 18:45

2 Answers2

0

The problem arises from the way you compare the passports. Instead of this:

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

use this:

if(c.getPassport().equals(pID)){
    return true;
}
ItamarG3
  • 4,092
  • 6
  • 31
  • 44
0

Java compares Strings with equals, not ==. You should use if (c.getPassport().equals(pID))

Mike B
  • 2,756
  • 2
  • 16
  • 28