java - 如何将一个类中 boolean 方法的结果获取到Java中另一个类的if语句中?

标签 java class methods boolean

我正在编写一个程序,用于收集员工的名字、姓氏和销售额。我的所有基础功能都运行良好,但我有一个问题。在我的 while 循环中,我对其进行了编程,因此如果用户输入“add”,那么它将调用我在不同类中创建的方法,并将他们输入的任何数字添加到员工当前的销售总额中,减法也是如此。但是如果我需要编写一个 if 语句来检查减法是否不成功以及是否显示“错误”。

 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

这是使用 booleanMethod 方法的类的一部分

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");
            }
        }

该工具成功完成,但它不显示错误消息,它只显示我的行,询问另一个销售数据,我如何让它实际工作?

最佳答案

首先放置减法的责任来执行验证...

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

然后简单地使用返回结果来确定应该做什么...

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

恕我直言,减法实际上应该在达到可能的无效状态之前停止,例如......

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

关于java - 如何将一个类中 boolean 方法的结果获取到Java中另一个类的if语句中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47274060/

相关文章:

c++ - 显示结构/类错误数组 C++

python - 动态创建类python的实例

Java自然语言处理 : Extracting Indicies When Tokenizing Text

c++ - 对象构造期间缓冲区太小。觉得跟strcpy_s()有关系

java - hibernate 插入没有左刻度

c# - .Net 扩展方法(此字符串 Foo)仅部分可见

c# - 无法在 C# 中异步运行方法

java - 我的 boolean 方法java

java - 如何获取 Spring Batch Job 后的失败次数?

java - 这样使用线程效率高吗?