我已经抓住了这个哑巴,无法弄清楚为什么我的代码卡在了findAccount()。我可以创建帐户,但是当我去使用该帐户执行某些操作时,它无法“找到”,并且我无法弄清楚为什么我的生活。我查看了范围,创建帐户方法,一切。任何帮助表示赞赏!这是Bank,BankAccount和Teller类。
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;
public class Bank implements InterestListener{
//new scanner
Scanner input = new Scanner(System.in);
private int balance;
private Collection<BankAccount> accounts = new ArrayList<BankAccount>();
private Collection<InterestListener> interestGainingAccounts = new ArrayList<InterestListener>();
private double rate;
Bank(){
}
Collection<BankAccount> getAccounts() throws AccountNotFoundException{
if (accounts != null){
return accounts;
}
else{
AccountNotFoundException anfe = new AccountNotFoundException("Account not found");
throw anfe;
}
}
/*If there is not already an account with the specified name, then create a new instance of
BankAccount. Dont forget to add the new instance to the collection of accounts.*/
public BankAccount createAccount (String accountName) throws AccountNotFoundException {
System.out.println("What would you like to name your new account?");
BankAccount newAccount = new BankAccount(input.nextLine());
accounts.add(newAccount);
System.out.println(newAccount.getAccountName() + " has created a new account");
System.out.println("Number of accounts: " + accounts.size());
for (BankAccount account : accounts){
if (newAccount.getAccountName() != null && newAccount.getAccountName().equals(accountName)){
System.out.println("Account name already exists");
}
}
return newAccount;
}
/*This service method will be used in several places (deposit, withdraw, inquiry and
createAccount). Implement this method first. The method should search through the collection
of accounts for a reference to a BankAccount instance with the given account name. If found,
then the reference to the located account should be returned.*/
BankAccount findAccount (String accountName) {
for (BankAccount account : accounts){
if (accounts != null && accounts.equals(accountName)){
return account;
}
}
AccountNotFoundException anfe = new AccountNotFoundException("Account not found");
return null;
}
/*This Bank method must first try to locate the specified account (see findAccount). If the search
is successful, then the deposit should occur. Otherwise, generate an appropriate error message.
Of course, you will need to do the same sort of thing when implementing withdraw, inquiry and
createAccount.*/
public void deposit (String accountName, double credit) throws AccountNotFoundException, NegativeDepositException{
findAccount(accountName).deposit(credit);
if (credit < 0){
NegativeDepositException nde= new NegativeDepositException("Cannot deposit negative credit.");
throw nde;
}
for (BankAccount account : accounts){
if (account.getBalance() >= (1000 * 100)){
postInterest(0.05);
}
AccountNotFoundException anfe = new AccountNotFoundException("Account not found");
throw anfe;
}
}
//user can withdraw from desired account, method will search for desired account and allow withdrawal, throw insufficient funds exception, or account not found exception
public void withdraw(String accountName, double debit) throws InsufficientFundsException, AccountNotFoundException{
findAccount(accountName).withdraw(debit);
for (BankAccount account : accounts) {
if (account.getBalance() < (1000 * 100)){
postInterest(0.00);
}
InsufficientFundsException ife = new InsufficientFundsException("Insufficient funds");
throw ife;
}
}
//total assets of all accounts in bank
public void getTotalAssets() throws AccountNotFoundException{
for (BankAccount account : accounts){
if (findAccount(account.getAccountName()) != null){
double total = 0;
for (int i = 0; i < accounts.size(); i++){
}
System.out.println("Total assets for all accounts found is $" + (total + findAccount(account.getAccountName()).getBalance()));
System.out.println("Number of accounts: " + accounts.size());
}
else{
System.out.println("No accounts found");
}
}
}
//report of all accounts and their balances, respectively
public Collection<BankAccount> getReport() throws AccountNotFoundException{
for (BankAccount account : accounts){
if (findAccount(account.getAccountName()) != null){
System.out.println((ArrayList<BankAccount>) accounts);
System.out.println("Number of accounts: " + accounts.size());
}
}
return accounts;
}
//add interest listener and post interest at desired user amount when balance is $1000 or greater
public void addInterestListener (InterestListener listener) throws AccountNotFoundException{
for (BankAccount account : accounts){
if (account.getBalance() >= (1000 * 100)){
rate = input.nextDouble(); //in decimal, ex. 0.05 = 5%, user will receive example in prompt
postInterest(rate);
}
}
}
//remove interest listener and post interest at 0% when balance is below $1000
public void removeInterestListener (InterestListener listener) throws AccountNotFoundException{
for (BankAccount account : accounts){
if (account.getBalance() < (1000 * 100)){
postInterest(rate);
}
}
}
//post interest to accounts that are considered interest gaining accounts
public void postInterest (double rate) throws AccountNotFoundException{
for (InterestListener listener : interestGainingAccounts) {
listener.postInterest(rate);
balance = (int) (balance * rate);
}
}
public void addInterestListener(BankAccount holder) {
addInterestListener(holder);
}
public void removeInterestListener(BankAccount holder) {
removeInterestListener(holder);
}
}
public class BankAccount implements InterestListener{
//data fields
private String accountName;
private int balance; //number of pennies, not dollars. Ex. if int balance is 100, then balance is $1.00
//convert balance in dollars(double) to cents(int) and vice versa
private static double centsToDollars (int cents){
//divide by either 0.01 or 100.0 to convert pennies to dollars
return (cents * .01);
}
private static int dollarsToCents (double dollars){
//multiply dollar by 100 then implement Math.round(double)
return ((int)Math.round(dollars * 100.0));
}
//constructor
public BankAccount (){
}
//get account name and balance methods
public String getAccountName() throws AccountNotFoundException{
if (accountName != null){
return accountName;
}
else if (accountName == null){
throw new AccountNotFoundException("Account not found");
}
else{
return accountName;
}
}
public double getBalance(){
return centsToDollars(balance);
}
double getInterestRate(double rate){
return rate;
}
public void setInterestRate(double rate){
balance = balance + (int) (balance * rate);
}
//deposit method
public void deposit (double credit) throws NegativeDepositException{
balance = balance + dollarsToCents(credit);
}
//withdraw method throws insufficient funds exception
public void withdraw (double debit) throws InsufficientFundsException{
int debitInCents = dollarsToCents(debit);
if (debitInCents <= balance) {
balance = (balance - debitInCents);
}
InsufficientFundsException ife = new InsufficientFundsException("Insufficient funds");
throw ife;
}
public BankAccount (String accountName){
this.accountName = accountName;
}
public String toString(){
try {
return String.format ("Account \"%s\" has a balance of $%.2f", getAccountName(), getBalance());
} catch (AccountNotFoundException e) {
System.out.println("Account not found.");
}
return accountName;
}
@Override
public void postInterest(double rate) throws AccountNotFoundException {
Bank bank = new Bank();
if (balance >= (1000 * 100)){
((Bank) bank.getAccounts()).addInterestListener(this);
}
if (balance < (1000 * 100)){
((Bank) bank.getAccounts()).removeInterestListener(this);
}
}
}
/*Angela Schneider
March 17,2015
*/
import java.util.Scanner;
public class Teller {
public static void main(String[] args) throws AccountNotFoundException, InsufficientFundsException, NegativeDepositException{
Teller menu = new Teller();
menu.display();
}
Bank bank = new Bank();
BankAccount bankAccount = new BankAccount();
Scanner input = new Scanner(System.in);
public void display() throws AccountNotFoundException, InsufficientFundsException, NegativeDepositException {
System.out.println("--Please enter a number from the following menu--");
System.out.println("1) Open a new account.\n" +
"2) Open existing account.\n" +
"3) Generate report of bank accounts.\n" +
"4) See total assets of all bank accounts.\n" +
"5) Exit.");
int number1 = input.nextInt();
//continue to ask for user input until user specifically asks to exit
if (number1 == 1) {
bankAccount = bank.createAccount(input.nextLine());
display2();
}
else if (number1 == 2){
bank.findAccount(bankAccount.getAccountName());
display2();
}
else if (number1 == 3){
bank.getReport();
display2();
}
else if (number1 == 4){
bank.getTotalAssets();
display2();
}
else if (number1 == 5){
System.out.println("Goodbye.");
System.exit(0);
}
else {
System.out.println("Invalid selection.");
display();
}
}
public void display2() throws AccountNotFoundException, InsufficientFundsException, NegativeDepositException {
while (true){
//second menu
System.out.println("--Please enter a number from the following list--");
System.out.println("1) Withdraw from account\n" +
"2) Deposit to account\n" +
"3) Check account balance\n" +
"4) Choose interest rate for your account\n" +
"5) Go back\n" +
"6) Exit");
int number2 = input.nextInt();
if (number2 == 1){
//withdraw method including subtracting input from balance
System.out.println("Please enter the amount you would like to withdraw");
double debit = input.nextDouble();
bank.findAccount(bankAccount.getAccountName()).withdraw(debit);
System.out.println("Your new balance is: " + bank.findAccount(bankAccount.getAccountName()).getBalance());
}
else if (number2 == 2){
//deposit method including converting input to pennies
System.out.println("Please enter the amount would like to deposit, in a whole number.");
int depositAmount = input.nextInt();
bank.findAccount(bankAccount.getAccountName()).deposit(depositAmount);
System.out.println("Your new balance is: " + bank.findAccount(bankAccount.getAccountName()).getBalance());
}
else if (number2 == 3){
System.out.println("Your balance is " + bank.findAccount(bankAccount.getAccountName()).getBalance());
}
else if (number2 == 4){
System.out.println("Please enter your desired interest rate. Example: 0.05 is 5%. Your balance won't reflect your interest rate if you currently have less than $1000");
double rate = input.nextDouble();
bank.findAccount(bankAccount.getAccountName()).setInterestRate(rate);
System.out.println("Your new interest rate is " + bank.findAccount(bankAccount.getAccountName()).getInterestRate(rate));
}
else if (number2 == 5){
display();
}
else if (number2 == 6){
System.out.println("Goodbye.");
System.exit(0);
}
else{
System.out.println("Invalid selection.");
display2();
}
}
}
}
答案 0 :(得分:2)
在findAccounts
的循环中,您正在比较列表accounts
而不是account
实例。所以在你的findAccounts
方法中它应该是这样的
for (BankAccount account : accounts){
if (account != null && account.getAccountName().equals(accountName)){
return account;
}
}