java - 类(class)考勤系统的打印输出数组

标签 java arrays joptionpane

我正在设计一个考勤系统,我的问题是我不知道如何显示考勤的输入。这是我的系统的简介。

每个学生都有自己的矩阵编号和密码。用户将输入他们的矩阵编号和密码。如果矩阵编号和密码正确,系统将显示消息对话框。

我不知道如何保存输入然后将其显示到数组中。

源代码:

public class TESTARRAY {


    public static void main(String[] args) {

        String input1;



         // Creating array

       String[][] multi =  { {"40604", "MUHAMAD SYAMIL HAIQAL BIN KAMARUZZAMAN",""},
       {"40599","NIK MUHAMMAD 'INWAN BIN NIK ADNAN",""},
       {"37027","HARNANI BINTI ABDUL MAJID",""},
       {"37067","NURUL ASHIKIN BINTI MOHD FAZLI",""},
       {"37064","NURLIYANA BINTI MOHD KAMAL",""},
       {"37045","NADIATUL NAJIHAH BINTI MOHD NAZRI ",""},
       { "37065","NURSUHAILA BINTI MAD SALIM ",""}
       };


      printArray(multi);
   }


   public static void printArray(String[][] num1) {

       String input2;
       String input3;
       int password;
       int matricnumber;
       int a = 0;
       String  input4;



       boolean one = true;


       String[] columnNames = { "MATRIX NUMBER", "NAME","ATTENENDE"};


     String[][] input1 = new String[num1.length][num1[0].length];
      for (int i = 0; i < input1.length; i++) {
         for (int j = 0; j < input1[i].length; j++) {

             input1[i][j] = num1[i][j];
            }
           }



      DefaultTableModel model = new DefaultTableModel(input1, columnNames);
      JTable table = new JTable(model);
      JScrollPane scrollPane = new JScrollPane(table);

      JOptionPane.showMessageDialog(null, scrollPane, "CLASS ATTENDANCE",
            JOptionPane.INFORMATION_MESSAGE);






      //for (int i = 0; i < 7; i++) {

           while (one== true)
      {

         input2 = JOptionPane.showInputDialog("Please Enter Your Matric Number " );
           matricnumber = Integer.parseInt(input2);


          input3=JOptionPane.showInputDialog("Please Enter  Your Password: " );
          password = Integer.parseInt(input3);

          input4=JOptionPane.showInputDialog("Attendende : " );
          password = Integer.parseInt(input4);




          if (matricnumber==40604&&password==11){

              JOptionPane.showMessageDialog(null,"Hi "+num1[0][1]+""+num1[0][3]+" ("+input2+").\nYou have attend the class. Enjoy the class\nThank you " );

              //JOptionPane.showMessageDialog(null,"The student that attend the class for today:\n"+num1[0][1]+" ("+input2+" )");


               input4=JOptionPane.showInputDialog("Press 0 to stop or other number to continue " );
             a = Integer.parseInt(input4);

              switch(input4){

                  case"0":one=false;
                      break;


              }


          }

          else if (matricnumber==40599&&password==22){

                      JOptionPane.showMessageDialog(null,"Hi "+num1[1][1]+""+num1[1][3]+" ("+input2+").\nYou have attend the class. Enjoy the class\nThank you" );

                      //JOptionPane.showMessageDialog(null,"The student that attend the class for today:\n"+num1[1][1]+" ("+input2+" )");

                       input4=JOptionPane.showInputDialog("Press 0 to stop or other number to continue " );
              a = Integer.parseInt(input4);

              switch(input4){

                 case"0":one=false;
                   break;
              }
          }

          else if (matricnumber==37027&&password==33){

                      JOptionPane.showMessageDialog(null,"Hi "+num1[2][1]+""+num1[2][3]+" ("+input2+").\nYou have attend the class. Enjoy the class\nThank you" );
                      //JOptionPane.showMessageDialog(null,"The student that attend the class for today:\n"+num1[2][1]+" ("+input2+" )");

                       input4=JOptionPane.showInputDialog("Press 0 to stop or other number to continue " );
              a = Integer.parseInt(input4);

              switch(input4){

               case"0":one=false;
                break;
              }

          }

          else if (matricnumber==37067&&password==44){

                      JOptionPane.showMessageDialog(null,"Hi "+num1[3][1]+""+num1[3][3]+" ("+input2+").\nYou have attend the class. Enjoy the class\nThank you" );

                       //JOptionPane.showMessageDialog(null,"The student that attend the class for today:\n"+num1[3][1]+" ("+input2+" )");

                       input4=JOptionPane.showInputDialog("Press 0 to stop or other number to continue " );
              a = Integer.parseInt(input4);

              switch(input4){

                  case"0":one=false;
                      break;
              }

          }

          else if (matricnumber==37064&&password==55){

                      JOptionPane.showMessageDialog(null,"Hi "+num1[4][1]+""+num1[4][3]+" ("+input2+").\nYou have attend the class. Enjoy the class\nThank you" );
                      //JOptionPane.showMessageDialog(null,"The student that attend the class for today:\n"+num1[4][1]+" ("+input2+" )");

                       input4=JOptionPane.showInputDialog("Press 0 to stop or other number to continue " );
              a = Integer.parseInt(input4);

              switch(input4){

                  case"0":one=false;
                      break;
              }
          }

          else if (matricnumber==37045&&password==66){

                      JOptionPane.showMessageDialog(null,"Hi "+num1[5][1]+""+num1[5][3]+" ("+input2+")\nYou have attend the class. Enjoy the class\nThank you" );
                      //JOptionPane.showMessageDialog(null,"The student that attend the class for today:\n"+num1[5][1]+" ("+input2+" )");

                       input4=JOptionPane.showInputDialog("Press 0 to stop or other number to continue " );
              a = Integer.parseInt(input4);

              switch(input4){

                  case"0":one=false;
                      break;
              }
          }

         else if (matricnumber==37065&&password==77){

                      JOptionPane.showMessageDialog(null,"Hi "+num1[6][1]+""+num1[6][3]+" ("+input2+").\nYou have attend the class. Enjoy the class\nThank you" );
                      //JOptionPane.showMessageDialog(null,"The student that attend the class for today:\n"+num1[6][1]+" ("+input2+" )");

                       input4=JOptionPane.showInputDialog("Press 0 to stop or other number to continue " );
              a = Integer.parseInt(input4);

              switch(input4){

                  case"0":one=false;
                      break;
              }

      }

      else 

                      JOptionPane.showMessageDialog(null,"You are not the student in this class. GET OUT !!!" );
           input4=JOptionPane.showInputDialog("Press 0 to stop or other number to continue " );
              a = Integer.parseInt(input4);

              switch(input4){

                  case"n":one=false;
                     break;
              }




           } 


        String input5;
         String[][] ulti =  { {"40604", "MUHAMAD SYAMIL HAIQAL BIN KAMARUZZAMAN","num1[0][2]"},
       {"40599","NIK MUHAMMAD 'INWAN BIN NIK ADNAN","num2[1][3]"},
       {"37027","HARNANI BINTI ABDUL MAJID","num2[2][3]"},
       {"37067","NURUL ASHIKIN BINTI MOHD FAZLI","num2[3][3]"},
       {"37064","NURLIYANA BINTI MOHD KAMAL","num2[4][3]"},
       {"37045","NADIATUL NAJIHAH BINTI MOHD NAZRI ","num2[5][3]"},
       { "37065","NURSUHAILA BINTI MAD SALIM ","num2[6][3]"}
       };


      printA(ulti);
           }

       public static void printA(String[][] num2){ 



       String[] columnNames = { "MATRIX NUMBER", "NAME","ATTENDENCE"};


     String[][] input5 = new String[num2.length][num2[0].length];
      for (int i = 0; i < input5.length; i++) {
         for (int j = 0; j < input5[i].length; j++) {

             input5[i][j] = num2[i][j];
            }
           }



      DefaultTableModel model = new DefaultTableModel(input5, columnNames);
      JTable table = new JTable(model);
      JScrollPane scrollPane = new JScrollPane(table);

      JOptionPane.showMessageDialog(null, scrollPane, "CLASS ATTENDANCE",
            JOptionPane.INFORMATION_MESSAGE);
   }   
   }

最佳答案

您确实应该将数据提取到自己的类中,例如:

public class User {
    String name;
    int maxtrix;
    int attend;

    public User(int matrix, String name, int attend) {
        this.name = name;
        this.maxtrix = matrix;
        this.attend = attend;
    }

    public boolean checkCredentials(int matrix, int attend) {
        if (matrix == this.maxtrix && attend == this.attend) {
            return true;
        }
        return false;
    }
}

这个数据类与列表完美配合。看这个:

public static void main(String[] args) {

        List<User> users = new ArrayList<>();

        // Creating users       
        users.add(new User(40604, "MUHAMAD SYAMIL HAIQAL BIN KAMARUZZAMAN", 0));
        users.add(new User(40599, "NIK MUHAMMAD 'INWAN BIN NIK ADNAN", 0));
        users.add(new User(37027, "HARNANI BINTI ABDUL MAJID", 0));
        users.add(new User(37067, "NURUL ASHIKIN BINTI MOHD FAZLI", 0));
        users.add(new User(37064, "NURLIYANA BINTI MOHD KAMAL", 0));
        users.add(new User(37045, "NADIATUL NAJIHAH BINTI MOHD NAZRI ", 0));
        users.add(new User(37065, "NURSUHAILA BINTI MAD SALIM ", 0));

        printArray(users);
    }

现在可以更轻松地填充表格并检查用户的输入(您可以迭代用户列表并使用 checkCredentials 进行检查)

关于java - 类(class)考勤系统的打印输出数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34007745/

相关文章:

java - GridBagLayout 中超过 1 个 JPanel

c# - 只寻找现有变量

java - 为什么 JOptionPane 会导致 JFrame 从任务栏中消失?

java - 如何在 javafx 应用程序中添加许可证/安装文件夹屏幕

java - 是否可以使用反射获取类中的 transient 场

java - 对 FLINK task slot 的困惑

arrays - 如何确保 knockout 可观察数组保持排序?

c - 计算机(C 编译器或其他)如何处理 "automatic array declaration"? | C语言

java - 使用 JOptionPane 进行提醒

java - 为什么我的 JOptionPane 消息不起作用?