Java Banking program -


this question has answer here:

i ask following issue. im trying build simple banking system in java. idea create new customer current account. after opening customer possible create saving accounts well. customer has provide passport id. passport id program checks if customer exist in database or not.

so far have 2 classes bank , customer , 2 forms main , newcustomerform.

the problem when customer created , added database(arraylist) if create new customer , type passport id exists program still comes false value. if passport value in database , new passport value same.

here codes:

bank.java

import java.util.arraylist;  public class bank {     //variables     private arraylist<customer> customers = new arraylist<customer>(); //holds customers of bank     private double interestrate=2.5;     private double chargefee=0.5;       //check if customer exist in database 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 customers array     public void addcustomer(customer customer) {         customers.add(customer);     }      //get number of customers stored in 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 , 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 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 bank has customer             //if bank has customer            if (bank.getnumberofcustomers()!=0){                //check if customer exist using passport id                //if customer 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 customers array                     this.dispose(); //close form                            }                //if customer exist                else{                 system.out.println("exist");                             }                  }            //if bank 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 result: enter image description here

the 2 numbers same got "false" response. should "true".

thanks help!

the problem arises way compare passports. instead of this:

if(c.getpassport() == pid){     return true; } 

use this:

if(c.getpassport().equals(pid)){     return true; } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -