encapsulation

 09-05-2022

encapsulation in java

import java.util.Scanner;

class BloodData123{
	private String bloodType = "O";
	private String rhFactor = "-";

	public void setBloodType(String bt) {
		this.bloodType = bt;
	}
	public void setRhFactor(String rh) {
		this.bloodType = rh;	
	}
	
	public String getBloodType() {
		return bloodType;
	}
	
	public String getRhFactor() {
		return rhFactor;
	}

	public void display() {
		System.out.println(this.bloodType.toUpperCase() + this.rhFactor + " is added to the blood bank.");
	}
}

public class RunBloodDataEncapsulation {
	public static void main(String[] args) {
	Scanner scan = new Scanner(System.in);
	
	System.out.print("Enter the blood type of patient: ");
	String btype = scan.nextLine();
	System.out.print("Enter the Rhesus factor (+ or -) ");
	String rh = scan.nextLine();
	
	if (btype == "" && rh == "") {	
		BloodData123 bd = new BloodData123();
		bd.display();
	} else {
		BloodData123 bd = new BloodData123();
		bd.setRhFactor(rh);
		bd.setBloodType(btype);
		System.out.print(bd.getBloodType() + bd.getRhFactor());
	}
	
	}
}