迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

Java 中的名册应用程序

作者:迹忆客 最近更新:2023/08/06 浏览次数:

本文介绍如何使用 Java 开发名册应用程序。


Java 中的名册应用程序

名册申请提供了候选人记录的详细信息或某些候选人必须执行工作的订单详细信息。 Roster 应用程序用于维护任何内容的记录。

我们创建了一个简单的名册应用程序示例,用于学生记录; 该应用程序可以添加/更新、查看和删除学生的记录。


Java 中的学生名册应用程序

如上所述,我们创建了一个简单的名册应用程序,仅限于一些记录维护操作。 名册应用程序可以包含与用于记录维护的选项一样多的选项。

我们的应用程序执行以下操作:

  1. 添加/更新 - 添加新生记录,其中包括三个年级的姓名; 它还更新了以前学生的记录。
  2. 删除 - 只需输入姓名即可删除学生记录。
  3. 查看 - 查看成绩最高的学生的记录。
  4. 显示 - 在主页上显示学生的成绩列表。

我们的应用程序包含两个类; 一是创建学生记录,二是维护学生记录。 该系统基于Java Swing。

让我们看看学生名册应用程序的代码:

代码-CreateStudent.java:

package jiyik;

public class CreateStudent {

    private String Student_Name;
    private int [] Student_Grades;

    // Default constructor
    public CreateStudent() {
        Student_Name = "";
        for (int i = 0; i < Student_Grades.length; i++)
        {
            Student_Grades[i] = 0;
        }
    }

    // Constructor with inputs
    public CreateStudent(String Name1, int [] Grade1) {
        Student_Grades = new int[3];
        SetStudentName(Name1);
        SetStudentGrades(Grade1);
    }

    // Return the name of the students
    public String GetStudentName() {
        return Student_Name;
    }

    // Return the grades of the student
    public int [] GetStudentGrades() {
        return Student_Grades;
    }

    // To store the Name
    public void SetStudentName(String Name1) {
        Student_Name = Name1;
    }

    // To store or set the grades
    public void SetStudentGrades(int [] Grade2) {
        for(int i = 0; i < Grade2.length; i++) {
            Student_Grades[i] = Grade2[i];
        }
    }

    // Return the name and grades in string
    public String ConverttoString() {
        return(Student_Name + " " + Student_Grades[0] + " " + Student_Grades[1] + " " + Student_Grades[2]);
    }

    // Returns the top grade
    public int GetTopGrade(int [] Grade3) {
        int top = Grade3[0];

        for (int x = 0; x < Grade3.length; x++) {
            if (top < Grade3[x]) {
                top = Grade3[x];
            }
        }
        return top;
    }
}

上面的类用于创建和检索学生记录。 它还返回特定学生的最高成绩。

代码-MaintainStudent.java:

package jiyik;

import javax.swing.*;
import java.text.*;
import java.util.Vector;

public class MaintainStudent {
    // to determine the user input errors
    static int Error_Flag = 0;

    public static void main(String args[]) {
        // The default value for grades
        int [] Grade1 = {98, 99, 95};
        int [] Grade2 = {89, 93, 75};
        int [] Grade3 = {50, 45, 65};

        // The default Student record
        CreateStudent Student1 = new CreateStudent("Jack Sparrow", Grade1);
        CreateStudent Student2 = new CreateStudent("Frank Morgan", Grade2);
        CreateStudent Student3 = new CreateStudent("Forest Gump", Grade3);

        //Create A vector of 'students' objects
        Vector Students_Vector = new Vector();
        Students_Vector.add(Student1);
        Students_Vector.add(Student2);
        Students_Vector.add(Student3);

        String Option_Choice = "";
        String Option_Message = "";
        String Option_Text = "";
        String Option_Input = "";
        String Option_Record = "";

        // To keep track of the top grade
        int Top_Grade = 0;

        // To Hold the student's grade
        int [] Students_Grade = {0,0};
        int Student_Index1 = 0;
        int Student_Index2 = 0;
        int Student_Index3 = 0;
        String Student_Grade_1 = "";
        String Student_Grade_2 = "";
        String Student_Grade_3 = "";
        int Gr_1 = 0;
        int Gr_2 = 0;
        int Gr_3 = 0;
        String Student_Name = "";

        // To validate of input
        boolean Retry_Student = false;
        // To validate if the name is found
        boolean Found_Student = false;

        try {
            while(!(Option_Choice.equalsIgnoreCase("Exit"))) {
                Option_Message = "";
                for(int i = 0; i < Students_Vector.size(); i++) {
                    Option_Message = Option_Message + ((CreateStudent)Students_Vector.get(i)).ConverttoString() + "\n";
                }

                Option_Message = Option_Message + "\n\n\nType 'Add' press 'Ok' to add a student\nType 'Delete' and press 'Ok' to delete a student\nType 'View' and press 'Ok' to view a student's record\nType 'Exit' and press 'Ok' to exit:";

                Option_Choice = JOptionPane.showInputDialog(null, Option_Message, "Students Roster Application", JOptionPane.QUESTION_MESSAGE);

                if (Option_Choice.equalsIgnoreCase("Add")) {
                    try {
                        Option_Input = JOptionPane.showInputDialog(null, "Please enter student's name and grades\nwith no spaces between entries:\nFor Example: Sheeraz,87,88,99", "Input", JOptionPane.QUESTION_MESSAGE);
                        Option_Input = Option_Input.trim();

                        // Reset the flag
                        Error_Flag = 0;

                        Retry_Student = Input_Validation(Option_Input);

                        if (1 == Error_Flag) {
                            JOptionPane.showMessageDialog(null, "Wrong Entry: Please Enter a name which begins with a Letter!\nPlease click OK to retry.", "Error", JOptionPane.ERROR_MESSAGE);
                        }
                        else if (2 == Error_Flag) {
                            JOptionPane.showMessageDialog(null, "Wrong Entry: The student's grade is missing!\nPlease click OK to retry.", "Error", JOptionPane.ERROR_MESSAGE);
                        }
                        else if (3 == Error_Flag) {
                            JOptionPane.showMessageDialog(null, "Worng Entry: Student's grades digit limit exceeded, please enter grades with three digits.!\nPlease click OK to retry.", "Error", JOptionPane.ERROR_MESSAGE);
                        }
                        else if (4 == Error_Flag) {
                            JOptionPane.showMessageDialog(null, "Worng Entry: Please enter a Student name it cannot be blank or ommitted!\nPlease click OK to retry.", "Error", JOptionPane.ERROR_MESSAGE);
                        }
                        else if (5 == Error_Flag) {
                            JOptionPane.showMessageDialog(null, "Wrong Entry: Please do not add Blank spaces \nbefore or after the full name, commas and grades!\nPlease click OK to retry.", "Error", JOptionPane.ERROR_MESSAGE);
                        }

                        // Check if the first input was invalid
                        if (true == Retry_Student) {
                            // ask the user for valid input
                            while(true == Retry_Student) {
                                Option_Input = JOptionPane.showInputDialog(null, "Please Re-Enter student's info\nwith no spaces between entries:\nFor example: (Sheeraz Gul,87,88,99)", "Input", JOptionPane.QUESTION_MESSAGE);
                                Option_Input = Option_Input.trim();

                                Retry_Student = Input_Validation(Option_Input);

                                if (1 == Error_Flag) {
                                    JOptionPane.showMessageDialog(null, "Wrong Entery: Please Enter a name which begins with a Letter!\nPlease click OK to retry.", "Error", JOptionPane.ERROR_MESSAGE);
                                }
                                else if (2 == Error_Flag) {
                                    JOptionPane.showMessageDialog(null, "Wrong Entry: The student's grade is missing!\nPlease click OK to retry.", "Error", JOptionPane.ERROR_MESSAGE);
                                }
                                else if (3 == Error_Flag) {
                                    JOptionPane.showMessageDialog(null, "Worng Entry: Student's grades digit limit exceeded, please enter grades with three digits.!\nPlease click OK to retry.", "Error", JOptionPane.ERROR_MESSAGE);
                                }
                                else if (4 == Error_Flag) {
                                    JOptionPane.showMessageDialog(null, "Worng Entry: Please enter a Student name it cannot be blank or ommitted!\nPlease click OK to retry.", "Error", JOptionPane.ERROR_MESSAGE);
                                }
                                else if (5 == Error_Flag) {
                                    JOptionPane.showMessageDialog(null, "Wrong Entry: Please do not add Blank spaces \\nbefore or after the full name, commas and grades!\nPlease click OK to retry.", "Error", JOptionPane.ERROR_MESSAGE);
                                }

                                // Reset the flag
                                Error_Flag = 0;
                            }
                        }
                    }
                    catch(IndexOutOfBoundsException e) {
                        Option_Input = JOptionPane.showInputDialog(null, "Please Re-Enter the student's Infor \nwith no spaces between entries:\nFor example: (Sheeraz,87,88,99)", "Input", JOptionPane.QUESTION_MESSAGE);
                        Option_Input = Option_Input.trim();
                    }

                    // All the validation is done above; if everything is fine, process the user input.
                    Student_Index1 = Option_Input.indexOf(",");
                    Student_Index2 = Option_Input.indexOf(",",Student_Index1+1);
                    Student_Index3 = Option_Input.indexOf(",",Student_Index2+1);

                    Student_Name = Option_Input.substring(0,Student_Index1);
                    Student_Grade_1 = Option_Input.substring(Student_Index1+1, Student_Index2);
                    Student_Grade_2 = Option_Input.substring(Student_Index2+1, Student_Index3);
                    Student_Grade_3 = Option_Input.substring(Student_Index3+1);

                    // Remove the spaces
                    Student_Name = Student_Name.trim();
                    Student_Grade_1 = Student_Grade_1.trim();
                    Student_Grade_2 = Student_Grade_2.trim();
                    Student_Grade_3 = Student_Grade_3.trim();

                    Gr_1 = Integer.parseInt(Student_Grade_1);
                    Gr_2 = Integer.parseInt(Student_Grade_2);
                    Gr_3 = Integer.parseInt(Student_Grade_3);

                    Grade1[0] = Gr_1;
                    Grade1[1] = Gr_2;
                    Grade1[2] = Gr_3;

                    Found_Student = false;

                    // To update the student's record if it exists
                    for(int p = 0; p < Students_Vector.size(); p++) {
                        if (Student_Name.equals(((CreateStudent)Students_Vector.get(p)).GetStudentName())) {
                            ((CreateStudent)Students_Vector.get(p)).SetStudentGrades(Grade1);
                            Found_Student = true;
                        }
                    }

                    // add the student to the roster if it doesn't exist.
                    if (!Found_Student) {
                        CreateStudent student = new CreateStudent(Student_Name,Grade1);
                        Students_Vector.add(student);
                    }
                    // Inform the user
                    else {
                        JOptionPane.showMessageDialog(null, "The student, " + Student_Name + " is updated!\nPlease click OK to go back.", "Info", JOptionPane.INFORMATION_MESSAGE);
                    }
                }
                else if (Option_Choice.equalsIgnoreCase("Delete")) {
                    Option_Input = JOptionPane.showInputDialog(null, "Please enter student's name you want to delete:", "Input", JOptionPane.QUESTION_MESSAGE);
                    Found_Student = false;

                    // Search for a name and delete it.
                    for(int i = 0; i < Students_Vector.size(); i++) {
                        if (Option_Input.equals(((CreateStudent)Students_Vector.get(i)).GetStudentName())) {
                            Students_Vector.remove(i);
                            Found_Student = true;
                            break;
                        }
                    }
                    // Go back to the menu if the user info is not found.
                    if (!Found_Student) {
                        JOptionPane.showMessageDialog(null, "Please enter the correct name, this student does not exist!\nPlease click OK to go back.", "Error", JOptionPane.ERROR_MESSAGE);
                    }
                }
                else if (Option_Choice.equalsIgnoreCase("View")) {
                    Option_Input = JOptionPane.showInputDialog(null, "Please enter the student's name to view the record:", "Input", JOptionPane.QUESTION_MESSAGE);
                    Found_Student = false;

                    // Search for a name and show the record
                    for(int m = 0; m < Students_Vector.size(); m++) {
                        if (Option_Input.equals(((CreateStudent)Students_Vector.get(m)).GetStudentName())) {
                            Students_Grade = ((CreateStudent)Students_Vector.get(m)).GetStudentGrades();
                            Top_Grade = ((CreateStudent)Students_Vector.get(m)).GetTopGrade(Students_Grade);
                            Option_Record = ((CreateStudent)Students_Vector.get(m)).ConverttoString() + "\nThe Top Grade: " + Top_Grade;

                            JOptionPane.showMessageDialog(null, Option_Record, Option_Input, JOptionPane.INFORMATION_MESSAGE);
                            Found_Student = true;
                            break;
                        }
                    }
                    // If no record is found, go back to the menu.
                    if (!Found_Student) {
                        JOptionPane.showMessageDialog(null, "Please enter the correct name, this student does not exist!\nPlease click OK to go back.", "Error", JOptionPane.ERROR_MESSAGE);
                    }
                }
                else {
                    // If the user types anything other than "Add", "Delete", "View", or "Exit", then inform him.
                    if (!(Option_Choice.equalsIgnoreCase("Exit"))) {
                        if (Option_Choice.trim().equals("")) {
                            JOptionPane.showMessageDialog(null, "Please first make a selection then click OK!\nPlease click OK to go back.", "Error", JOptionPane.ERROR_MESSAGE);
                        }
                        else {
                            JOptionPane.showMessageDialog(null, "Please enter the correct option, this option does not exist!\nPlease click OK to go back.", "Error", JOptionPane.ERROR_MESSAGE);
                        }

                    }
                }

            }
        }
        catch(NullPointerException e) {
            System.exit(0);
        }

        System.exit(0);
    }

    // For input validation
    private static boolean Input_Validation(String Demo_String) {
        boolean R_Value = false;

        int Comma_Count = 0;
        int Index1_Comma = 0;
        int Index2_Comma = 0;
        int Index3_Comma = 0;
        char Comma_Char;
        boolean Comma_Position = true;

        String String_Name = "";
        String String_Grade1 = "";
        String String_Grade2 = "";
        String String_Grade3 = "";
        int String_Grade_1 = 0;
        int String_Grade_2 = 0;
        int String_Grade_3 = 0;

        // Check for a blank string
        if ((0 == Demo_String.length()) || (Demo_String.equals(""))) {
            R_Value = true;
        }

        // Check for commas
        for (int i = 0; i < Demo_String.length(); i++) {
            if ((Demo_String.charAt(i)) == ',') {
                Comma_Count++;
            }
        }

        // Check for the positions of commas and make sure there are three commas
        if (3 == Comma_Count) {
            Index1_Comma = Demo_String.indexOf(",");
            Index2_Comma = Demo_String.indexOf(",",Index1_Comma+1);
            Index3_Comma = Demo_String.indexOf(",",Index2_Comma+1);

            if (Index2_Comma == Index1_Comma+1) {
                R_Value = true;
                Comma_Position = false;
            }
            else if(Index3_Comma == Index2_Comma+1) {
                R_Value = true;
                Comma_Position = false;
            }
        }
        else {
            R_Value = true;
        }

        // Dissects the 'string' to store the data
        if ((3 == Comma_Count) && (true == Comma_Position)) {
            String_Name = Demo_String.substring(0,Index1_Comma);
            String_Grade1 = Demo_String.substring(Index1_Comma+1, Index2_Comma);
            String_Grade2 = Demo_String.substring(Index2_Comma+1, Index3_Comma);
            String_Grade3 = Demo_String.substring(Index3_Comma+1);

            // Compare the string with the trimmed string because white spaces are not allowed.

            if (!(String_Name.equals(String_Name.trim()))) {
                Error_Flag = 5;
                R_Value = true;
            }
            if (!(String_Grade1.equals(String_Grade1.trim()))) {
                Error_Flag = 5;
                R_Value = true;
            }
            if (!(String_Grade2.equals(String_Grade2.trim()))) {
                Error_Flag = 5;
                R_Value = true;
            }
            if (!(String_Grade3.equals(String_Grade3.trim()))) {
                Error_Flag = 5;
                R_Value = true;
            }

            // Once the flag is set, return to call the program.
            if (0 < Error_Flag) {
                return R_Value;
            }

            // Remove the white spaces
            String_Name = String_Name.trim();
            String_Grade1 = String_Grade1.trim();
            String_Grade2 = String_Grade2.trim();
            String_Grade3 = String_Grade3.trim();

            // Check for the empty names or null values
            if ((String_Name.equals("")) || (null == String_Name)) {
                Error_Flag = 4;
                R_Value = true;
            }


            if (0 < Error_Flag) {
                return R_Value;
            }

            // Make sure the name starts with a letter.
            if (!((String_Name.equals("")) || (null == String_Name))) {
                int ASCII = Demo_String.charAt(0);

                if (ASCII < 65) {
                    Error_Flag = 1;
                    R_Value = true;
                }
                else if ((ASCII > 58) && (ASCII < 65)) {
                    Error_Flag = 1;
                    R_Value = true;
                }
                else if (ASCII > 122) {
                    Error_Flag = 1;
                    R_Value = true;
                }
            }


            if (0 < Error_Flag) {
                return R_Value;
            }

            // Check for empty grades or null values
            if ((String_Grade1.equals("")) || (null == String_Grade1)) {
                Error_Flag = 2;
                R_Value = true;
            }
            else if ((String_Grade2.equals("")) || (null == String_Grade2)) {
                Error_Flag = 2;
                R_Value = true;
            }
            else if ((String_Grade3.equals("")) || (null == String_Grade3)) {
                Error_Flag = 2;
                R_Value = true;
            }


            if (0 < Error_Flag) {
                return R_Value;
            }

            // Make sure the grades contain only numbers
            try {
                String_Grade_1 = Integer.parseInt(String_Grade1);
                String_Grade_2 = Integer.parseInt(String_Grade2);
                String_Grade_3 = Integer.parseInt(String_Grade3);
            }
            catch(NumberFormatException e) {
                R_Value = true;
            }

            // Make sure the grades have only three digits
            if (3 < String_Grade1.length()) {
                Error_Flag = 3;
                R_Value = true;
            }
            else if (3 < String_Grade2.length()) {
                Error_Flag = 3;
                R_Value = true;
            }
            else if (3 < String_Grade3.length()) {
                Error_Flag = 3;
                R_Value = true;
            }
        }

        return R_Value;
    }
}

MaintainStudent 类用于维护学生的记录; 它执行添加/更新、删除、查看和退出操作。 该类还包含驱动程序的 main 方法。

让我们尝试运行该应用程序并查看不同操作的输出:

  1. 主页:

    Java名册主页

  2. 添加/更新学生记录:

    要添加新的学生记录,请键入“添加”并按“确定”。 如果您想更新记录,请键入“添加”,按“确定”,然后键入带有新成绩的学生姓名,记录将被更新。

    添加:

    Java名册添加学生

    更新:

    Java名册更新学生
  3. 删除学生记录:

    要删除学生记录,请键入删除,按确定,然后键入学生姓名以删除记录:

    Java名册删除学生
  4. 查看学生记录:

    要查看学生记录,请键入“查看”,按“确定”,然后键入学生姓名以显示学生记录。 它还将显示学生的最高成绩。

    Java名册查看学生
  5. 退出名单:

    要退出学生名册应用程序,请输入 Exit 并按确定。

    Java名册退出

上一篇:用Java读取Excel文件

下一篇:没有了

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

用Java读取Excel文件

发布时间:2023/08/06 浏览次数:141 分类:Java

本文介绍了在 Java 程序中读取 Excel 文件的必要信息。 使用什么软件? 要遵循哪些步骤,最后但并非最不重要的一点是如何应用库以便用户可以读取 Excel 文件。 那么,让我们开始吧。用 Java 读

用 Java 创建 Excel 文件

发布时间:2023/08/06 浏览次数:141 分类:Java

本文介绍如何使用 Java 创建 Excel 文件。用 Java 创建 Excel 文件 Excel 工作表有存储数据的单元格,但用 Java 创建、读取和写入 Excel 文件很棘手。

Java 字母电话号码转换器

发布时间:2023/08/06 浏览次数:68 分类:Java

本文介绍如何用 Java 生成字母电话号码转换器。Java 字母电话号码转换器 有时,公司会使用电话号码格式,例如 555-GET-FOOD,这是编写电话号码的标准化流程,以便客户更容易记住。

Java中的并发队列实现

发布时间:2023/08/06 浏览次数:69 分类:Java

本文将讨论 Java 中并发队列的一些最有效的实现以及需要利用哪些实现。Java 中的并发队列 首先,我们来讨论所有三个队列。Java ConcurrentLinkedQueue、Java ArrayBlockingQueue、Java LinkedBlockingQueue

Java 中的闭包

发布时间:2023/08/06 浏览次数:140 分类:Java

本文将详细讨论闭包并提供必要的示例和解释以使该主题更容易。在开始讨论之前,让我们先看看闭包的一般语法,如下所示。Java 中的闭包 在下面的示例中,我们将说明最基本的闭包,其中我

在 Java 中使用带有 Jackson 的自定义序列化器

发布时间:2023/08/05 浏览次数:80 分类:Java

本文将介绍如何在 Java 中使用 Jackson 的自定义序列化器。 首先,我们来了解一下序列化的概念。Java 中的序列化 Java 中的序列化过程是一种将对象与字节流相互转换的技术。 这不仅可以将对象

Java 中的 Trie 数据结构

发布时间:2023/08/05 浏览次数:111 分类:Java

本文介绍了 Java 中的 Trie 数据结构。Java 中的 Trie 数据结构 Trie 词是从单词 Retrieval 中提取出来的,它是一种用于存储字符串集合的排序数据结构。

JAVA_OPTS 环境变量

发布时间:2023/08/05 浏览次数:199 分类:Java

JAVA_OPTS 是一个环境变量,用于将自定义设置传递给 Java 虚拟机。 本文介绍了 JAVA_OPTS 的使用。JAVA_OPTS 环境变量 JAVA_OPTS 是一个标准环境变量,用于设置 Java 虚拟机的自定义设置。

在Ubuntu中设置JAVA_HOME环境路径

发布时间:2023/08/05 浏览次数:154 分类:Java

本文将介绍如何在Ubuntu中设置Java环境路径。 Ubuntu 将 openjdk6 安装到 /usr/lib/jvm/java-6-openjdk 路径。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便