2016-04-05 5 views
-1

私はJavaとプログラミングには初めてです。私は、どのログインが使用されているかに応じて2つの異なるメニューを表示する2つの異なるタイプのログインを作成する必要がある、割り当てられた課題の1つのセクションについています。私はEclipseとコンソールを使用しています。2人の異なるタイプのログインと別個のメニュー

ユーザーの種類はBossとWorkerで、ユーザー名とパスワードを使用してログインする必要があります。

  • 移動労働者が
  • ワーカーメニューは、ログインした後、次のメニューオプションを持っている必要がありますスケジュール

    1. セットアップワーカースケジュール
    2. ビューワーカー:ボスメニューは、ログイン後、以下のメニューオプションを持っている必要がありますin:

      本当にありがとうございました。ありがとうございました。

      EDIT: わかりましたので、私は今、次のコードを持っている:

      if (userName.equals(bossName) && passWord.equals(bossPassword)) { 
      

      else if (userName.equals(workerName) && passWord.equals(workerPassword)) { 
      
      :しかし

      import java.util.Scanner; 
      
      public class Depot { 
          public static void main(String[] arguments){ 
      
           String bossName; 
           String bossPassword; 
           String workerName; 
           String workerPassword; 
      
           System.out.println("Enter your name: "); 
           Scanner authenticate = new Scanner(System.in); 
           String userName = authenticate.nextLine(); 
           System.out.println("Your username is " + userName); 
      
           System.out.println("Enter your password: "); 
           String passWord = authenticate.nextLine(); 
           System.out.println("Your password is " + passWord); 
      
           if (userName.equals(bossName) && passWord.equals(bossPassword)) { 
      
            int selection; 
      
            Scanner bossMenu = new Scanner(System.in); 
            System.out.println("1. Setup Worker Schedule"); 
            System.out.println("2. View Worker Schedule"); 
            System.out.println("3. Move Worker"); 
            System.out.println("4. Quit"); 
      
            do { 
             selection = bossMenu.nextInt(); 
      
             if (selection == 1) { 
              System.out.println("1"); 
             } 
             else if (selection == 2) { 
              System.out.println("2"); 
             } 
             else if (selection == 3) { 
              System.out.println("3"); 
             } 
             else { 
              System.out.println("4"); 
             } 
            } 
             while(selection != 4); 
             bossMenu.close(); 
           } 
           else if (userName.equals(workerName) && passWord.equals(workerPassword)) { 
      
            int selection; 
      
            Scanner userMenu = new Scanner(System.in); 
            System.out.println("1. View Worker Schedule"); 
            System.out.println("2. Quit"); 
      
            do { 
             selection = userMenu.nextInt(); 
      
             if (selection == 1) { 
              System.out.println("1"); 
             } 
            } 
             while(selection != 2); 
             userMenu.close(); 
           } 
          } 
      } 
      

      、次の2つのコード行は私にエラーを与えています

      bossName、bossPassword、workerNameおよびworkerPasswordが初期化されていない可能性があります。 d?

    +0

    ログイン情報を収集します。通常は、ユーザ名がその人が上司かワーカーかを示すかどうかに応じてメニューを表示するために 'if'ステートメントを使います。実際にはオブジェクト指向を使用し、操作を別のクラスに分けることもできます。ログインの一部が役割を選択していない限り、あなたはBossである人に関する情報を保持する必要があります。 – KevinO

    +0

    クイック返信をありがとう。ログイン情報は、Bossの場合は1つのユーザー名とパスワード、Workerの場合は1つのユーザー名とパスワードでハードコードすることができます。あなたはどこから始めるべきかわからないので私に例を示すことができますか? –

    答えて

    0

    まず、スキャナを使用して資格情報を取得し、ここでスキャナオブジェクトを構築するための基本的な方法があり、あなたが何かする前に、あなたのコードの先頭に次のimport文を持っている必要があります:

    import java.util.Scanner; 
    

    は以下を実行し、スキャナを作成するには、次のキーボードになります入力ストリームから読み込むためのスキャナに指示します

    Scanner scannerName = new Scanner(System.in); 
    

    を。スキャナからデータを取得するには、まずユーザに必要なデータを入力してから、Scannerの.next ___メソッドの1つを使用して入力を取得し、変数に格納します。どのAPIを使用するかは教えてもらえません.Java APIのScannerページをチェックし、自分で調べることができるかどうかを確認してください。

    それは次のようになります。

    System.out.println("Enter your name"); 
    String userLoginString = scannerName.next____(); 
    
    System.out.println("Enter your password"); 
    String userPasswordString = scannerName.next____(); 
    

    あなたは文字列変数に保存されている資格情報を持っていたら、私はあなたには、いくつかの保存された値に対してこれらの資格情報を検証する必要があります、例としてuserLoginStringとuserPasswordStringを使用します。したがって、文字列変数bossName、bossPassword、workerName、workerPasswordを作成します。

    ユーザーの資格情報を取得したら、これらのログイン資格情報の検証を実行します。あなたはそうのように、論理演算子とStringクラスのメソッドを使用していることを行うことができます:

    if (userLoginString.equals(bossName) && userPasswordString.equals(bossPassword)) { 
        // print the boss menu 
    } 
    else if (userLoginString.equals(workerName) && userPasswordString.equals(workerPassword)) { 
        // print the user menu 
    } 
    

    論理& &(「と」)演算子は、正しいメニューがユーザーの資格情報が一致した場合にのみ表示されるようになります格納された資格情報ユーザーが正しい名前(上司または従業員)を入力したにもかかわらず間違ったパスワード(またはその逆)を入力した場合、中括弧内のステートメントは実行されません。

    UPDATEはここで、これまでそれを改善する方法についてのいくつかのヒントを使用して、コードのコメントバージョンです。これは、コンパイルして正常に動作あなただけの上部にある文字列変数の値を提供する場合、私はそれが少しよりよいするためにいくつかのより多くの提案があります:再度更新

    import java.util.Scanner; 
    
    public class Depot { 
        public static void main(String[] arguments){ 
    
         // you need to initialize these to some value or else there is 
         // nothing to compare them with. I tried some dummy values and 
         // your code worked as expected, as long as the user entered the 
         // correct values in the prompt. 
         String bossName; 
         String bossPassword; 
         String workerName; 
         String workerPassword; 
    
         // you can just use one Scanner for the whole program, since they are 
         // both just reading input from the standard input stream. Replace the 
         // other Scanners with "input" and close "input" at the end 
         Scanner input = new Scanner(System.in); 
    
         System.out.println("Enter your name: "); 
         // not needed 
         Scanner authenticate = new Scanner(System.in); 
         String userName = authenticate.nextLine(); 
         System.out.println("Your username is " + userName); 
    
         System.out.println("Enter your password: "); 
         String passWord = authenticate.nextLine(); 
         System.out.println("Your password is " + passWord); 
    
         if (userName.equals(bossName) && passWord.equals(bossPassword)) { 
    
          // this could be declared at the top of the program instead of 
          // redeclaring in the if...else 
          int selection; 
    
          Scanner bossMenu = new Scanner(System.in); 
          System.out.println("1. Setup Worker Schedule"); 
          System.out.println("2. View Worker Schedule"); 
          System.out.println("3. Move Worker"); 
          System.out.println("4. Quit"); 
    
          do { 
           selection = bossMenu.nextInt(); 
    
           if (selection == 1) { 
            System.out.println("1"); 
           } 
           else if (selection == 2) { 
            System.out.println("2"); 
           } 
           else if (selection == 3) { 
            System.out.println("3"); 
           } 
           else { 
            System.out.println("4"); 
           } 
          } while(selection != 4); // this is usually here 
           bossMenu.close(); 
         } 
         else if (userName.equals(workerName) && passWord.equals(workerPassword)) { 
    
          // this could be declared at the top of the program instead of 
          // redeclaring in the if...else 
          int selection; 
    
          // don't need this one, just use "input" Scanner 
          Scanner userMenu = new Scanner(System.in); 
          System.out.println("1. View Worker Schedule"); 
          System.out.println("2. Quit"); 
    
          do { 
           selection = userMenu.nextInt(); 
    
           if (selection == 1) { 
            System.out.println("1"); 
           } 
          } while(selection != 2); // this is usually here 
    
          // you would close the "input" Scanner here 
          userMenu.close(); 
         } 
        } 
    } 
    

    を! BossとWorkerを実装するより良い方法は、継承と多相を使うことです。ボスとワーカーの共通の特徴を持つ抽象スーパークラスから始めましょう。これをEmployeeスーパークラスと呼んでいます。それはfirstNameの、lastNameの、およびパスワードのインスタンス変数を持っている、とあなたはそれぞれにゲッターとセッターを追加する必要があります

    // abstract class, CANNOT be instantiated but can be used as the supertype 
    // in an ArrayList<Employee> 
    public abstract class Employee { 
    
        private String firstName; 
        private String lastName; 
        private String password; 
    
        public Employee() { 
         // don't have to do anything, just need this so you can instantiate 
         // a subclass with a no-arg constructor 
        } 
    
        // constructor that takes only the name of the Employee 
        public Employee(String firstName, String lastName) { 
         this(firstName, lastName, null); 
        } 
    
        // constructor that takes name and password 
        public Employee(String firstName, String lastName, String password) { 
         this.firstName = firstName; 
         this.lastName = lastName; 
         this.password = password; 
        } 
    
        public String getFirstName() { 
         return firstName; 
        } 
    
        public void setFirstName(String firstName) { 
         this.firstName = firstName; 
        } 
    
        // and so on, for the lastName and password.... 
    
        // you must implement this specifically in any subclass! 
        public abstract void getMenu(); 
    } 
    

    その後、上司やワーカークラスの可能性があり、このEmployeeクラスを拡張し、それらが同じメソッドのすべてを持っているでしょうし、インスタンス変数。それぞれがEmployeeクラスの抽象クラスだったので、オーバーライドされたgetMenu()メソッドをそれぞれ提供する必要があります。ここにあなたのボスクラスがどのように見えるかのサンプルは、あなたがgetMenu()を実装する必要がある自分自身とワーカークラス:あなたは、従業員、労働者、およびボスクラスを持っていたら

    public class Boss extends Employee { 
    
        // notice we don't need the instance variables in the class declaration, 
        // but they are here since they are part of Employee 
    
        public Boss() { 
         // don't need to do anything here, just allows no-arg constructor 
         // to be called when creating a Boss 
        } 
    
        // just calls the superclass constructor, could do more if you want 
        public Boss(String firstName, String lastName) { 
         super(firstName, lastName); 
        } 
        // just calls the superclass constructor, could do more if you want 
        public Boss(String firstName, String lastName, String password) { 
         super(firstName, lastName, password); 
        } 
    
        @Override 
        public void getMenu() { 
         // put the print statment for Boss's menu here 
        } 
    
        // don't need to re-implement other methods, we can use them since 
        // they are part of the superclass 
    } 
    

    が、あなたは準備が整いましたこれまでのように単純な変数の代わりにオブジェクトにプログラムを書き直して書き直すことができます。それがどのように始まるのかの例があります:

    import java.util.Scanner; 
    
    public class EmployeeTester { 
        public static void main(String[] args) { 
    
         Scanner input = new Scanner(System.in); 
    
         // can make workers and bosses able to be processed polymorphically 
         // by assinging their references to Employee variables, since 
         // and Employee is the supertype of each, a Worker "is an" Employee 
         // and a Boss "is an" Employee. 
         Employee worker1 = new Worker("Bob", "Worker"); 
         Employee worker2 = new Worker("Sue", "Bush", "Password1"); 
         Employee worker3 = new Worker(); 
         Employee boss1 = new Boss("Jenny", "Boss"); 
         Employee boss2 = new Boss("Bill", "OtherBoss", "Password2"); 
         Employee boss3 = new Boss(); 
    
         // if you're going to have a lot of workers and bosses, and you don't 
         // need named variables for each because their info will be included 
         // in their constructors, you could do this 
         Employee[] employees = {new Worker("Bob", "Bailey", "myPassword"), 
               new Worker("Sue", "Sarandon", "123Seven"), 
               new Boss("Jenny", "Strayhorn", "hardPassword"), 
               new Boss("Billy", "MeanGuy", "pifiaoanaei")}; 
    
         // then, you could iterate through this list to check if a password 
         // entered matches a firstName, lastName, and password combination 
         // for ANY type of employee in the array, then call the getMenu() 
         // method on that employee, like so: (This could all be in a loop 
         // if you wanted to process multiple Employees...) 
    
         System.out.println("Enter firstName:"); 
         // you figure out which Scanner method to use! 
         String firstName = input._____(); 
    
         System.out.println("Enter lastName:"); 
         String lastName = input._____(); 
    
         System.out.println("Enter password:"); 
         String password = input._____(); 
    
         // figure out what get____() method of the Employee class 
         // needs to be called in each case, and what it should be 
         // compared to with the .equals() method. 
         for (int i = 0; i < employees.length; i++) { 
          if (employees[i].get______().equals(______) && 
           employees[i].get______().equals(______) && 
           employees[i].get______().equals(______)) { 
    
           // if all of those conditions are true, print the menu 
           // for this employee 
           employees[i].get_____(); 
    
           // you could do more stuff here.... 
    
           // breaks out of the loop, no need to check for anymore employees 
           break; 
          } 
         } 
        } 
    } 
    
    +0

    クイック返信をありがとう。私はスキャナに精通していません。ログイン情報は、Bossの場合は1つのユーザー名とパスワード、Workerの場合は1つのユーザー名とパスワードでハードコードすることができます。あなたはどこから始めるべきかわからないので私に例を示すことができますか? –

    +0

    @ThreeLegsコードを編集して、Scannerの仕組みの例を少し紹介しました。 Java APIをチェックするか、シンプルなチュートリアルを使って使い方を理解してください。基本的なユーザー入力のために、あなたは多くのスキャナを使用します! –

    +0

    助けてくれてありがとう。私はこれまでに持っていたコードを含めるように質問を編集しました。しかし、私はエラーがあります。手伝ってくれる? –

    関連する問題