如何在java中使用带重载构造函数的if-else条件

如何在java中使用带重载构造函数的if-else条件,java,if-statement,constructor-overloading,Java,If Statement,Constructor Overloading,我们的教授给我们提供了一个活动来创建一个重载构造函数来存储血型。我已经创建了两(2)个名为BloodData(无类修饰符)和RunBloodData(public)的类。我的问题是,我不知道如何将if-else语句应用于main方法中的这2个对象,如果用户没有输入任何内容,那么将显示存储在默认构造函数中的值 public class RunBloodData { public static void main(String[] args) { Scanner input = new Scanne

我们的教授给我们提供了一个活动来创建一个重载构造函数来存储血型。我已经创建了两(2)个名为BloodData(无类修饰符)和RunBloodData(public)的类。我的问题是,我不知道如何将if-else语句应用于main方法中的这2个对象,如果用户没有输入任何内容,那么将显示存储在默认构造函数中的值

public class RunBloodData {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter blood type of patient: ");
String input1 = input.nextLine();
System.out.print("Enter the Rhesus Factor (+ or -): ");
String input2 = input.nextLine();

//How to use conditions in this 2 objects with agruments and w/out arguments?

BloodData bd = new BloodData(input1,input2);
bd.display();
System.out.println(" is added to the blood bank.");   

// if the user did not input values, the values stored in the default constructor should display this.
BloodData bd = new BloodData();
bd.display();
System.out.println(" is added to the blood bank.");


//constructor
class BloodData {
static String bloodType;
static String rhFactor;

public BloodData() {
bloodType = "O";
rhFactor = "+";   
}
public BloodData(String bt, String rh) {
bloodType = bt;
rhFactor = rh;
}static
public void display(){
System.out.print(bloodType+rhFactor);
  
 }
}
// this is the output of it
Enter blood type of patient: B
Enter the Rhesus Factor (+ or -): -
B- is added to the blood bank.
O+ is added to the blood bank.//this should not be displayed since the user input values. How to fix it?

这是活动说明的一部分--在main方法中,添加语句,要求用户输入血型和恒河猴因子(+或-)。使用基于用户输入的参数实例化BloodData对象名称。例如,BloodData bd new BloodData(inputl,input2);其中input1和input2是存储用户输入内容的字符串变量。如果用户没有输入任何内容,则实例化一个不带参数的BloodData对象。通过创建的对象(例如bd.disp1ay)调用显示方法打印确认消息

一种简单的方法是使用.equals()检查输入是否等于空字符串(“”)。例如:

if (input1.equals("") && input2.equals(""))
    BloodData bd = new BloodData();
    // ...
else
    BloodData bd = new BloodData(input1,input2);
    // ...

但是,如果您的用户没有为一个输入输入任何内容,而是为另一个输入了一些内容,则这一点不起作用。我会添加一个条件来说明这一点,例如再次请求用户输入。

下面的代码将从用户处获取输入,并检查两个输入是否都为空。若其中一个为空,那个么它将不带参数地调用构造函数并相应地打印消息

import org.apache.commons.lang3.StringUtils;

public class RunBloodData {
  public static void main(String[] args) {

  Scanner input = new Scanner (System.in);
  System.out.print("Enter blood type of patient: ");
  String input1 = input.nextLine();

  System.out.print("Enter the Rhesus Factor (+ or -): ");
  String input2 = input.nextLine();
  BloodData bd = null;
  if(!StringUtils.isEmpty(input1) && !StringUtils.isEmpty(input2)) {
    bd = new BloodData(input1,input2);
  } else {
    bd = new BloodData();
  }
  bd.display();
  System.out.println(" is added to the blood bank.");  

  class BloodData {
    static String bloodType;
    static String rhFactor;

    public BloodData() {
      bloodType = "O";
      rhFactor = "+";   
    }

    public BloodData(String bt, String rh) {
      bloodType = bt;
      rhFactor = rh;
    }
    public void display(){
      System.out.print(bloodType+rhFactor);
    }
}