mercredi 7 juin 2017

Issue with refactoring of a switch statement

Here is the method that performs the switch(Note The switch statement works correctly and according to the requirements)

public void performChecks() {
        priceInput = priceReader.nextDouble();
       loop: while(true){
           nameInput = nameReader.nextLine();
            switch(nameInput){
                case "A1": Cola colaItem = new Cola();
                                    if(colaItem.checkPrice(priceInput)){
                                            System.out.println("You ordered " + colaItem.getName()+ ", here is it");
                                    } else {
                                            System.out.println("Sorry, the amount is not enough for this purchase. Please add more money and try again");
                                    }
                                    break loop;

                case "B2": Chips chipsItem = new Chips();
                                    if(chipsItem.checkPrice(priceInput)){
                                        System.out.println("You ordered " + chipsItem.getName()+ ", here is it");
                                    } else {
                                        System.out.println("Sorry, the amount is not enough for this purchase. Please add more money and try again");
                                    }
                                    break loop;

                case "C3": Crackers crackerItem = new Crackers();
                                    if(crackerItem.checkPrice(priceInput)){
                                        System.out.println("You ordered " + crackerItem.getName()+ ", here is it");
                                    } else {
                                        System.out.println("Sorry, the amount is not enough for this purchase. Please add more money and try again");
                                    }
                                        break loop;
                default:
                    System.out.println("Sorry, we don't have item with such a code");
            }
        }
     }

Now I started doing some refactoring and put the method in the super class of the items(Cola, Chips and Crackers) and it looks like this:

public void performChecks(){
    inputPrice = priceReader.nextDouble();
    inputCode = codeReader.nextLine();
    initializeItems();
    for(int i = 0; i < items.length; i++){
        if(items[i].checkCode(inputCode)){
                if(items[i].checkPrice(inputPrice)){
                    System.out.println("You ordered " + items[i].getName() + " here it is");
                    break;
                } else {
                    System.out.println("Sorry, the amount is not enough for this purchase. Please add more money and try again");
                    break;
                }
        } else if(!items[i].checkCode(inputCode)){
            continue;
        } else {
            System.out.println("Sorry, we don't have item with such a code");
            inputCode = codeReader.nextLine();
        }
    }
}

The issue is the following: When I enter correct/incorrect price and incorrect item code, I should be getting the "Sorry, we don't have an item with such a code" message and the option to enter the item code again. I've ran out of ideas on how to implement this option inside the

else if(!items[i].checkCode(inputCode)){
                continue;

Since I'm pretty sure, it just gets stuck there and returns nothing(for incorrect item code).

Aucun commentaire:

Enregistrer un commentaire