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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:36:22+00:00 2026-06-16T02:36:22+00:00

So I am still fairly new to java. I have written this but am

  • 0

So I am still fairly new to java. I have written this but am not sure as to why when I run it, the amounts will not add to what is set in the client.

For example: I enter 500 as starting balance, if I hit deposit, and type 500, and then hit case 3, it should say 1000 but it still says 500. And then If i hit withdraw, it says I have -500.

Any ideas? Thanks

package bankaccount;
import java.util.Random;
import java.util.Scanner;

public class Client {
 public static void main(String args[]) {
     Scanner input = new Scanner(System.in);       
     System.out.println("Enter your Name: ");
            String cusName = input.nextLine();
            Random randomGenerator = new Random();
            int accNo = randomGenerator.nextInt(100000);
            System.out.println("Enter Initial Balance: ");
            int balance = input.nextInt();
            BankAccount b1 = new BankAccount(cusName, accNo, balance);
            int menu;
            System.out.println("Menu");
            System.out.println("1. Deposit Amount");
            System.out.println("2. Withdraw Amount");
            System.out.println("3. Display Information");
            System.out.println("4. Exit");
            boolean quit = false;
            do {
                    System.out.print("Please enter your choice: ");
                    menu = input.nextInt();
                    switch (menu) {
                    case 1:            
                             System.out.print("Enter depost amount:");
                             Money.amount = input.nextInt(); 
                             b1.getDeposit(); 

                            break;

                    case 2:
                             System.out.println("Current Account Balance=" + b1.getBalance());
                             System.out.print("Enter withdrawal amount:");
                             Money.amount = input.nextInt();
                             b1.getWithdraw();

                            break;

                    case 3:
                            b1.display();
                            break;
                    case 4:
                            quit = true;
                            break;
                    }
            } while (!quit);
    }

public class Money
{

public static int accountNumber, balance=0;
static int amount;

static String name;
public void setDeposit(int amount) {   

            balance = balance + amount;   
    }
public int getDeposit()
{

            balance = balance + amount;
            if (amount < 0) {
                    System.out.println("Invalid");
            }
                                    return 1; 

}
 public void setBalance(int b)
 {
     b = balance;
 }
 public int getBalance()
 {
     return balance;
 }

 public void setWithdraw(int amount)  {


            balance = balance - amount;
    }
 public int getWithdraw()
 {
            balance = balance - amount;
     if (balance < amount) 
     {
                    System.out.println("Not enough funds.");
                    return 1;
            }
           else if (amount < 0) {
                    System.out.println("Invalid");
                    return 1;}
                    else 
                             return 0;
 }

import java.util.*;

public class BankAccount extends Money {
    static String name;
    public static int balance, amount, acctNum;
    Money customerMoney;


    BankAccount(String name, int accNo, int bal) {
            this.name = name;
            this.acctNum = accNo;
            this.balance = bal;
            this.customerMoney = new Money();
    }
            void display() {
            System.out.println("Name:" + name);
            System.out.println("Account No:" + acctNum);
            System.out.println("Balance:" + balance);

    }

    void displayBalance() {
            System.out.println("Balance:" + balance);
    }
    public Money getMoney(){
          return this.customerMoney;
    }
}
  • 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-06-16T02:36:23+00:00Added an answer on June 16, 2026 at 2:36 am

    I would remove all the public static int variables that you are using. These are going to cause confusion because it’s much harder to follow what their values are as the program executes. Best to encapsulate your logic into BankAccount using private variables and public methods to modify them.

    I would, personally, eliminate the Money class from your code. It’s just going to cause confusion and with your simplified logic is not required. Let’s assume the account holds some arbitrary count of ‘money’ but there is no real-life money actually backing it up – (kind of like real life, it’s just ‘numbers on a screen’ right?) – in this case we wouldn’t need the Money class, just an int for our BankAccount‘s balance.

    Without trying to make too many changes to your underlying functionality, I rewrote as the below two classes:

    A BankAccount class:

    package banking;
    
    public class BankAccount {
    
        /**
         * The balance of this account. <br/>
         * Assumes integer money (Floating point math is horrible and who really
         * needs pesky pence or cents right?!)
         */
        private int balance;
        /**
         * The account number
         */
        private final int acctNum;
        /**
         * Name of the account holder
         */
        private final String name;
    
        /**
         * Construct our basic account with an account number, account holder and
         * starting balance.
         *
         * @param name
         * @param accNo
         * @param bal
         */
        public BankAccount(String name, int accNo, int bal) {
            this.name = name;
            this.acctNum = accNo;
            this.balance = bal;
        }
    
        /**
         * Make a deposit to this account by adding a fixed sum to the existing
         * balance. <br/>
         *
         * @param amount
         */
        public void deposit(int amount) {
            if (amount <= 0) {
                throw new IllegalArgumentException("You cannot deposit zero or less");
            } else {
                this.balance += amount;
            }
        }
    
        /**
         * Make a withdrawal from this account by subtracting a fixed amount from
         * the existing balance. <br/>
         *
         * @param amount
         */
        public void withdraw(int amount) {
            if (amount > balance) {
                throw new IllegalArgumentException("Insufficient Funds");
            } else if (amount <= 0) {
                throw new IllegalArgumentException("You cannot withdraw zero or less");
            } else {
                balance -= amount;
            }
        }
    
        /**
         * Get the account holder name.
         *
         * @return
         */
        public String getName() {
            return name;
        }
    
        /**
         * Get the current account balance.
         *
         * @return
         */
        public int getBalance() {
            return balance;
        }
    
        /**
         * Get the account identifier for this account.
         *
         * @return
         */
        public int getAcctNum() {
            return acctNum;
        }
    
        /**
         * Debug print method.
         */
        public void display() {
            System.out.println("Name:" + name);
            System.out.println("Account No:" + acctNum);
            System.out.println("Balance:" + balance);
        }
    }
    

    And the main class:

    package banking;
    
    import java.util.Random;
    import java.util.Scanner;
    
    public class BankMain {
    
        public static void main(String args[]) {
            Scanner input = new Scanner(System.in);
    
            System.out.println("Enter your Name: ");
            String customerName = input.nextLine();
    
            Random randomGenerator = new Random();
            int acctNo = randomGenerator.nextInt(100000);
    
            System.out.println("Enter Initial Balance: ");
            int balance = input.nextInt();
    
            BankAccount acct = new BankAccount(customerName, acctNo, balance);
    
            System.out.println("Menu");
            System.out.println("1. Deposit Amount");
            System.out.println("2. Withdraw Amount");
            System.out.println("3. Display Information");
            System.out.println("4. Exit");
    
            boolean quit = false;
            int menu;
            do {
                final int transaction;
                System.out.print("Please enter your choice: ");
                menu = input.nextInt();
                switch (menu) {
                    case 1:
                        System.out.print("Enter depost amount:");
                        transaction = input.nextInt();
                        acct.deposit(transaction);
                        break;
    
                    case 2:
                        System.out.println("Current Account Balance=" + acct.getBalance());
                        System.out.print("Enter withdrawal amount:");
                        transaction = input.nextInt();
                        try {
                            acct.withdraw(transaction);
                        } catch (IllegalArgumentException iaEx) {
                            System.out.println(iaEx.getMessage());
                        }
                        break;
    
                    case 3:
                        acct.display();
                        break;
    
                    case 4:
                        quit = true;
                        break;
                }
            } while (!quit);
        }
    }
    

    This is still far from perfect, but I feel it’s easier to follow for having the static variables and Money class removed.

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

Sidebar

Related Questions

I'm still fairly new to Java, but I'm fairly sure this shouldn't happen. There's
I'm still fairly new to rails so I'm not sure what I'm missing here.
I am still fairly new to parallel computing so I am not too sure
You'll have to forgive me because i'm still fairly new to Obj-C but i'm
I'm still fairly new to Java and have a question in regards to JTable
I'm still fairly new to Python, and my OO experience comes from Java. So
I am still fairly new to C# and I have a question regarding attributes.
I'm still fairly new to symfony, so my apologies if this is a stupid
This is a bit of noob question - I'm still fairly new to C#
I am fairly new to JAVA and especially concurrency, so probably/hopefully this is fairly

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.