okent
okent

Reputation: 5

How get the result of a boolean method in one class into an if statement of another class in Java?

I'm writing a program that gathers a first name, a last name, and the sales figure of an employee. I have all the basics working perfectly but I have an issue. In my while loop I have programmed it so if the user enters "add" then it will call a method I made in a different class and add whatever number they entered to the employees current sales total, same for subtraction. But if I need to write an if statement that checks if the subtraction is unsuccessful and if it is display an "error".

 public class EmployeeInfo
 {
 //class constants

 //class variables
 private String firstName;
 private String lastName;
 private int itemsSold;
 private int employeeTotal;

public EmployeeInfo(String first, String last, int sold)
{

    //local constants

    //local variables

    /********************   Start Constructor  *****************/

    //Calls the first and last name, and the amount of items sold
    firstName = first;
    lastName = last;
    itemsSold = sold;

}//End constructor


 public void addition(int itemsSold)
   {
      //local constants

      //local variables


      /********************   Start main method  *****************/

      //Adds the items sold to the employee total
      employeeTotal = itemsSold + employeeTotal;

}//end addition


 public void subtraction(int itemsSold)
 {
       //local constants

       //local variables

       /********************   Start main method  *****************/

       //Subtracts the items not sold to the employee total
       employeeTotal = (employeeTotal - itemsSold);

}//end subtraction


public boolean booleanMethod(boolean result)
{
       //local constants

       //local variables

       /********************   Start main method  *****************/

       if(employeeTotal < 0)
       {
           result = false;
       }
       else
       {
           result = true;
       }

       return result;

 }//End booleanMethod

   public String toString()
    {
       //local constants

       //local variables


       /******************************************************/

       return String.format(Util.setLeft(45,"Eployee Name and Sales Figures") + "\n\n" + Util.setLeft(40, "First Name: ") + Util.setRight(30, firstName) +
       "\n" + Util.setLeft(40, "Last Name : ") + Util.setRight(30, lastName) + "\n" + Util.setLeft(40, "Items Sold: ") + Util.setRight(30, Integer.toString(employeeTotal)));

   }//end to string

}//end employeeInfo

and this is a partial part of the class that uses the booleanMethod, method

while(soldItems != QUIT)
    {

        //Clear screen then prompt the user to add or subtract
        System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
        System.out.print(Util.setLeft(35, "Add or Subtract: "));
        input = Keyboard.readString();
        System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

        //if the user enters add the program will add to the employee total
        if(input.equals(ADD))
        {
            info.addition(soldItems);

        }

        //else the program will subtract from the employee total
        else
        {
            info.subtraction(soldItems);

            if (info.booleanMethod(false))
            {
                System.out.print("Error");
                System.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
            }
        }

the tool completes successfully but it doesn't display the error message it just displays my line asking for another sales figure, how do I get it to actually work?

Upvotes: 0

Views: 68

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347234

Start by placing the responsibility of the subtraction to perform the validation...

public boolean subtraction(int itemsSold)
{
    //local constants

    //local variables

    /********************   Start main method  *****************/

    //Subtracts the items not sold to the employee total
    employeeTotal = (employeeTotal - itemsSold);

    return employeeTotal < 0;
}//end subtraction

Then simply use the return result to determine what should be done...

if (!info.subtraction(soldItems)) {
    //...
}

IMHO, subtraction should actually stop before it reaches a possible invalid state, for example...

public boolean subtraction(int itemsSold)
{
    //local constants

    //local variables

    /********************   Start main method  *****************/

    if (employeeTotal - itemsSold >= 0) {
        //Subtracts the items not sold to the employee total
        employeeTotal = (employeeTotal - itemsSold);
        return true; 
    } 

    return false;
}//end subtraction

Upvotes: 1

isaace
isaace

Reputation: 3429

Change your code to

if (!info.booleanMethod(false)) 

which means that it's false. Your current code means

if(true)

Actually there is no reason to pass in the boolean at all. The boolean belongs in the booleanMethod() method.

It should look like this:

public boolean booleanMethod()
{


//local constants

   //local variables

   /********************   Start main method  *****************/

   if(employeeTotal < 0)
   {
       return false;
   }
   else
   {
       return  true;
   }

}

and you should call it like this:

if (!info.booleanMethod())

Upvotes: 0

Related Questions