简体   繁体   English

如何使 class 动态检查不同的对象变量?

[英]How do I make a class to dynamic check different objects variable?

I'm new to Java, and i'm trying to create an automatic working shift schedule.我是 Java 的新手,我正在尝试创建一个自动工作班次计划。

I want the code to mix four different employees to handle a morning shift and afternoon shift every work day.我希望代码混合四个不同的员工来处理每个工作日的早班和下午班。 I have made some code that just pick a random employee into a shift:我编写了一些代码,只是随机选择一名员工轮班:

import java.util.Arrays;
import java.util.Random;

public class CreateNewShift {

    public static void main(String[] args) {
        
        int startWeek = 30; //Which week would start from?
        int endWeek = 32; //which week will you end on?
        generateShift(startWeek, endWeek);

    }

    private static void generateShift(int startWeek, int endWeek) {

        String Employees[] = {"Employee1", "Employee2", "Employee3", "Employee4"};
        String morningShift;
        String afternoonShift;

      for (int x = 0; x <= (endWeek - startWeek); x++) { //This is counting the number of weeks
         System.out.println("\nWeek: " + (startWeek+x));

      for (int i = 1; i <= 5; i++) {   //this is finding the next working shift day
          morningShift = p.chooseRandomEmployee(Employees);
          afternoonShift = p.chooseRandomEmployee(Employees);
          if (i == 1) {
            System.out.println("Mon: " + morningShift + " + " + afternoonShift);
          }
          else if (i == 2) {
            System.out.println("Tue: " + morningShift + " + " + afternoonShift);
          }
          else if (i == 3) {
             System.out.println("Wed: " + morningShift + " + " + afternoonShift);
          }
          else if (i == 4) {
            System.out.println("Thu: " + morningShift + " + " + afternoonShift);
          }
          else {
            System.out.println("Fri: " + morningShift + " + " + afternoonShift);
          }
        }
      }
    }



public class Employee {
  public String chooseRandomEmployee(String[] Employees) {
        Random r = new Random();
        int randomNumber = r.nextInt(Employees.length);
        return Employees[randomNumber];
  }
}

However, I now want the code to handle more restictions.但是,我现在希望代码能够处理更多限制。 So i'm currently trying to add the option for the employees to choose some specific days that they dont want to have a shift.因此,我目前正在尝试为员工添加选项,以选择他们不想轮班的特定日期。 I have done this by adding this code to the Employee class:我通过将此代码添加到 Employee class 来完成此操作:

public class Employee {
  boolean monShift = true;
  boolean tueShift = true;
  boolean wedShift = true;
  boolean thuShift = true;
  boolean friShift = true;

  public String chooseRandomEmployee(String[] Employees) {
        Random r = new Random();
        int randomNumber = r.nextInt(Employees.length);
        return Employees[randomNumber];
  }
}

And then i had tried to create new objects in my main class:然后我试图在我的主要 class 中创建新对象:

private static void generateShift(int startWeek, int endWeek) {
        Employee Employee1 = new Employee("Employee1");
        Employee Employee2 = new Employee("Employee2");
        Employee Employee3 = new Employee("Employee3");
        Employee Employee4 = new Employee("Employee4");
        String Employees[] = {"Employee1", "Employee2", "Employee3", "Employee4"};
        String morningShift;
        String afternoonShift;
        ....

Quetions:问题:

How can I improve my code in the Employee class to do a check if the random chosen employee have如何改进员工 class 中的代码以检查随机选择的员工是否有

monShift = true;

I have tried something like this, but i know it will not work, (and does not work either):我已经尝试过这样的事情,但我知道它不会起作用,(也不起作用):

import java.util.Random;

public class Employee {
    public String chooseRandomEmployee(String[] Employees) {
        Random r = new Random();
        int randomNumber = r.nextInt(Employees.length);
        **if (("Employee" + randomNumber).monShift == false) {**
           // Go back and try find a new random employee
        }
        else {
          return Employees[randomNumber];
        }
    }
}

So i need a way to make my code dynamic to know which object (employee) it has to check if they are available that specific day or not.所以我需要一种让我的代码动态的方法,以了解它必须检查哪些 object(员工)在特定日期是否可用。

Feel free to ask for a deepening if my question is not clear.如果我的问题不清楚,请随时要求加深。

Since this i my first question on this forum, I also appriciate feedback if my question and thoughts are too long, or any other comments.由于这是我在这个论坛上的第一个问题,如果我的问题和想法太长或任何其他评论,我也会给予反馈。

I dont think that putting the chooseRandomEmployee() function inside the Employee object is a good idea beacuse is not a part of the employee, is not an "action" of it.我不认为将 chooseRandomEmployee() function 放在 Employee object 中是一个好主意,因为它不是员工的一部分,不是它的“动作”。 I think you shiudl put it outside but I want to respect your decision so shoudl check the do while loop .我认为你应该把它放在外面,但我想尊重你的决定,所以应该检查do while 循环

import java.util.Random;

public class Employee {
    public String chooseRandomEmployee(String[] Employees) {

        int randomNumber;

        do {

        //Generate a new random number
        Random r = new Random();
        randomNumber = r.nextInt(Employees.length);     
   
        //The line under is the same that saying "If monSift == false return to 
        //the beginning and start again generating a new number"
        } while ("Employee" + randomNumber).monShift == false);

        return Employees[randomNumber];
       
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM