user10255954
user10255954

Reputation:

Confused with Switch statement in Java

Can someone please tell me why Case 1 is not showing updated balance after a deposit (Case 2) is made.

  1. I have declared and initialized the variables before the switch statement.
  2. Case 1 - shows the balance in each account.
  3. Case 2 - when a deposit is made, it add the deposit to the variables soldesCompteCheques and soldesCompteEpargne

Issue - when I check the balance (case 1) after a deposit is made. It still shows me the original balance (i.e 250 and 1000).

Thanks

public static void traiterMenu(int optionChoisie) {
    double soldesCompteCheques;
    double soldesCompteEpargne;
    double depot;
    int compte;

    soldesCompteCheques = 250;
    soldesCompteEpargne = 1000;

    switch (optionChoisie) {
        case 1:
            System.out.println("Vous avez choisi option 1.");
            System.out.println("Votre soldes est:");
            System.out.println(String.format("%-30s %,12.2f$", "  >> Compte Chèque", soldesCompteCheques));
            System.out.println(String.format("%-30s %,12.2f$", "  >> Compte Épargne", soldesCompteEpargne));
            break;
        
        case 2:
            System.out.println("Vous avez choisi option 2.");
            System.out.println("Entrez un montant de dépôt:");
            depot = clavier.nextDouble();
            System.out.println("Dans quel compte voulez vous effectuer le dépôt?");
            System.out.println("1. Chèque");
            System.out.println("2. Épargne");
            compte = clavier.nextInt();
            switch (compte) {
                case 1:
                    soldesCompteCheques += depot;
                    System.out.println(String.format("Votre dépôt est completé. \n"
                                                   + "Le solde de votre compte Cheque est %,.2f$", soldesCompteCheques));
                    break;
                case 2:
                    soldesCompteEpargne += depot;
                    System.out.println(String.format("Votre dépôt est completé. \n"
                                                   + "Le solde de votre compte Epargne est %,.2f$", soldesCompteEpargne));
                    break;
                default:
                    System.out.println("Saisie erroneée.");
                    break;
            }
            break;
        
        default:
            System.out.println("Saisie erronnée. Veuillez réessayer.");
            break;
    }

}

Upvotes: 0

Views: 60

Answers (2)

Sheldom
Sheldom

Reputation: 21

soldesCompteCheques, soldesCompteEpargne are local variables of that function so every time you call that function they will get initialized with 250,1000 thats why you are seeing the same values even after making deposit. so in order for them to get updated make them global variables.

Upvotes: 1

rzwitserloot
rzwitserloot

Reputation: 103813

Variables declared within methods exist for the entire method invocation and are eliminated immediately afterwards. If you enter the method once more, you get a brand, fresh, new copy. Which you then immediately set to 250 / 1000.

Basically, your case 2 will set these values to something else, the method exits, and with that, your changes just disappear into the air.

If you want long-term stuff to remain, you need fields, not local variables. Move double compte and friends out to be directly within the class X{} declaration, get rid of static everywhere in your entire code base (except main, which is the one use of static you get, and its content should be one line only: new MyApp().go(). Then make sure that 250/1000 init goes along with it to the field decl, and is removed from the body of this method.

Upvotes: 1

Related Questions