Skip to main content

Help with Java program

3 replies [Last post]
guzumba
Offline
Joined: 2010-08-29

Hi everyone I am trying to store the name of a bank account holder in another class but im stuck. I keep getting errors everytime I try to run it. Everything ran fine until I started trying to add the name feature to the program. Can someone please tell me whats wrong with my code. I know im close but I cant figure it out! Thanks everyone!

import java.util.Scanner;

class BankAccount
{
private String name; // name of account holder
private double balance; // account balance

public BankAccount(double openingBalance) // constructor
{
balance = openingBalance;
}

public void accountName(String n)
{
name = n;
}

public void deposit(double amount) // makes deposit
{
balance = balance + amount;
}

public void withdraw(double amount) // makes withdrawal
{
balance = balance - amount;
}

public void display() // displays balance
{
System.out.println("balance=" + balance);
}
} // end class BankAccount
////////////////////////////////////////////////////////////////
public class BankApp
{
public static void main(String[] args)
{
BankAccount ba1 = new BankAccount(100.00); // create acct

Scanner input = new Scanner( System.in );
// prompt user for input
System.out.printf( "Enter your name: " );
String name = input.next( );
accountName() = name;

System.out.print("Before transactions, ");
ba1.display(); // display balance

ba1.deposit(74.35); // make deposit
ba1.withdraw(20.00); // make withdrawal

System.out.print("After transactions, ");
ba1.display(); // display balance
} // end main()
} // end class BankApp

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
OneJumpedUp
Offline
Joined: 2012-04-05

He is right about the line but not the advice, here is correct code:

    public static void main(String[] args) 
    {
        BankAccount ba1 = new BankAccount(100.00); // create acct

        Scanner input = new Scanner(System.in);
        // prompt user for input
        System.out.printf("Enter your name: ");
        String name = input.next();
        ba1.accountName( name );

        System.out.print("Before transactions, ");
        ba1.display(); // display balance

        ba1.deposit(74.35); // make deposit
        ba1.withdraw(20.00); // make withdrawal

        System.out.print("After transactions, ");
        ba1.display(); // display balance
    } // end main()

public class BankAccount 
{
    private String name; // name of account holder
    private double balance; // account balance

    public BankAccount(double openingBalance) // constructor
    {
        this.balance = openingBalance;
    }

    public void accountName(String n)
    {
        this.name = n;
    }

    public void deposit(double amount) // makes deposit
    {
        this.balance = this.balance + amount;
    }

    public void withdraw(double amount) // makes withdrawal
    {
        this.balance = this.balance - amount;
    }

    public void display() // displays balance
    {
        System.out.println( "balance=" + this.balance );
    }
} // end class BankAccount

P.S. Make sure to use the this keyword.

jdraven
Offline
Joined: 2006-07-01

Hi,

I think the problem lies with this line
accountName() = name;
You need a getter and a setter of the form String getBankAccountName() void setBankAccountName(String name). Then use setBankAccountName("your name");
Cheers,

OneJumpedUp
Offline
Joined: 2012-04-05

You were right about the line, however, what he needed was simple:

ba1.accountName( name );