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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:27:26+00:00 2026-06-15T09:27:26+00:00

The problem is that I keep getting the balance to be minus, it executes

  • 0

The problem is that I keep getting the balance to be minus, it executes the part where the money needs to be taken out, but the ‘if’ statement that I’ve put in place should prevent that. I’m new to programming, I’m very sorry if this is silly.

Here is my main:

public class PrinterAccount
{
    private int balance;

    public void topUp(int amount)
    { 
        balance += amount;
    }
    public int getBalance()
    { 

        return balance;
    }
    public boolean printDocument(int numPages, boolean isDoubleSided)
    {

        if (isDoubleSided)
        {
            if(numPages % 2 == 0)
            {
                int b = 0;
                b = b - ((numPages/2)*5);
                    if (balance > b)
                    {
                        balance -= ((numPages/2) * 5);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
            }
            else
            {
                numPages = numPages + 1;
                int a = 0;
                a = a - ((numPages/2)*5);
                    if (balance > a)
                    {
                        balance -= ((numPages/2) * 5);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
            }
        }
        else
        {
            int c = 0;
            c = c - (numPages*5);
                if (balance > c)
                {
                    balance -= (numPages*5);
                    return true;
                }
                else
                {
                    return false;
                }

        }   
    }
}

and here is my test:

public class PrinterAccountTest
{
    public static void main(String[] args)
    {
        // Create an object of class PrinterAccount
        PrinterAccount myprinterAccount = new PrinterAccount();
        // Add 10 pounds credit (1000 pence)
        myprinterAccount.topUp(1000);

        // **************TEST 1************************
        // Print a 15-page document, single-sided, should cost 75p
        boolean res = myprinterAccount.printDocument(15, false);
        // The document should have printed successfully
        if (res)
            System.out.println("The document printed successfully");
        else
            System.out.println("The document failed to print: not enough credit.");
        // Display the remaining balance: should be 925
        System.out.println("Remaining balance is " + myprinterAccount.getBalance());

        // **************TEST 2************************
        // Print a 20-page document (even number of pages), double-sided, should cost 50p
        res = myprinterAccount.printDocument(20, true);
        // The document should have printed successfully
        if (res)
            System.out.println("The document printed successfully");
        else
            System.out.println("The document failed to print: not enough credit.");
        // Display the remaining balance: should be 875
        System.out.println("Remaining balance is " + myprinterAccount.getBalance());

        // **************TEST 3************************
        // Print a 7-page document (odd number of pages), double-sided, should cost 20p
        res = myprinterAccount.printDocument(7, true);
        // The document should have printed successfully
        if (res)
            System.out.println("The document printed successfully");
        else
            System.out.println("The document failed to print: not enough credit.");
        // Display the remaining balance: should be 855
        System.out.println("Remaining balance is " + myprinterAccount.getBalance());

        // **************TEST 4************************
        // Print a 200-page document, single-sided, should cost 1000p
        res = myprinterAccount.printDocument(200, false);
        // Should get a message saying the document failed to print
        if (res)
            System.out.println("The document printed successfully");
        else
            System.out.println("The document failed to print: not enough credit.");
        // Display the remaining balance: should be 855
        System.out.println("Remaining balance is " + myprinterAccount.getBalance());


    }
}

This is the result I get when I run the code:

The document printed successfully
Remaining balance is 925
The document printed successfully
Remaining balance is 875
The document printed successfully
Remaining balance is 855
The document printed successfully
Remaining balance is -145

But I need it to display:

The document printed successfully
Remaining balance is 925
The document printed successfully
Remaining balance is 875
The document printed successfully
Remaining balance is 855
The document failed to print: not enough credit.
Remaining balance is 855
  • 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-15T09:27:27+00:00Added an answer on June 15, 2026 at 9:27 am

    This lines in the print document method:

        int c = 0;
        c = c - (numPages*5);
        if (balance > c) ...
    

    Variable c will always have negative value. I suppose you wanted to add (numPages*5) instead of subtracting it.

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

Sidebar

Related Questions

Problem is that I keep getting the 'No Such Slot' runtime error in Qt
So, one problem pattern that I keep coming across and don't have a good
Simple problem that I can't figure out... How can I print a '%' character
Seems to be a problem that many people have, but all the answers I
I have a problem that I need some help on but I feel I'm
I'm trying to make HTTPS connections, using HttpClient lib, but the problem is that,
The problem that I have is somehow very specific. I have to implement a
I have a problem that I'm working on for quite some time now. I
Here is a problem that has been bothering me a lot about Python, I
Im currently facing the problem that when i try to set focus on some

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.