虽然 运行 添加后我无法让应用程序识别银行客户,但客户存储在数组列表中
While running I can't get app to recognize bankcustomers once added, customers are stored in arraylist
MAIN CLASS -
这里是我生成 arrayList 并打开菜单以开始添加客户的地方。在我 运行 选项 1(客户添加)之后,我的应用程序卡住了。然后我 运行 选项 2 进行存款,它不记得我为选项 1 输入的信息。
package BankCustomer;
``
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.*;
/**
*
* @author Tyler
*/
public class MainClass {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException, ClassNotFoundException{
//create an arraylist for the bankcustomers
ArrayList<BankCustomer> list = new ArrayList<BankCustomer>();
//bring in necessary variables for computation
BankCustomer b;
String name, address;
int accountNumber;
double balance, dep;
String resp;
boolean done = false;
//set up the menu
while(!done){
resp = JOptionPane.showInputDialog("Choose a command from\n" +
"\t1: Add a new customer\n" +
"\t2: Make a deposit\n" +
"\t3: Make a withdrawal\n" +
"\t4: Look up a balance\n" +
"\t5: Remove a customer\n" +
"\t6: Total bank balance\n" +
"\t7: Quit" );
int cmd = Integer.parseInt(resp);
switch(cmd) {
case 1:
//add a customer
name = JOptionPane.showInputDialog("Enter the name of the customer");
address = JOptionPane.showInputDialog("Enter the customer address");
resp = JOptionPane.showInputDialog("Enter the accountNumber");
accountNumber = Integer.parseInt(resp);
resp = JOptionPane.showInputDialog("Enter the balance");
balance = Double.parseDouble(resp);
b = new BankCustomer(address, name, accountNumber, balance);
list.add(b);
break;
case 2:
//make customer deposits
name = JOptionPane.showInputDialog("Enter the name of the customer to deposit for");
resp = JOptionPane.showInputDialog("Enter the amount to deposit");
dep = Double.parseDouble(resp);
Iterator<BankCustomer> iter = list.iterator();
boolean found = false;
while(!found && iter.hasNext()){
b = iter.next();
if(b.getName().compareTo(name) == 0){
b.deposit(dep);
found = true;
JOptionPane.showMessageDialog(null, "Customer deposit complete");
}
}
if(!found){
JOptionPane.showMessageDialog(null, "No such bank customer");
}
break;
case 3:
//make customer withdrawals
name = JOptionPane.showInputDialog("Enter the name of the customer to withdraw from");
resp = JOptionPane.showInputDialog("Enter the amount to withdraw");
double with = Double.parseDouble(resp);
found = false;
iter = list.iterator();
while(!found && iter.hasNext()){
b = iter.next();
if(b.getName().compareTo(name) == 0){
b.withdrawal(with);
found = true;
JOptionPane.showMessageDialog(null, "Customer withdrawal complete");
}
}
if(!found){
JOptionPane.showMessageDialog(null, "No such bank customer");
}
break;
case 4:
//look up customers by name
name = JOptionPane.showInputDialog("Enter the name of the customer to show the balance");
found = false;
iter = list.iterator();
while(!found && iter.hasNext()){
b = iter.next();
if(b.getName().compareTo(name) == 0){
b.getBalance();
found = true;
JOptionPane.showMessageDialog(null, b.getBalance());
}
}
if(!found){
JOptionPane.showMessageDialog(null, "No such bank customer");
}
break;
case 5:
//remove customers by name
name = JOptionPane.showInputDialog("Enter the name of the customer to remove");
found = false;
iter = list.iterator();
while(!found && iter.hasNext()){
b = iter.next();
if(b.getName().compareTo(name) == 0){
iter.remove();
found = true;
}
}
if(!found){
JOptionPane.showMessageDialog(null, "No such bank customer");
}
break;
case 6:
//total balances of all customers in the bank
double total = 0.0;
iter = list.iterator();
while(iter.hasNext()){
b = iter.next();
total = total + b.getBalance();
}
JOptionPane.showMessageDialog(null, "Total balance of bank" + total);
break;
case 7:
//close the menu
done = true;
}
}
}
}
BANKCUSTOMER CLASS
package BankCustomer;
import java.io.Serializable;
import java.util.Iterator;
/**
*
* @author Tyler
*/
//introduce attributes and serialize the information
public class BankCustomer implements Serializable, Comparable<BankCustomer>{
private String name, address;
private int accountNumber;
private double balance;
//constructor with passable values
public BankCustomer(String name, String address, int accountNumber, double balance) {
this.name = name;
this.address = address;
this.accountNumber = accountNumber;
this.balance = balance;
}
//option for no argument constructor
public BankCustomer() {
this.name = "Tyler Ridings";
this.address = "unknown";
this.accountNumber = 0;
this.balance = 0.0;
}
//setters and getters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
//method for making a deposit
public void deposit(double bal) {
this.balance = this.balance + bal;
}
//method for making a withdrawal
public boolean withdrawal(double bal) {
if(this.balance > bal){
this.balance = this.balance -bal;
return true;
}
else {
return false;
}
}
//compareTo for iterator jobs
public int compareTo(BankCustomer x){
return accountNumber - x.getAccountNumber();
}
}
您在调用 BankCustomer 构造函数时混淆了参数的顺序。你称它为:
b = new BankCustomer(address, name, accountNumber, balance);
而声明是:
public BankCustomer(String name, String address, int accountNumber, double balance) { ... }
MAIN CLASS -
这里是我生成 arrayList 并打开菜单以开始添加客户的地方。在我 运行 选项 1(客户添加)之后,我的应用程序卡住了。然后我 运行 选项 2 进行存款,它不记得我为选项 1 输入的信息。
package BankCustomer;
``
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.util.*;
/**
*
* @author Tyler
*/
public class MainClass {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException, ClassNotFoundException{
//create an arraylist for the bankcustomers
ArrayList<BankCustomer> list = new ArrayList<BankCustomer>();
//bring in necessary variables for computation
BankCustomer b;
String name, address;
int accountNumber;
double balance, dep;
String resp;
boolean done = false;
//set up the menu
while(!done){
resp = JOptionPane.showInputDialog("Choose a command from\n" +
"\t1: Add a new customer\n" +
"\t2: Make a deposit\n" +
"\t3: Make a withdrawal\n" +
"\t4: Look up a balance\n" +
"\t5: Remove a customer\n" +
"\t6: Total bank balance\n" +
"\t7: Quit" );
int cmd = Integer.parseInt(resp);
switch(cmd) {
case 1:
//add a customer
name = JOptionPane.showInputDialog("Enter the name of the customer");
address = JOptionPane.showInputDialog("Enter the customer address");
resp = JOptionPane.showInputDialog("Enter the accountNumber");
accountNumber = Integer.parseInt(resp);
resp = JOptionPane.showInputDialog("Enter the balance");
balance = Double.parseDouble(resp);
b = new BankCustomer(address, name, accountNumber, balance);
list.add(b);
break;
case 2:
//make customer deposits
name = JOptionPane.showInputDialog("Enter the name of the customer to deposit for");
resp = JOptionPane.showInputDialog("Enter the amount to deposit");
dep = Double.parseDouble(resp);
Iterator<BankCustomer> iter = list.iterator();
boolean found = false;
while(!found && iter.hasNext()){
b = iter.next();
if(b.getName().compareTo(name) == 0){
b.deposit(dep);
found = true;
JOptionPane.showMessageDialog(null, "Customer deposit complete");
}
}
if(!found){
JOptionPane.showMessageDialog(null, "No such bank customer");
}
break;
case 3:
//make customer withdrawals
name = JOptionPane.showInputDialog("Enter the name of the customer to withdraw from");
resp = JOptionPane.showInputDialog("Enter the amount to withdraw");
double with = Double.parseDouble(resp);
found = false;
iter = list.iterator();
while(!found && iter.hasNext()){
b = iter.next();
if(b.getName().compareTo(name) == 0){
b.withdrawal(with);
found = true;
JOptionPane.showMessageDialog(null, "Customer withdrawal complete");
}
}
if(!found){
JOptionPane.showMessageDialog(null, "No such bank customer");
}
break;
case 4:
//look up customers by name
name = JOptionPane.showInputDialog("Enter the name of the customer to show the balance");
found = false;
iter = list.iterator();
while(!found && iter.hasNext()){
b = iter.next();
if(b.getName().compareTo(name) == 0){
b.getBalance();
found = true;
JOptionPane.showMessageDialog(null, b.getBalance());
}
}
if(!found){
JOptionPane.showMessageDialog(null, "No such bank customer");
}
break;
case 5:
//remove customers by name
name = JOptionPane.showInputDialog("Enter the name of the customer to remove");
found = false;
iter = list.iterator();
while(!found && iter.hasNext()){
b = iter.next();
if(b.getName().compareTo(name) == 0){
iter.remove();
found = true;
}
}
if(!found){
JOptionPane.showMessageDialog(null, "No such bank customer");
}
break;
case 6:
//total balances of all customers in the bank
double total = 0.0;
iter = list.iterator();
while(iter.hasNext()){
b = iter.next();
total = total + b.getBalance();
}
JOptionPane.showMessageDialog(null, "Total balance of bank" + total);
break;
case 7:
//close the menu
done = true;
}
}
}
}
BANKCUSTOMER CLASS
package BankCustomer;
import java.io.Serializable;
import java.util.Iterator;
/**
*
* @author Tyler
*/
//introduce attributes and serialize the information
public class BankCustomer implements Serializable, Comparable<BankCustomer>{
private String name, address;
private int accountNumber;
private double balance;
//constructor with passable values
public BankCustomer(String name, String address, int accountNumber, double balance) {
this.name = name;
this.address = address;
this.accountNumber = accountNumber;
this.balance = balance;
}
//option for no argument constructor
public BankCustomer() {
this.name = "Tyler Ridings";
this.address = "unknown";
this.accountNumber = 0;
this.balance = 0.0;
}
//setters and getters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
//method for making a deposit
public void deposit(double bal) {
this.balance = this.balance + bal;
}
//method for making a withdrawal
public boolean withdrawal(double bal) {
if(this.balance > bal){
this.balance = this.balance -bal;
return true;
}
else {
return false;
}
}
//compareTo for iterator jobs
public int compareTo(BankCustomer x){
return accountNumber - x.getAccountNumber();
}
}
您在调用 BankCustomer 构造函数时混淆了参数的顺序。你称它为:
b = new BankCustomer(address, name, accountNumber, balance);
而声明是:
public BankCustomer(String name, String address, int accountNumber, double balance) { ... }