MMM
MMM

Reputation: 23

Use switch case with array

is it possible to use array in switch statment in this code? because I try several times and I could not, also is there is another way to declare the BloodType then assign multiple String values to it without this array? e.g.(enum Or ArrayList ?) can I use them in this case?

private String []BloodType  = new String[8];
public String[] getBloodType() {
        return BloodType;
    }

    public void setBloodType(String[] BloodType) {
        this.BloodType = BloodType;
    }
        private void BloodType()
        {
            BloodType[0]="A+";
            BloodType[1]="A-";
            BloodType[2]="B+";
            BloodType[3]="B-";
            BloodType[4]="O+";
            BloodType[5]="O-";
            BloodType[6]="AB+";
            BloodType[7]="AB-";
        }
public void BloodInfomation () // use BloodType here as Array
    {

            switch (BloodType) {
                case "A+":
                    BloodCode=1;
                    System.out.println("Blood Type A+ & Blood Code is : " + BloodCode + "No of Bags avaliable"
                            + s.checkStockType.equals("A+"));
                    break;
                case "A-":
                    BloodCode=2;
                    System.out.println("Blood Type A- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("A-"));
                    break;
                case "B+":
                    BloodCode=3;
                    System.out.println("Blood Type B+ & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("B+"));
                    break;
                case "B-":
                    BloodCode=4;
                    System.out.println("Blood Type B- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("B-"));
                    break;
                case "O+":
                    BloodCode=5;
                    System.out.println("Blood Type O+ & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("O+"));
                    break;
                case "O-":
                    BloodCode=6;
                    System.out.println("Blood Type O- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("O-"));
                    break;
                case "AB+":
                    BloodCode=7;
                    System.out.println("Blood Type AB+ & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("AB+"));
                    break;
                case "AB-":
                    BloodCode=8;
                    System.out.println("Blood Type AB- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("AB-"));
                    break;
                default:
                    System.out.println("There is no code for the type you write !!");
                    break;
            }
        }

Upvotes: 0

Views: 870

Answers (3)

Erik
Erik

Reputation: 479

Switching on an array won't work. Instead, look over the items in the array and switch on each item. To do this, you'll need to wrap the switch in a loop to iterate over the blood types in the BloodInformation method.

public void BloodInfomation () // use BloodType here as Array
{
          for(String type : BloodType) {
            switch (type) {
                case "A+":
                    BloodCode=1;
                    System.out.println("Blood Type A+ & Blood Code is : " + BloodCode + "No of Bags avaliable"
                            + s.checkStockType.equals("A+"));
                    break;
                case "A-":
                    BloodCode=2;
                    System.out.println("Blood Type A- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("A-"));
                    break;
                case "B+":
                    BloodCode=3;
                    System.out.println("Blood Type B+ & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("B+"));
                    break;
                case "B-":
                    BloodCode=4;
                    System.out.println("Blood Type B- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("B-"));
                    break;
                case "O+":
                    BloodCode=5;
                    System.out.println("Blood Type O+ & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("O+"));
                    break;
                case "O-":
                    BloodCode=6;
                    System.out.println("Blood Type O- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("O-"));
                    break;
                case "AB+":
                    BloodCode=7;
                    System.out.println("Blood Type AB+ & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("AB+"));
                    break;
                case "AB-":
                    BloodCode=8;
                    System.out.println("Blood Type AB- & Blood Code is : " + BloodCode + "No of Bags avaliable" 
                            + s.checkStockType.equals("AB-"));
                    break;
                default:
                    System.out.println("There is no code for the type you write !!");
                    break;
            }
        }
     }

Upvotes: 0

lohnns
lohnns

Reputation: 173

You're right. There is another way to implement your BloodType stuff. You mention enum and it is a better implementation for objects like blood type (with a finite number of instance variations).

enum BloodType {
    A_MINUS("A+", 1), 
    B_PLUS("B+", 2), 
    B_MINUS("B-", 3), 
    O_PLUS("AO", 4), 
    O_MINUS("O-", 5), 
    AB_PLUS("AB+", 6), 
    AB_MINUS("AB-", 7);

    BloodType(String value, int code) {
        this.value = value;
        this.code = code;
    }

    String value;
    int code;

}

void getBloodInformation(BloodType type) {
    switch (type) {
    case A_MINUS:
        // do something ...
        break;
    case O_MINUS:
        // do other things
        break;
    default:
        // default behavior if you didn't define all cases above
    }
}

That is better to use enums with switches. This way you cannot miss a case and you avoid potential errors due to wrong string spelling.

Upvotes: 2

Justin Tang
Justin Tang

Reputation: 21

The array works fine to store the blood types but your comparison will not work. In your switch you are comparing cases which are Strings to a String[]. What I think you want is two separate variables one that is the blood type array and another which is the specific blood type of the object you are referring too.

public class person{
//Defining the array of possible blood types which
private String[] BloodTypeArray = new String[8];
private void BuildBloodTypeArray(){
    BloodTypeArray[0]="A+";
    BloodTypeArray[1]="A-";
    BloodTypeArray[2]="B+";
    BloodTypeArray[3]="B-";
    BloodTypeArray[4]="O+";
    BloodTypeArray[5]="O-";
    BloodTypeArray[6]="AB+";
    BloodTypeArray[7]="AB-";
}
//Defining the specific blood type of this instance
private String BloodType;
public String getBloodType(){
    return BloodType;
}
public void setBloodType(String BloodType){
    this.BloodType = BloodType;
}
//The switch needs something to compare the case to so it takes an input string
public void BloodInformation(){
    int BloodCode;
    switch(this.BloodType){
        case "A+":
        BloodCode=1;
        System.out.println("Blood Type A+ & Blood Code is : " + BloodCode + " No of Bags avaliable" );
        break;
    case "A-":
        BloodCode=2;
        System.out.println("Blood Type A- & Blood Code is : " + BloodCode + " No of Bags avaliable" );
        break;
    case "B+":
        BloodCode=3;
        System.out.println("Blood Type B+ & Blood Code is : " + BloodCode + " No of Bags avaliable");
        break;
    case "B-":
        BloodCode=4;
        System.out.println("Blood Type B- & Blood Code is : " + BloodCode + " No of Bags avaliable");
        break;
    case "O+":
        BloodCode=5;
        System.out.println("Blood Type O+ & Blood Code is : " + BloodCode + " No of Bags avaliable");
        break;
    case "O-":
        BloodCode=6;
        System.out.println("Blood Type O- & Blood Code is : " + BloodCode + " No of Bags avaliable");
        break;
    case "AB+":
        BloodCode=7;
        System.out.println("Blood Type AB+ & Blood Code is : " + BloodCode + " No of Bags avaliable");
        break;
    case "AB-":
        BloodCode=8;
        System.out.println("Blood Type AB- & Blood Code is : " + BloodCode + " No of Bags avaliable");
        break;
    default:
        System.out.println("There is no code for the type you write !!");
        break;
    }
}
public static void main(String[] args){
    person p = new person();
    p.BuildBloodTypeArray();
    p.setBloodType("AB-");
    System.out.println(p.getBloodType());
    p.BloodInformation();

}

Here is an example using the code you have posted and a class called person. Also notice that a loop can be used in this case rather than a switch because of all the repete code. You could replace your switch with this.

public void BloodInformation(){
    int BloodCode;
    for(int i=0; i<BloodTypeArray.length; i++){
        BloodCode = i+1;
        if(this.BloodType == BloodTypeArray[i]){
            System.out.println("Blood Type "+ BloodTypeArray[i] + " & Blood Code is : " + BloodCode + " No of Bags avaliable" );
            return;
        }
    }
    System.out.println("There is no code for the type you write !!");
}

Upvotes: 0

Related Questions