1

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;
                                 }
                 }
            }
        }
Diogo Rocha
  • 9,759
  • 4
  • 48
  • 52
Don't
  • 51
  • 5
  • 3
    How can you have `switch(clicked) { case 1: ...` and then text if `clicked == 0`. Wouldn't click *have* to be 1 to be in that case statement? – KevinO Apr 25 '16 at 02:12
  • You create a public static int "clicked" but you never instantiate it. Unless another method is setting the value of "clicked", your switch statement will not know what to do because it is currently NULL and the switch statement does not have a default case. Furthermore, Kevin is right, your case number will be the value of "clicked". – almostcolin Apr 25 '16 at 02:15
  • 1
    @almostcolin, It is true we don't know the value of `clicked` as it is static, and one assumes it is set elsewhere. However, minor point, since it is an `int` it would be the integer value of `0`, not `null`. – KevinO Apr 25 '16 at 02:16
  • @Kevin0 I figured it out i must of commented out the line that initiates the switch(LOL) I missed that this morning, Thx for the help guys – Don't Apr 25 '16 at 02:24
  • @Don't I also added some clarification about switch statements as an aswer if you find that helpful. – modesitt Apr 25 '16 at 02:30
  • I think you should read about switch cases. You believe that "case 1" means the first case, "case 2" means second case and so on. These numbers are not about order of cases, they are about the value of 'clicked' variable. – Raptor Apr 25 '16 at 08:18

1 Answers1

2

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 :)

Community
  • 1
  • 1
modesitt
  • 7,052
  • 2
  • 34
  • 64