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 6382669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:33:10+00:00 2026-05-25T02:33:10+00:00

I have a really long problem. I am creating a bank account and setting

  • 0

I have a really long problem. I am creating a bank account and setting the balance to 0. If the user chooses to withdraw or deposit money to the account, the balance never changes. I choose to show the balance and it still says 0. This is probably a no brainer but I am spent right now. Here is my long code(The switch statement is in my main class and the methods are in an object class):

public class MyProgram2{
public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    Scanner input = new Scanner (System.in);
    String menu, outputString, poo;
    int option = 1;
    int id = 0;
    double balance = 0, amount = 0;
    Account acc = new Account();


    menu ="\n\t1  Create Account and ID" + 
            "\n\t2  Check balance" +
            "\n\t3  Withdraw" +
            "\n\t4  Deposit" + 
            "\n\t5  Get account ID" +
            "\n\t6  Display Account Info" +

            "\n\t0  Quit\n\n\n";

    System.out.println(menu);
    System.out.println("\tEnter your selection:  ");
    option = scan.nextInt();

    while (option != 0) {

            switch (option) {

            case 1: //Create an account and set ID
                        System.out.print("Enter Your Account ID to create account:\t");
                        id = input.nextInt();
                        System.out.println("Account created!");
                        break;

            case 2:     //check balance
                        acc.checkBalance(balance);
                        break;

            case 3:     //withdraw money
                        acc.withdraw(balance, amount);
                        break;

            case 4: //deposit money
                        acc.deposit(balance, amount);
                        break;

            case 5:     //get account id
                        acc.getID(id);
                        break;

            case 7:     //display account info
                        System.out.print("option 7");
                        break;

            default: outputString = "\nInvalid Selection\n";
                        System.out.println(outputString);
                        break;

        }
    System.out.println(menu);
    System.out.print("\tEnter your selection:   ");
    option = scan.nextInt();    
    }

And these are the methods I am calling:

public class Account{
Scanner input = new Scanner (System.in);

public Account(){
}

public void getID(int id){
    System.out.println("Your account ID is:\t" + id);
}

public void checkBalance(double balance){
    System.out.println("Your balance is:\t$" + balance);
}

public double withdraw(double amount, double balance){
    System.out.println("How much do you want to withdraw?:\t$");
    amount = input.nextDouble();
    balance -= amount;
    return balance;
}

public double deposit(double amount, double balance){
    System.out.println("How much do you want to deposit?:\t");
    amount = input.nextDouble();
    balance += amount;
    return balance;
}

public void getAccountInfo(int id, double 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-25T02:33:11+00:00Added an answer on May 25, 2026 at 2:33 am

    The variable

    double balance

    cannot be passed as a reference. It does a copy so when you try to manipulate it, it won’t affect the original that you pass as an argument. You need to update the value using the return value that you have in the function.

    In order to make it work, you should do:

    
    case 4: //deposit money
    
    // note here that you need to update the balance variable using the return value that
    // you put in the function
    balance = acc.deposit(balance, amount);
    break;
    

    Note: Your design separating the balance from the Account class is not ideal per @Psyrus’s answer. You should keep the balance as part of the Account class. The reason being that balance is part of the account and if your program grows to handle multiple accounts (just for the sake of examples), separating the variable balance from the account would create maintenance headache (imagine that with two instances of Account, you will have balance1 and balance2 variables (or whatever you will call it) in MyProgram2, the main application). While I gave the cause of your problem in regards of variable passing, you should refactor your code to follow @Psyrus suggestion.

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

Sidebar

Related Questions

I have a really long port map where I want to replace a bunch
I have a really long integration test that simulates a sequential process involving many
So I have a really long chain of methods, something similar to: return self.append_command(fbghasjfa).append_command(input_file_part).append_command(output_video_codec_part).append_command(output_resolution_part).append_command(output_video_bitrate_part).append_command(strict_part).append_command(output_audio_codec_part).append_command(output_number_of_audio_channels_part).append_command(output_audio_bitrate_part).append_command(output_file_part).__finalized
I have a block of text which occasionally has a really long word/web address
I have this case <WrapPanel> <CheckBox>Really long name</CheckBox> <CheckBox>Short</CheckBox> <CheckBox>Longer again</CheckBox> <CheckBox>Foo</CheckBox> <Slider MinWidth=200
I have a rather long switch-case statement. Some of the cases are really short
In my Swing application I have the problem that when creating a new instance
i have to admit, i never really completely got the *nix filesystem permission model.
I have a JS JQuery DataTable, it is really long. Normally if you have
I have really simple XML (HTML) parsing ANTLR grammar: wiki: ggg+; ggg: tag |

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.