I am working out of the blue pelican java textbook and using drjava. I am currently working on the Lesson 43 Big Bucks project basically I have to use this BankAccount class:
public class BankAccount
{
public BankAccount(String nm, double amt)
{
name = nm;
balance = amt;
}
public void deposit(double dp)
{
balance = balance + dp;
}
public void withdraw(double wd)
{
balance = balance - wd;
}
public String name;
public double balance;
}
and also creat another class that will allow me to enter multiple accounts, store them in an array list and then determine which account has the largest balance. Here is my code so far:
import java.io.*;
import java.util.*; //includes ArrayList
import java.text.*; //for NumberFormat
public class BigBucks
{
public static void main(String args[])
{
NumberFormat formatter = NumberFormat.getNumberInstance( );
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
String name;
List aryLst = new ArrayList( );
do
{
Scanner kbReader = new Scanner(System.in);
System.out.print("Please enter the name to whom the account belongs. (\"Exit\" to abort)");
name = kbReader.nextLine( );
if( !name.equalsIgnoreCase("EXIT") )
{
System.out.print("Please enter the amount of the deposit. ");
double amount = kbReader.nextDouble();
System.out.println(" "); //gives an eye-pleasing blank line
BankAccount myAccount = new BankAccount(name,amount);
aryLst.add(myAccount); //add account to array list
}
}while(!name.equalsIgnoreCase("EXIT"));
//Search aryList and print out the name and amount of the largest bank account
BankAccount ba = aryLst.get(0);//get first account in the list
double maxBalance = ba.balance;
String maxName = ba.name;
for(int j = 1; j < aryLst.size( ); j++)
{
//? Step through the remaining objects and decide which one has
//largest balance (compare each balance to maxBalance)
BankAccount na = aryLst.get(j);
double nBalance = na.balance;
String nName = na.name;
if(nBalance > maxBalance)
{
maxBalance = nBalance;
maxName = nName;
}
//? Step through the remaining objects and decide which one has largest balance (compare each balance to maxBalance)
//?
}
System.out.println("The accouint with the largest balance belongs to "+ maxName + ".");
System.out.println("The amount is $" + maxBalance + ".");
}
}
However every time i compile it i get this error
Type mismatch: cannot convert from java.lang.Object to BankAccount
at lines 28 and 35. Why do i get this error and how can I fix it??? Any help is appreciated.
You're storing
BankAccount
objects in a plain list.Change that to specify a list of
BankAccount
objects.When using generics this way, it will be a compiler error to try and add anything other than a
BankAccount
to your list, but you won't have to cast objects when you access list elements.The alternative (not recommended if you're using Java 5 or later) would be to cast the objects when you access the list.
Heres what I typed to get the correct printout as stated in the project itself.
I hope this helps, I had trouble with this same project and the least I could do is help you out.
You are creating an arraylist of raw objects. Rather you should create arraylist of
BankAccount
objects. Change thisto