Results 1 to 3 of 3
  1. #1
    überfuzz is offline Member
    Join Date
    Feb 2011
    Posts
    43
    Rep Power
    0

    Default Working with a switch

    I'm using a switch statement to handle a process. The method is called handle() and I'm having trouble getting to the right place when I want to end it. It's part of a exercise I did in school a while back. This bank was made to handle one customer in the exercise, now I'd like to expand it to be able to handle multiple customers.

    Java Code:
      public void handle()                                          //Metod given av uppgiften.
      {
        int choice;
        Scanner sc = new Scanner(System.in);
        
        while (true)
        {
          System.out.println("");
          System.out.println("******************************************");
          System.out.println("*    Hanterar kund " + customerNumber + " (" + person.getName() + ")" );
          System.out.println("*   (0) Avsluta");
          System.out.println("*   (1) Lista konton");
          System.out.println("*   (2) Skapa konto");
          //etc...
          System.out.print("*   Ditt val: ");
          if (sc.hasNextInt())
          {
            choice = sc.nextInt();
          }
          else
          {
            choice = -1;
          }
          sc.nextLine();  // Läs förbi radslutet
          switch (choice)
          {
            case 0:
              break;    //This is the choice that should end it, or better let the user choose an other customer.
            case 1:
              a_method();
              break;
            case 2:
              an_other_method();
              break;
              //etc...
          }
        }
      }
    I run this bank from a main class:

    Java Code:
        while(customer != -1)                                                       //Programmet avslutas om användaren väljer -1.
        {
          System.out.println("***********************************************************");            //Skapa en tjusig tabell.
          System.out.println("*   Välj       Kundnr.       Namn");
          System.out.println("***********************************************************");
          int numberCostumers = costumers.length;                                                       //iterera igenom de kunder som finns.
          for(int i = 0; i < numberCostumers; i++)
          {
            System.out.println("*   (" + i + ")        " + costumers[i].getCustomerNumber() + "          "+ costumers[i].getPerson().getName());      //skriv ut kunder i listan
          }
          System.out.println("***********************************************************");            //ge användaren alternativ.
          System.out.println("*   Välj en person att hantera, använd siffrorna under välj.  ");
          System.out.println("*   Avsluta programmet genom att välja -1.  ");
          System.out.println("***********************************************************");
          
          Scanner sc = new Scanner(System.in);                                                        //öppna för scanning.
          System.out.print("Välj kund: ");
          int valdKund = sc.nextInt();                                                                //läs in användarens val.
          customer = valdKund;                                                                        //sätt customer för att se in while-loopen ska fortsätta köras.
          while( (valdKund < 0 ) || (valdKund >= numberCostumers))                                    //Om användaren väljer felaktigt.
          {
            System.out.println("*    Välj bland de kunder som finns med i listan.");
            System.out.print("Välj kund: ");
            valdKund = sc.nextInt();
            if(valdKund == -1)                                                                        //Se till att användaren kan avsluta programmet inne i while-loopen.
            {
              System.out.println("Programmet avslutas");   
              System.exit(0);                                                                         //Testar System.exit(0);
            }
          } 
          costumers[valdKund].handle();                                                               //hantera den valda kunden.
    
        }
    The idea now was to add a while loop that runs the bank as long as customer -1 isn't picked. I'd be thrilled if anyone would help me out.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    Spam reported.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by überfuzz View Post
    The idea now was to add a while loop that runs the bank as long as customer -1 isn't picked. I'd be thrilled if anyone would help me out.
    You can do it in a do-while loop. Because you've to run the application at least once, or exit at the very beginning. Exit on the first step is straightforward.

Similar Threads

  1. Replies: 2
    Last Post: 01-04-2011, 05:30 AM
  2. switch
    By dj kourampies in forum New To Java
    Replies: 17
    Last Post: 01-30-2009, 06:32 PM
  3. switch
    By dj kourampies in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 09:46 AM
  4. Switch help please!!!!
    By soc86 in forum New To Java
    Replies: 6
    Last Post: 11-23-2008, 08:25 PM
  5. Replies: 8
    Last Post: 05-28-2008, 08:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •