我想就以下问题寻求帮助。我试图用Java构建一个简单的银行系统。这个想法是用当前帐户创建新客户。在打开客户之后,也可以创建储蓄帐户。客户必须提供护照ID。该程序使用护照ID来检查客户是否存在于数据库中。
到目前为止,我有2类“银行”和“客户”,还有2种形式的main和newcustomerform。
问题是当创建客户并将其添加到数据库(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();
}
客户.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);
}
新客户形式
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());
}
}
这两个数字是相同的,但我得到了“假”响应。它应该是“ true”。
谢谢你的帮助!
问题出在您比较护照的方式上。代替这个:
if(c.getPassport() == pID){
return true;
}
用这个:
if(c.getPassport().equals(pID)){
return true;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句