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());
}
}
The two numbers are the same but i got "false" response. It should be "true".
Thanks for any help!