2016-04-08 14 views
1

eDepot sistemim için (Eclipse kullanarak) bir Giriş işlevine dahil oldum ancak bir oturum kapatma işlevi nasıl gerçekleştirileceğinden emin değilim. Araştırdıktan sonra birkaç çalıştım ama işe yaramıyor. Oturum kapatma işlevini nasıl ekleyebilirim?

Bu

benim kodudur:

package uk.ac.livjm.cms; //Name of package 

import java.util.Scanner; //Imports scanner 
import java.io.IOException; //Imports IOException 

public class Depot { //Name of Program 

    static Scanner console = new Scanner(System.in); 

    @SuppressWarnings("unused") 
    public static void main(String[] args) throws IOException { 

    String User = "SFoster"; //Username of Manager 1 
    String Pass = "001"; //Password of Manager 1 
    String Username = "BSamuel"; //Username of Manager 2 
    String Password = "002"; //Password of Manager 2 
    String User1 = "1"; //Username of Driver 1 
    String Pass1 = "111"; //Password of Driver 1 
    String Username1 = "2"; //Username of Driver 2 
    String Password1 = "222"; //Password of Driver 2 
    String User2 = "3"; //Username of Driver 3 
    String Pass2 = "333"; //Password of Driver 3 
    String inputuser; // Input when user is typing their username in 
    String inputpass; // Input when user is typing their password in 
    int menu = 0; //Displays menu 
    int count = 0; 
    int input; 

    System.out.println(
     "Are you a Manager or a Driver or are you Creating a new account?" 
      + "\n" 
      + "1. Manager" 
      + "\n" 
      + "2. Driver" 
      + "\n" 
      + "3. Creating a new account"); //Displays message 
    input = console.nextInt(); 

    if (input == 3) { 
     DepotSystem.Account(); 
    } 

    if (input == 1) { //Login for Manager 

     System.out.println("Managers Login"); //Displays message 
     System.out.println(
      "Please Note: You will only be allowed to re-enter your Username and Password three times."); //Displays message 
     while (count < 3) // 3 menu options 
     { 
     System.out.println("Enter your Username:"); //Displays message 
     inputuser = console.next(); 
     System.out.println("Enter your Password:"); //Displays message 
     inputpass = console.next(); 

     if (inputuser.equals(User) && inputpass.equals(Pass) 
      || (inputuser.equals(Username) && inputpass.equals(Password))) { 
      System.out.println(
       "You have successfully logged in." 
        + "\n" 
        + "Welcome to the Managers Section!" 
        + "\n" 
        + "Enter the Number that corresponds to the option you wish to choose:"); 
      System.out.println(
       "1. Transfer available depot to another vehicle." 
        + "\n" 
        + "2. Set up new work schedule." 
        + "\n" 
        + "3. Search for available driver and vehicle." 
        + "\n" 
        + "4. Log out."); 
      menu = console.nextInt(); 

      if (menu == 1) { 
      System.out.println("Transfer available depot to another vehicle."); 

      Transfer.transfer(); 

      count = 0; 
      break; 
      } else if (menu == 2) { 
      System.out.println("Set up new work schedule."); 

      WorkSchedule WorkSchedule = new WorkSchedule(); 
      uk.ac.livjm.cms.WorkSchedule.work(); 

      count = 0; 
      break; 
      } else if (menu == 3) { 
      System.out.println("Search for available driver and vehicle."); 

      Vehicle Vehicle = new Vehicle(); 
      uk.ac.livjm.cms.Vehicle.vehi(); 

      count = 0; 
      break; 
      } 

      if (menu == 4) { 
      System.out.println("Log out."); 
      count = 0; 
      break; 
      } else { 
      System.out.println(menu + ": Invalid option."); 
      count++; 
      } 

     } else { 
      System.out.println("Incorrect Username or Password"); 
      count++; 
     } 
     } 
    } 
    if (input == 2) { 

     System.out.println("Drivers Login"); 
     System.out.println(
      "Please Note: You will only be allowed to re-enter your Username and Password three times."); 
     while (count < 3) { 
     System.out.println("Enter your Username: "); 
     inputuser = console.next(); 
     System.out.println("Enter your Password: "); 
     inputpass = console.next(); 

     if (inputuser.equals(User1) && inputpass.equals(Pass1) 
      || (inputuser.equals(Username1) && inputpass.equals(Password1)) 
      || (inputuser.equals(User2) && inputpass.equals(Pass2))) { 
      System.out.println(
       "You have successfully logged in." 
        + "\n" 
        + "Welcome to the Drivers Section!" 
        + "\n" 
        + "Enter the Number that corresponds to the option you wish to choose: "); 
      System.out.println("1. View Work Schedule." + "\n" + "2. Log Out."); 
      menu = console.nextInt(); 

      if (menu == 1) { 
      System.out.println("View Work Schedule."); 

      WorkScheduleDriver.workdriver(); 

      count = 0; 
      break; 
      } else if (menu == 2) { 
      System.out.println("Log out."); 
      count = 0; 
      break; 
      } else { 
      System.out.println("Incorrect Username or Password"); 
      count++; 
      } 
     } 
     } 
    } 
    } 
} 

cevap

1

Kodunuz bir karmaşa içinde, bu bunu yapmak için doğru bir yol değildir, ancak koda göre sen sayım < 3 iken çalışan bir süre döngü var ve sayım sıfırı sıfırlamak yerine çıkış bölümünün içinde, 3 veya başka bir sayıya ayarlayın, böylece while döngüsünden kopar, bu da oturumu simüle eder. yani: -

else if (menu == 2) 
{ 
    System.out.println("Log out."); 
    count = 3; // set to 3 
} 

böyle count = 0; break; olarak Kodunuzdaki gereksiz mola tabloların bir sürü, ya da 3'e kadar saymak ya da bunun yerine orada gereksizdir count = 0 tek başına kırmak kullanın ve tekrar ayarlayabilirsiniz var üzerinde daha fazla ayrı bir giriş yöntemi oluşturabilir ve birkaç satırlık kodu azaltarak orada kullanıcı adı ve şifre ile karşılaştırabilirsiniz.

İlgili konular