Java: 搜索时未返回 ArrayList 项目
Java: ArrayList item not returning when searched for
我试图在我的主要方法中概述 ArrayList
个银行账户,调用对该列表中所有账户的搜索,如果该账户存在...存款。程序的实际规格:
设计并实现一个按以下方式执行的程序:
• 程序启动时,会创建两个银行帐户,使用姓名和
写入代码的数字;
• 然后要求用户输入帐号,然后输入金额
存入该帐户;
• 相应帐户的余额随后会相应更新—或者如果
输入了不正确的帐号会显示一条消息; 规范的搜索部分让我很不爽。
• 然后询问用户是否希望进行更多存款;
• 如果用户回答确实希望进行更多存款,则流程继续;
• 如果用户不希望进行更多存款,则两个帐户的详细信息
显示(帐号、账户名和余额)。
当我 运行 当找不到帐户时它 returns 输出的代码...显然我正在定义的帐户没有像我期望的那样被转移到 ArrayList
.
银行账户class:
import java.util.ArrayList;
public class BankAccount {
// the attributes
private String accountNumber;
private String accountName;
private double balance;
private char choice;
//Notice the static attribute
private static double interestRate;
// the methods
// the constructor
public BankAccount(String numberIn, String nameIn)
{
accountNumber = numberIn;
accountName = nameIn;
balance = 0;
}
// methods to read the attributes
public String getAccountName()
{
return accountName;
}
public String getAccountNumber()
{
return accountNumber;
}
public double getBalance()
{
return balance;
}
// methods to deposit and withdraw money
public void deposit(double amountIn)
{
balance = balance + amountIn;
}
public void withdraw(double amountIn)
{
if (amountIn > balance)
{
System.out.println("Sorry, there is an insufficient amount in your account to complete this withdrawal");
}
else
{
System.out.println("Withdrawal Successful");
balance = balance - amountIn;
}
}
public void setInterestRate(double rateIn)
{
interestRate = rateIn;
}
public double getInterestRate()
{
return interestRate;
}
public void addInterest()
{
balance = balance + (balance *interestRate)/100;
}
}
银行Class:
import java.util.ArrayList;
public class Bank {
ArrayList<BankAccount> list = new ArrayList<>();
// helper method to find the index of a specified account
private int search(String accountNumberIn)
{
for(int i = 0; i <= list.size() - 1; i++)
{
BankAccount tempAccount = list.get(i); // find the account at index i
String tempNumber = tempAccount.getAccountNumber(); // get account number
if(tempNumber.equals(accountNumberIn)) // if this is the account we are looking for
{
return i; // return the index
}
}
return -999;
}
// return the total number of items
public int getTotal()
{
return list.size();
}
// return an account with a particular account number
public BankAccount getItem(String accountNumberIn)
{
int index = search(accountNumberIn);
if(index != -999) // check that account exists
{
return list.get(index);
}
else
{
return null; // no such account
}
}
// add an item to the list
public boolean addAccount(String accountNumberIn, String nameIn)
{
if(search(accountNumberIn) == -999) // check that account does not already exist
{
list.add(new BankAccount(accountNumberIn, nameIn)); // add new account
return true;
}
return false;
}
// deposit money in a specified account
public boolean depositMoney(String accountNumberIn, double amountIn)
{
BankAccount acc = getItem(accountNumberIn);
if(acc != null)
{
acc.deposit(amountIn);
return true; // indicate success
}
else
{
return false; // indicate failure
}
}
// withdraw money from a specified account
public boolean withdrawMoney(String accountNumberIn, double amountIn)
{
BankAccount acc = getItem(accountNumberIn);
if(acc != null && acc.getBalance() >= amountIn)
{
acc.withdraw(amountIn);
return true; // indicate success
}
else
{
return false; // indicate failure
}
}
// remove an account
public boolean removeAccount(String accountNumberIn)
{
int index = search(accountNumberIn); // find index of account
if(index != -999) // if account exists account
{
list.remove(index);
return true; // remove was successful
}
else
{
return false; // remove was unsuccessful
}
}
}
最后用 main 方法测试 class:
import java.util.ArrayList;
public class BankAccountTester {
public static void main(String args[])
{
Bank myBank = new Bank();
ArrayList<BankAccount> accountList = new ArrayList<>();
accountList.add(new BankAccount("123","Susan Richards"));
accountList.add(new BankAccount("44567109","Delroy Jacobs"));
accountList.add(new BankAccount("46376205","Sumana Khan"));
char choice;
do {
System.out.println("Please enter your account number:");
String myAcc = EasyScanner.nextString();
BankAccount account = myBank.getItem(myAcc);
System.out.println("Please enter an amount to deposit: ");
double depIn = EasyScanner.nextDouble();
boolean found = myBank.depositMoney(myAcc, depIn);
if (found)
{
System.out.println("Deposit made");
} else
{
System.out.println("Invalid account number"); //this is running everytime
}
System.out.println("Would you like to deposit again? (y/n)");
choice = EasyScanner.nextChar();
} while (choice == 'y' || choice == 'Y');
System.out.println("Account Details..... \n");
for(BankAccount item : accountList)
{
System.out.println("Account number: " + item.getAccountNumber());
System.out.println("Account name: " + item.getAccountName());
System.out.println("Current balance: " + item.getBalance());
System.out.println();
}
}
}
我得到的输出如下:
Please enter your account number:
123
Please enter an amount to deposit:
10
**Invalid account number**
Would you like to deposit again? (y/n)
虽然我猜到正在搜索的ArrayList中没有包含已输入的帐户信息,但我无法弄清楚为什么没有提取帐号。
如有任何帮助,我们将不胜感激!
您必须在 myBank 中而不是在 main 中添加到 accountList。
myBank.addAccount ("123","Susan Richards"));
myBank.addAccount ("44567109","Delroy Jacobs"));
myBank.addAccount ("46376205","Sumana Khan"));
我试图在我的主要方法中概述 ArrayList
个银行账户,调用对该列表中所有账户的搜索,如果该账户存在...存款。程序的实际规格:
设计并实现一个按以下方式执行的程序:
• 程序启动时,会创建两个银行帐户,使用姓名和 写入代码的数字;
• 然后要求用户输入帐号,然后输入金额 存入该帐户;
• 相应帐户的余额随后会相应更新—或者如果 输入了不正确的帐号会显示一条消息; 规范的搜索部分让我很不爽。
• 然后询问用户是否希望进行更多存款;
• 如果用户回答确实希望进行更多存款,则流程继续;
• 如果用户不希望进行更多存款,则两个帐户的详细信息 显示(帐号、账户名和余额)。
当我 运行 当找不到帐户时它 returns 输出的代码...显然我正在定义的帐户没有像我期望的那样被转移到 ArrayList
.
银行账户class:
import java.util.ArrayList;
public class BankAccount {
// the attributes
private String accountNumber;
private String accountName;
private double balance;
private char choice;
//Notice the static attribute
private static double interestRate;
// the methods
// the constructor
public BankAccount(String numberIn, String nameIn)
{
accountNumber = numberIn;
accountName = nameIn;
balance = 0;
}
// methods to read the attributes
public String getAccountName()
{
return accountName;
}
public String getAccountNumber()
{
return accountNumber;
}
public double getBalance()
{
return balance;
}
// methods to deposit and withdraw money
public void deposit(double amountIn)
{
balance = balance + amountIn;
}
public void withdraw(double amountIn)
{
if (amountIn > balance)
{
System.out.println("Sorry, there is an insufficient amount in your account to complete this withdrawal");
}
else
{
System.out.println("Withdrawal Successful");
balance = balance - amountIn;
}
}
public void setInterestRate(double rateIn)
{
interestRate = rateIn;
}
public double getInterestRate()
{
return interestRate;
}
public void addInterest()
{
balance = balance + (balance *interestRate)/100;
}
}
银行Class:
import java.util.ArrayList;
public class Bank {
ArrayList<BankAccount> list = new ArrayList<>();
// helper method to find the index of a specified account
private int search(String accountNumberIn)
{
for(int i = 0; i <= list.size() - 1; i++)
{
BankAccount tempAccount = list.get(i); // find the account at index i
String tempNumber = tempAccount.getAccountNumber(); // get account number
if(tempNumber.equals(accountNumberIn)) // if this is the account we are looking for
{
return i; // return the index
}
}
return -999;
}
// return the total number of items
public int getTotal()
{
return list.size();
}
// return an account with a particular account number
public BankAccount getItem(String accountNumberIn)
{
int index = search(accountNumberIn);
if(index != -999) // check that account exists
{
return list.get(index);
}
else
{
return null; // no such account
}
}
// add an item to the list
public boolean addAccount(String accountNumberIn, String nameIn)
{
if(search(accountNumberIn) == -999) // check that account does not already exist
{
list.add(new BankAccount(accountNumberIn, nameIn)); // add new account
return true;
}
return false;
}
// deposit money in a specified account
public boolean depositMoney(String accountNumberIn, double amountIn)
{
BankAccount acc = getItem(accountNumberIn);
if(acc != null)
{
acc.deposit(amountIn);
return true; // indicate success
}
else
{
return false; // indicate failure
}
}
// withdraw money from a specified account
public boolean withdrawMoney(String accountNumberIn, double amountIn)
{
BankAccount acc = getItem(accountNumberIn);
if(acc != null && acc.getBalance() >= amountIn)
{
acc.withdraw(amountIn);
return true; // indicate success
}
else
{
return false; // indicate failure
}
}
// remove an account
public boolean removeAccount(String accountNumberIn)
{
int index = search(accountNumberIn); // find index of account
if(index != -999) // if account exists account
{
list.remove(index);
return true; // remove was successful
}
else
{
return false; // remove was unsuccessful
}
}
}
最后用 main 方法测试 class:
import java.util.ArrayList;
public class BankAccountTester {
public static void main(String args[])
{
Bank myBank = new Bank();
ArrayList<BankAccount> accountList = new ArrayList<>();
accountList.add(new BankAccount("123","Susan Richards"));
accountList.add(new BankAccount("44567109","Delroy Jacobs"));
accountList.add(new BankAccount("46376205","Sumana Khan"));
char choice;
do {
System.out.println("Please enter your account number:");
String myAcc = EasyScanner.nextString();
BankAccount account = myBank.getItem(myAcc);
System.out.println("Please enter an amount to deposit: ");
double depIn = EasyScanner.nextDouble();
boolean found = myBank.depositMoney(myAcc, depIn);
if (found)
{
System.out.println("Deposit made");
} else
{
System.out.println("Invalid account number"); //this is running everytime
}
System.out.println("Would you like to deposit again? (y/n)");
choice = EasyScanner.nextChar();
} while (choice == 'y' || choice == 'Y');
System.out.println("Account Details..... \n");
for(BankAccount item : accountList)
{
System.out.println("Account number: " + item.getAccountNumber());
System.out.println("Account name: " + item.getAccountName());
System.out.println("Current balance: " + item.getBalance());
System.out.println();
}
}
}
我得到的输出如下:
Please enter your account number:
123
Please enter an amount to deposit:
10
**Invalid account number**
Would you like to deposit again? (y/n)
虽然我猜到正在搜索的ArrayList中没有包含已输入的帐户信息,但我无法弄清楚为什么没有提取帐号。
如有任何帮助,我们将不胜感激!
您必须在 myBank 中而不是在 main 中添加到 accountList。
myBank.addAccount ("123","Susan Richards"));
myBank.addAccount ("44567109","Delroy Jacobs"));
myBank.addAccount ("46376205","Sumana Khan"));