Don't
Don't

Reputation: 51

My switch statement is not running (It was working yesterday but IDK what happened) JAVA

I dont know what i did but hen i run the main program it is not going through the switch statement. It was working last night and i haven't changed it in anyway i only added a print statement for debugging purposes. This is apart of a POS system i have to do for a class assignment.

package MultiBuy;

import static POS.POS_System.NonGSTVodka;
import static POS.POS_System.PRICE_FORMAT;
import static POS.POS_System.Vodka;
import static POS.POS_System.btnCancelPrevious;
import static POS.POS_System.nPreviousPrice;
import static POS.POS_System.nTotal;
import static POS.POS_System.strPreviousDrink;
import static POS.POS_System.txtBill;
import static POS.POS_System.spaces;

public class MultiBuy {

    public static int clicked;
    public static double DiscountAmt = 0.05;
    public static double Discount_PRICE_Vodka = 0.385;
    public static double NewVodkaPrice;

    public static void MultiBuy(){
                         POS.POS_System.btnVodkaPressed = true;
                         System.out.println("No Cases Ran");
                 switch(clicked){
                 case 1:
                     if(clicked == 0){
                     //Plus 1 to clicked to create event
                         clicked++;
                                 System.out.println("Case 1 Completed");
                                 break;
                    }

                case 2 :
                     if(clicked >= 2 && POS.POS_System.btnVodkaPressed == true){
                                     txtBill.setText(txtBill.getText() + "\n" +
                    "     " + strPreviousDrink + 
                    spaces(40 - strPreviousDrink.length()) + "-" +
                    PRICE_FORMAT.format(nPreviousPrice) + "\n" +  "     (Canceled)\n");
                nTotal -= nPreviousPrice + NewVodkaPrice;
                btnCancelPrevious.setEnabled(false);
                     NewVodkaPrice = Vodka - Discount_PRICE_Vodka;
                     POS.POS_System.txtBill.setText(POS.POS_System.txtBill.getText() + "\n" + "Multibuy Special = " + MultiBuy.Discount_PRICE_Vodka);
                                 POS.POS_System.txtBill.setText(POS.POS_System.txtBill.getText() + "New Price = " + NewVodkaPrice);
                                 POS.POS_System.nTotal = POS.POS_System.nTotal;
                     clicked--;
                                 System.out.println("Case 2 Completed");//Should equal 3
                                 break;
                                 }
                        case 3 :
                                 if(clicked <= 1){
                                 clicked++;
                                 System.out.println("Case 3 Completed");//Should equal 2
                                 break;
                                 }
                 }
            }
        }

Upvotes: 1

Views: 74

Answers (1)

modesitt
modesitt

Reputation: 7210

There are a couple issues first. Notably, you never initialized the property (or private instance variable) of clicked, maybe you do it elsewhere? This probably needs to be done in your constructor (the job of the constructor is to initialize the private instance variables). From what I can see, the integer (a primitive type) will be 0. Anyway, I also don't believe you're utilizing a switch statement correctly. Switch is nothing more than syntactical candy for if statements.

For example, the following code blocks are identical

If statement:

public int foo = 0;
if(foo == 1) {
    // do things
} else if (foo == 2) {
   // do things
} else {
   // do things
}

Switch:

public int foo = 0;
switch(foo) {
    case 1: 
         // do things
         break;
    case 2: 
         // do things
         break;
    default:
         // do things
         break;
}

The reason for the break keyword being crucial in java is explained well here if you would like to know about that.

Note that in java you can only switch on a variable that is an int. Other languages allow you to switch on all primitive types and even custom objects! Moving on...

For these reasons, the switch statements you wrote would never be processed, as those if blocks are irrelevant and will never execute the way you have it setup. From what I can tell, here is your edited code block as to what I think you want to happen (though I can't really tell what you want to do). Given what I explained above, however, i'm sure you can work it out with more ease.

// your imports

public class MultiBuy {

    public static int clicked;
    public static double DiscountAmt = 0.05;
    public static double Discount_PRICE_Vodka = 0.385;
    public static double NewVodkaPrice;

    public static void MultiBuy(){
        POS.POS_System.btnVodkaPressed = true;
        System.out.println("No Cases Ran");

        // initialize clicked!!!! 

        clicked = 3;

        switch(clicked){
            case 0:
            clicked++;
            System.out.println("Case 1 Completed");
            break;

            default: 
             txtBill.setText(txtBill.getText() + "\n" +
                    "     " + strPreviousDrink + 
                    spaces(40 - strPreviousDrink.length()) + "-" +
                    PRICE_FORMAT.format(nPreviousPrice) + "\n" +  "     (Canceled)\n");
                nTotal -= nPreviousPrice + NewVodkaPrice;
                btnCancelPrevious.setEnabled(false);
                NewVodkaPrice = Vodka - Discount_PRICE_Vodka;
                POS.POS_System.txtBill.setText(POS.POS_System.txtBill.getText() + "\n" + "Multibuy Special = " + MultiBuy.Discount_PRICE_Vodka);
                POS.POS_System.txtBill.setText(POS.POS_System.txtBill.getText() + "New Price = " + NewVodkaPrice);
                POS.POS_System.nTotal = POS.POS_System.nTotal;
                clicked--;
                System.out.println("Case 2 Completed");//Should equal 3
            break;
        }
    }
}

Hope that helped :)

Upvotes: 2

Related Questions