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

  • Home
  • SEARCH
  • 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 8920347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:14:48+00:00 2026-06-15T06:14:48+00:00

I’m trying to use a method in my class that charges a fee. The

  • 0

I’m trying to use a method in my class that charges a fee. The method is this:

  public double chargeFee()
  {
    balance -= 10;
    return balance;
  }

However, it subtracts 20. I’ve tried recompiling it but I can’t find what is causing the issue.

Complete code:

public class ManageAccounts
{
    public static void main(String[] args)
    {
     Account acct1, acct2; 


    //create account1 for Sally with $1000
    acct1 = new Account(1000, "Sally", 1111);
    acct2 = new Account(500, "Joe", 2222);//create account2 for Joe with $500

    System.out.println("Depositing $100 into Account 2222...");
    acct2.deposit(100.00);//deposit $100 to Joe's account
    System.out.println("New Balance for Account 2222: $" + acct2.getBalance());//print Joe's new balance (use getBalance())
    System.out.println();
    System.out.println("Withdrawing $50 from Account 1111...");
    acct1.withdraw(50);//withdraw $50 from Sally's account

    System.out.println("New Balance for Account 1111: $" + acct1.getBalance());//print Sally's new balance (use getBalance())
    System.out.println();
    acct1.chargeFee();
    acct2.chargeFee();//charge fees to both accounts
    System.out.println("Charging usage Fees...");
    System.out.println("Account balance after fees:");
    System.out.println("Account 1111: $" + acct1.chargeFee());
    System.out.println("Account 2222: $" + acct2.chargeFee());
    System.out.println();
    System.out.println("Changing name on Account 2222...");
    acct2.changeName("Joseph");//change the name on Joe's account to Joseph
    System.out.println();
    System.out.println("Printing account summaries...");
    System.out.println(acct1.toString());
    System.out.println(acct2.toString());//print summary for both accounts

    }
}



import java.text.NumberFormat;

public class Account
{
  private double balance;
  private String name;
  private long acctNum;
    NumberFormat money = NumberFormat.getCurrencyInstance();
  //----------------------------------------------
  //Constructor -- initializes balance, owner, and account number
  //----------------------------------------------
  public Account(double initBal, String owner, long number)
  {
    balance = initBal;
    name = owner;
    acctNum = number;
  }

  //----------------------------------------------
  // Checks to see if balance is sufficient for withdrawal.
  // If so, decrements balance by amount; if not, prints message.
  //----------------------------------------------
  public void withdraw(double amount)
  {
    if (balance >= amount)
       balance -= amount;
    else
       System.out.println("Insufficient funds");
  }

  //----------------------------------------------
  // Adds deposit amount to balance.
  //----------------------------------------------
  public void deposit(double amount)
  {
    balance += amount;
  }

  //----------------------------------------------
  // Returns balance.
  //----------------------------------------------
  public double getBalance()
  {
    return balance;
  }


  //----------------------------------------------
  // Returns a string containing the name, account number, and balance.
  //----------------------------------------------
  public String toString()
  {
    return ("Name: " + name + "\tAccount Number: " + acctNum + "\tBalance: " + money.format(balance));
  }

  //----------------------------------------------
  // Deducts $10 service fee
  //----------------------------------------------
  public double chargeFee()
  {
    balance -= 10;
     return balance;
  }

  //----------------------------------------------
  // Changes the name on the account 
  //----------------------------------------------
  public void changeName(String newName)                          
  {
    name = newName;
  }

}

Some sample output:

Depositing $100 into Account 2222...
New Balance for Account 2222: $600.0

Withdrawing $50 from Account 1111...
New Balance for Account 1111: $950.0

Charging usage Fees...

Account balance after fees:

Account 1111: $930.0
Account 2222: $580.0

Changing name on Account 2222...

Printing account summaries...
Name: Sally Account Number: 1111    Balance: $930.00
Name: Joseph    Account Number: 2222    Balance: $580.00
  • 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-15T06:14:49+00:00Added an answer on June 15, 2026 at 6:14 am

    You call chargeFee twice on both accounts, once when you charge the actual fee, and again when you print the results.

    For future reference, there’s a guideline called “Seperate command and query”, or CQS. You might want to read up on it. To put your situation in its light:

    • You are using the chargeFee as a query ( ie, you return the balance )
    • and you use chargeFee as a command, deducting money.

    Change chargeFee to return a void (or this if you want method chaining), and you won’t accidently use it as a query.

    http://en.wikipedia.org/wiki/Command-query_separation

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.