0

This code is working, but I tried to make it loop with do..while without success.

How do I use a char as the case in a switch-case and make it loop?

/**
 * @(#)bank1.java
 *
 *
 * @Zaim Afham
 * @version 1.00 2020/10/22
 */
import java.util.*;
public class bank1 {
        
    
    public static void main(String[] args) {
        
        //variable declaration  
        char option1;
        double currentAmount;
        double wantAmount;
        char answer;
        
           
        //to allow input
        Scanner scan=new Scanner(System.in);
        
        //Menu
        System.out.println("----     BANK ATM     ----");  
        System.out.println("   Welcome to the PCP Bank ");
        System.out.println();
        
        System.out.println("Please enter your bank balance");
        System.out.print("RM ");
        currentAmount = scan.nextDouble();
        
        System.out.println();
        System.out.println("Please select your option");
        System.out.println("----  W: withdraw     ----");
        System.out.println("----  C: check balance----");
        System.out.println("----  T: transfer     ----");
        System.out.println("----  A: deposit      ----");
        System.out.println(" W  -  C  -  T  -  A   ");
        System.out.print("-> "); //prompt
        option1=scan.next().charAt(0);  //get
        
        switch (option1) {
            case 'w':
            case 'W':System.out.println("You have selected withdrawal option ");
                     System.out.print("Enter withdrawal amount: RM ");
                     wantAmount = scan.nextDouble();
                     currentAmount = currentAmount - wantAmount;
                     System.out.println();
                     System.out.println("Amount withdrew :RM" +wantAmount);
                     System.out.println("Your current amount after withdrawal :RM" +currentAmount);
                    break;
                    
            case 'c':       
            case 'C':System.out.println("Checking your balance ");
                     System.out.println();
                     System.out.println("Your current balace is: " +currentAmount);
                    break;
                    
            case 'a':       
            case 'A':System.out.println("You have selected deposit option ");
                     System.out.print("Enter deposit amount: RM ");
                     wantAmount = scan.nextDouble();
                     currentAmount = currentAmount + wantAmount;
                     System.out.println();
                     System.out.println("Amount deposited :RM" +wantAmount);
                     System.out.println("Your current amount after deposit :RM" +currentAmount);
                    break;
                    
            case 't':       
            case 'T':System.out.println("Transfer your money now ");
                     System.out.println("Please enter your transfer amount :");
                     wantAmount = scan.nextDouble();
                     currentAmount = currentAmount - wantAmount;
                     System.out.println();
                     System.out.println("Amount transferred :RM" +wantAmount);
                     System.out.println("Your current amount after Transfer :RM" +currentAmount);
                    break;
            default : System.out.println("Select correct option ");
            
            
        }   
        
    }//end class
}//end main
Federico klez Culloca
  • 22,898
  • 15
  • 55
  • 90
  • https://stackoverflow.com/a/64472531/10819573 will help you solve your problem. – Arvind Kumar Avinash Oct 22 '20 at 07:05
  • Zaim Afham - I hope the solution worked for you. Do not forget to mark the answer as accepted so that future visitors can also use the solution confidently. In order to mark the answer as accepted, you need to click the big checkmark (✓) to the left of the answer. You can also check [this](https://meta.stackexchange.com/a/5235/676168) to learn more about it. Feel free to comment in case of any doubt/issue. – Arvind Kumar Avinash Oct 22 '20 at 20:30

1 Answers1

0

In order to continue doing it repeatedly, you can use an infinite loop (while(true) { }) and break it when the user wants to exit. You can do it using a do-while loop as well but that will require you to write a couple of more lines of code.

Note that you should use option1 = scan.nextLine().charAt(0) instead of option1=scan.next().charAt(0) to avoid the problem described here.

while (true) {
    //Menu
    System.out.println("----     BANK ATM     ----");  
    System.out.println("   Welcome to the PCP Bank ");
    //...
    System.out.println("----  E: exit      ----");
    System.out.println(" W  -  C  -  T  -  A   - E");
    System.out.print("-> "); //prompt
    option1 = scan.nextLine().toUpperCase().charAt(0);
    if (option1 == 'E') {
        System.out.println("Good bye!");
        break;
    }
    switch (option1) {
        //...
    }
}

Note that you can convert the input to upper case by using toUpperCase (as shown above) which will help you get rid of using small characters in your cases.

Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72