Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6683289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:48:16+00:00 2026-05-26T04:48:16+00:00

For my Java class, we need to make a bank account that has the

  • 0

For my Java class, we need to make a bank account that has the methods of withdrawing, depositing money, and displaying current balance. In the Tester class, I want to make it ask for the name, the balance, then allow you to choose 1, 2, or 3. Then it repeats the option you choose until you say type “n”. The problem is that running this code causes it to say after you deposit money “You deposited (amount of money deposited) in the account (name of account). Your new balance is (this).” The part where it says “this” is the exact same of the amount of money deposited. In other words, it doesn’t add it, it just makes the new balance the same as the deposit, regardless of how much was in before. Any help? Thanks.

import java.io.*;
import java.util.*;
public class BankAccount
{
    public BankAccount(double b, String n)
    {
        double balance = b;
        String name = n;
    }
    public void deposit(double d)
    {
        balance += d;
    }
    public void withdraw(double w)
    {
        balance -= w;
    }
    public String nickname()
    {
        System.out.print("Enter a new name: ");
        Scanner kbIn = new Scanner(System.in);
        String n = kbIn.nextLine();
        return n;
    }
    double balance;
    String name;
}

And the tester class:

import java.io.*;
import java.util.*;
public class Tester
{
    public static void main(String args[])
    {
        Scanner kbInLine = new Scanner(System.in);
        Scanner kbIn = new Scanner(System.in);

        System.out.print("Enter your name: ");
        String name = kbInLine.nextLine();

        System.out.print("Please enter balance: $");
        double balance = kbIn.nextDouble();

        BankAccount myAccount = new BankAccount(balance, name);
        String proceed = "y";

        while(proceed.equalsIgnoreCase("y"))
        {
            System.out.println("\nPlease pick a number. Would you like to...\n\t 1. Deposit\n\t 2. Withdraw\n\t 3. Print Balance\n");
            int choice = kbIn.nextInt();

            switch(choice)
            {
                case 1:
                    System.out.print("How much would you like to deposit?\n\t$");
                    double deposit = kbIn.nextDouble();
                    myAccount.deposit(deposit);
                    System.out.println("You have deposited $" + deposit + " into the account of " + name + ". The new balance is: " + myAccount.balance);
                    break;
                case 2:
                    System.out.print("How much would you like to withdraw?\n\t$");
                    double withdraw = kbIn.nextDouble();
                    if(myAccount.balance - withdraw > 0)
                    {
                        myAccount.withdraw(withdraw);
                        System.out.println("You have withdrawn $" + withdraw + " from the account of " + name + ". The new balance is: " + myAccount.balance);
                    }
                    else    
                    {
                        System.out.println("Sorry, you have insufficient funds for this operation. Your existing balance is $" + myAccount.balance);
                    }
                    break;
                case 3:
                    System.out.print("The balance in the account of " + name + " is $" + myAccount.balance);
                    break;
            }
            System.out.print("\nWould you like to do another transaction? (Y/N)");
            proceed = kbIn.next();
        }
        System.out.println("\nThank you for banking with us. Have a good day!");
    }
}

What’s really wierd is that I did a project before this one (it’s actually a simplified version) where it deposits and then withdraws a predetermined, coded amount, then outputs the new bank balance, and it does it fine. But the code for BankBalance is the same. Here’s the code for those.

BankAccount class is:

public class BankAccount
{
    public BankAccount(String nm, double amt) // Constructor
    {
        name = nm;
        balance = amt;
    }
    public void deposit(double d) // Sets up deposit object as balance += d
    {
        balance += d;
    }
    public void withdraw(double w) // Sets up withdraw object as balance -= w
    {
        balance -= w;
    }

    public double balance;
    public String name;
}

And the Tester class is:

import java.io.*;
import java.util.*;
public class Tester
{
    public static void main(String args[])
    {
        Scanner kbIn = new Scanner(System.in);
        System.out.print("Enter your name:");
        String name = kbIn.nextLine();

        System.out.print("Enter the balance:");
        double balance = kbIn.nextDouble();

        BankAccount myAccount = new BankAccount(name, balance);
        myAccount.deposit(505.22);
        System.out.println(myAccount.balance);
        myAccount.withdraw(100.00);

        System.out.println("The " + myAccount.name + " account balance is, $" + myAccount.balance);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:48:16+00:00Added an answer on May 26, 2026 at 4:48 am

    You’re not actually initialising your balance member variable here:

    public BankAccount(double b, String n)
    {
        double balance = b;
    

    This creates a new local variable called balance, to which you assign the value of b. The member variable balance will remain 0 (the default) after this constructor is run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Java class that I need to serialize to be stored
Will the fact that java class files can be decompiled and need of third
I need your expertise once again. I have a java class that searches a
I need to write a Java Comparator class that compares Strings, however with one
How can one make a Java class immutable, what is the need of immutability
I need to run Sh file from java class.In that java main class is
I have a java class and I need to debug it (put breakpoints and
I need a Java class to submit tickets to BMC Remedy's Helpdesk product. Wondering
I need this for calling a C function from Java class (JNI) and I
With ANTLR, I get some java class files after compilation. And I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.