the error is in the SavingsAccount class and i have commented in the error on the correct line.
SavingsAccount
public class SavingsAccount extends Account
{
//Initiazling variables.
private int countWithdrawals;
public SavingsAccount(int aNum, String oName, double iRate, double bal)
{
//Declaring variables and calling superclass constructor.
super(aNum, oName, iRate, bal);
countWithdrawals = 0;
}
public void withdraw(int number, double amt)
{
SavingsAccount sa = accounts.get(find(number)); //error: "cannot find symbol - method find(int)"
sa.withdraw(amt);
countWithdrawals++;
if(countWithdrawals > 3)
{
sa.withdraw(30);
}
if(sa.getBalance() < 0)
{
sa.withdraw(35);
}
}
}
Bank
//Importing libraries to use its functions.
import java.text.*;
import java.util.*;
public class Bank
{
//Declaring instance variables.
private int nextAccount;
private ArrayList<Account> accounts;
private ArrayList<SavingsAccount> sAccount;
private ArrayList<CheckingAccount> cAccount;
//Initializing the instance variables.
public Bank()
{
nextAccount = 101;
accounts = new ArrayList<Account>();
// sAccount = new ArrayList<SavingsAccount>();
// cAccount = new ArrayList<CheckingAccount>();
}
//Adds a savings account to the bank with the desired input from the user of the owner's name
//and the initial balance.
public void addSavingsAccount(String name, double balan)
{
accounts.add(new SavingsAccount(nextAccount, name, 3.5, balan));
nextAccount++;
}
//Adds a checking account to the bank with the desired input from the user of the owner's name
//and the initial balance.
public void addCheckingAccount(String name, double balan)
{
accounts.add(new CheckingAccount(nextAccount, name, 1.95, balan));
nextAccount++;
}
//Goes and finds at what position the user inputed account number is in the ArrayList
//else returns -1.
public int find(int number)
{
int position = -1;
for(int i = 0; i < accounts.size(); i++)
{
if(position == -1)
{
Account acc = accounts.get(i);
if(acc.getAccountNumber() == number)
{
position = i;
}
}
}
return position;
}
//Deposits money from the balance from the user inputed bank account.
public void deposit(int number, double amt)
{
Account acc = accounts.get(find(number));
acc.deposit(amt);
}
//Withdraws money from the balannce from the user inputed bank account.
public void withdraw(int number, double amt)
{
Account acc = accounts.get(find(number));
acc.withdraw(amt);
}
//Applies the standard 3.5% interest rate to all the accounts in the bank.
public void applyInterest()
{
for(int i = 0; i < accounts.size(); i++)
{
accounts.get(i).addInterest();
}
}
//Prints all the accounts in the bank in order with all the information associated with them.
public void printAccounts()
{
for(int i = 0; i < accounts.size(); i++)
{
while(i < accounts.size())
{
accounts.get(i).print();
System.out.println();
i++;
}
}
}
}
Account
//Importing library to use its functions.
import java.text.*;
public class Account
{
//Declaring instance variables.
private int accountNumber;
private String ownersName;
private double interestRate;
protected double balance;
public Account(int aNum, String oName, double iRate, double bal)
{
//User initializing the instance variables.
accountNumber = aNum;
ownersName = oName;
interestRate = iRate;
if(bal < 0.0)
{
balance = 0.0;
}
else
{
balance = bal;
}
}
//Gets the account number.
public int getAccountNumber()
{
return accountNumber;
}
//Gets the owner's name.
public String getOwnersName()
{
return ownersName;
}
//Gets the interest rate.
public double getInterestRate()
{
return interestRate;
}
//Gets the balance.
public double getBalance()
{
return balance;
}
//User sets the owner's name.
public void setOwnersName(String newOwnersName)
{
ownersName = newOwnersName;
}
//User sets the interest rate.
public void setInterestRate(double newInterestRate)
{
interestRate = newInterestRate;
}
//User deposits an amount to the balance that the user pleases.
public void deposit(double amt)
{
if(amt > 0.0)
{
balance = balance + amt;
}
else
{
balance = balance;
}
}
//User withdraws an amount from the balance that the user pleases.
public void withdraw(double amt)
{
if(amt > 0.0)
{
balance = balance - amt;
}
else
{
balance = balance;
}
}
//Adds the interest to the balance in the account.
public void addInterest()
{
balance = balance + (balance * (interestRate / 100));
}
//Prints all the information associated with the account.
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String s = "Account Number: " + accountNumber +"\nOwners Name: " + ownersName + "\nInterest Rate: " + interestRate + "%\n" + "Balance: " + fmt.format(balance);
// System.out.println("Account Number: " + accountNumber);
// System.out.println("Owners Name: " + ownersName);
// System.out.println("Interest Rate: " + interestRate + "%");
// System.out.println("Balance: " + fmt.format(balance));
return s;
}
}
one more thing: the toString() method that is the last line in the class right above, can i write that better? is there a way to break it up so it isn't so long? i had to change that method from a print() to a toString() so i am just learning this toString() method.

New Topic/Question
Reply




MultiQuote




|