Search code examples
javamethodsconstructorencapsulation

I'm having a logic error on my program, the if statement works, but when a user inputs a bloodType & rhFactor it still shows O+ regardless of uinput


Here's the first part:

package Week5;
class BloodData
{
private String bloodType;
private String rhFactor;

public BloodData()
{
    bloodType = "O";
    rhFactor = "+";
}
public void setBloodType (String bloodType){
    this.bloodType = bloodType;
}
public void setRhFactor (String rhFactor){
    this.rhFactor = rhFactor;
}
String getBloodType(){
    return this.bloodType;
}
String getRhFactor(){
    return this.rhFactor;
}
}

here's the main method:

package Week5;
import java.util.Scanner;
public class RunBloodData
{
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);     
    BloodData bd = new BloodData();

    System.out.print("Enter blood type of patient : ");
    bd.setBloodType(input.nextLine());
    System.out.print("Enter the Rhesus factor (+ or -) : ");
    bd.setRhFactor(input.nextLine());
        
    if(bd.getBloodType().equals("") && bd.getRhFactor().equals(""))
    {
        bd = new BloodData();
        System.out.println (bd.getBloodType().toUpperCase() + bd.getRhFactor() + " is added to the bloodbank.");
    }
    else
    {
        bd = new BloodData();
        System.out.println (bd.getBloodType().toUpperCase() + bd.getRhFactor() + " is added to the bloodbank.");
    }
}

}

NOTE: I HIGHLY SUSPECT THAT I HAVE TO PUT SOMETHING INSIDE of new bloodData in:

else
    {
        bd = new BloodData();

I just don't know what to call considering bloodType and rhFactor are private and non-static.


Solution

  • I fixed it, I just had to remove bd = new BloodData(); in my else statement so any input will be registered.