目前,我有一个血型班,检查捐赠者和接受者的血液相容性。它显示了捐赠者可以捐献的血型和接收者可以从中接收的血型。代码是
BLOODGROUP.JAVA
package com.safeguy.blooddonor.classes;
/**
* Created by Lenovo User on 6/17/2017.
*/
public class BloodGroup {
public BloodGroup(){
}
public boolean donor(String a, String b){
boolean t = false;
String donor = a.toUpperCase();
String rec = b.toUpperCase();
if(donor.equals("O+") && rec.equals("O+")){
t = true;
}
else if(donor.equals("O-")){
t = true;
}
else if(donor.equals("A+") && (rec.equals("A+") || rec.equals("AB+"))){
t = true;
}
else if(donor.equals("A-") && (rec.equals("A+") || rec.equals("AB+") || (rec.equals("A-") || (rec.equals("AB-"))))){
t = true;
}
else if(donor.equals("B+") && (rec.equals("AB+") || rec.equals("B+"))){
t = true;
}
else if(donor.equals("B-") && (rec.equals("AB+") || rec.equals("AB-") || rec.equals("B+") || (rec.equals("B-")))){
t = true;
}
else if(donor.equals("AB+") && (rec.equals("AB+"))){
t = true;
}
else if(donor.equals("AB-") && (rec.equals("AB+") || rec.equals("AB-"))){
t = true;
}
return t;
}
public boolean recepient(String a, String b){
boolean t = false;
String rec = a.toUpperCase();
String don = b.toUpperCase();
if(rec.equals("AB+")){
t = true;
}
else if(rec.equals("AB-") && (don.equals("AB-") || don.equals("A-") || don.equals("B-") || (don.equals("O-")))){
t = true;
}
else if(rec.equals("A+") && (don.equals("A+") || (don.equals("A-") || don.equals("O-") || don.equals("O+")))){
t = true;
}
else if(rec.equals("A-") && (don.equals("A-") || (don.equals("A+")))){
t = true;
}
else if(rec.equals("B+") && (don.equals("B+") || (don.equals("B-") || (don.equals("O-")) || (don.equals("O+"))))){
t = true;
}
else if(rec.equals("B-") && (don.equals("B-") || (don.equals("O-")))){
t = true;
}
else if(rec.equals("O+") && (don.equals("O+")) || (don.equals("O-"))){
t = true;
}
else if(rec.equals("O-") && (don.equals("O-"))){
t = true;
}
return t;
}
}
我希望应用程序访问主机血型并将其与供体的血型进行比较。从应用程序获取主机血型的代码是..
ViewActiveRequests.JAVA
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_lists_toolbar);
sessionManager = new SessionManager();
list = (ListView) findViewById(R.id.listView);
personList = new ArrayList<HashMap<String, String>>();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarViewRequests);
toolbar.setTitle("View Active Blood Requests");
//android.support.v7.widget.Toolbar
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
initialize();
callProgressDialog();
getData();
}
private void initialize() {
Intent getUsername = getIntent();
CURRENTUSER= getUsername.getStringExtra("USERNAME");
LOCAL_BLOOD_GROUP = sessionManager.getUserDetails(this,"BLOODGROUP_LOGGED");
filter = false;
FilteredBloodgroup = "unfiltered";
list.setOnItemClickListener(this);
FilterBySpinner = (Spinner)findViewById(R.id.spFilterBy);
FilterBySpinner.setOnItemSelectedListener(this);
List<String> bloodCategories = new ArrayList<String>();
bloodCategories.add("Filter List Here");
bloodCategories.add("A+");
bloodCategories.add("A-");
bloodCategories.add("B+");
bloodCategories.add("B-");
bloodCategories.add("AB+");
bloodCategories.add("AB-");
bloodCategories.add("O+");
bloodCategories.add("O-");
bloodCategories.add("Unfiltered");
ArrayAdapter<String> bloodCategoriesAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,bloodCategories);
FilterBySpinner.setAdapter(bloodCategoriesAdapter);
}
我想要的是应用程序应该获取捐赠者和接受者血型,检查他们在bloodgroup.java类中的兼容性,如果血液是否兼容则显示。
答案 0 :(得分:1)
class Human{
private BloodGroup bloodGroup;
private String username;
public Human(String username, String bloodGroup){
this.username = username;
this.bloodGroup = new BloodGroup(bloodGroup);
}
public boolean canAcceptBloodFrom(Human donor){
// implement (i.e. return bloodGroup.canAcceptBloodFrom(donor.bloodGroup);
// and in your bloodGroup class you can implement the logic,
// or you can implement it straight in your Human class)
}
public boolean canGiveBloodTo(Human recipient){
return recipient.canAcceptBloodFrom(this)
}
}
然后
Human recipient = new Human(usernameRecipient, bloodGroupRecipient);
Human donor = new Human(usernameDonor, bloodGroupDonor);
boolean canRecipientAcceptBloodFromDonor = recipient.canAcceptBloodFrom(donor);
然后你应该能够做到你想要的。