繁体   English   中英

如何为用户在 JAVA 程序中创建的每个学生提供唯一 ID

[英]How to give a unique ID to each student that is created by the user in a JAVA program

我正在 JAVA 中开展一个项目,其中用户键入信息,程序将该数据写入二进制文件中,您可以显示学生,也可以编辑学生,一切正常,我唯一缺少的就是我被卡住了,有没有一种方法,每次用户创建学生时,都会为该学生提供一个唯一的 ID,如 1、2、3 等。我可以让用户在创建时输入自己的 ID,但他们不知道有多少学生数据库中已经有,我找到了一个可能的解决方案,但我不知道这是否是最好的方法,请查看下面注释部分的代码。 我将不胜感激。 这是代码。 谢谢

import java.util.Scanner;
import java.io.*;
public class MidTermProject {

    public static void main(String[] args) throws IOException {
        
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("Here is the sample of menu choices for Main Menu.");
        
        System.out.println("\nWelcome to University Enrollment" + "\n1. Create Student");
        
        System.out.println("Please enter a valid choice(1, 0 to Quit) :");
        int userInput = keyboard.nextInt();
        
        if(userInput == 1) {
            CreateStudent();
        } else if(userInput == 0) {
            System.out.print("Done");
        } else {
            System.out.println("Invalid Option, Please try again.");
            userInput = keyboard.nextInt();
            if(userInput == 1) {
                CreateStudent();
            } else if(userInput == 0) {
                System.out.print("Done");
            }
    public static void CreateStudent() throws IOException {
        String FullName;
        String address;
        String city;
        String state;
        
        Scanner keyboard = new Scanner(System.in);
        
        FileOutputStream fstream =
                new FileOutputStream("StudentInfo.dat");
        DataOutputStream outputFile =
                new DataOutputStream(fstream);
        
        System.out.print("\nPlease enter your information bellow.\n" + "\nFull Name: ");
        FullName = keyboard.nextLine();
        outputFile.writeUTF(FullName);
        
        System.out.print("Address: ");
        address = keyboard.nextLine();
        outputFile.writeUTF(address);
        
        System.out.print("City: ");
        city = keyboard.nextLine();
        outputFile.writeUTF(city);
        
        System.out.print("State: ");
        state = keyboard.nextLine();
        outputFile.writeUTF(state);

        //allow the user to select its own ID
        System.out.print("Please get a Student ID(1-10): ");
        StudentID = keyboard.nextInt();
        //Used a lop to compare index with student ID and after that index increments
        for(int index = 0; index == StudentID; index++) {
            //if the ID has been selected, display a message and allow the user to select a different ID
            if(index == StudentID) {
                System.out.print("The selected ID has been selected already, Please select a different ID");
                StudentID = keyboard.nextInt();
            }
        }
        //write the ID in the file
        outputFile.writeInt(StudentID);
        
        System.out.print("Successfully Created");
        
    }
        
        System.out.print("Successfully Created");
        
    }
    public static void EditStudent() throws IOException {
        String editName;
        String editaddress;
        String editCity;
        String editState;
        
        Scanner keyboard = new Scanner(System.in);
        
        RandomAccessFile file = 
                new RandomAccessFile("StudentInfo.dat", "rw");
        file.seek(0);
        
        System.out.print("\nPlease enter NEW information bellow.\n" + "\nFull Name: ");
        editName = keyboard.nextLine();
        file.writeUTF(editName);
        
        System.out.print("Address: ");
        editaddress = keyboard.nextLine();
        file.writeUTF(editaddress);
        
        System.out.print("City: ");
        editCity = keyboard.nextLine();
        file.writeUTF(editCity);
        
        System.out.print("State: ");
        editState = keyboard.nextLine();
        file.writeUTF(editState);
        
        file.close();
    }
    public static void DisplayStudent() throws IOException {
        FileInputStream fstream = new FileInputStream("StudentInfo.dat");
        DataInputStream inputFile = new DataInputStream(fstream);
        
        String student;
        boolean endOfFile = false;
        
        //this do not display the ID number that was written in CreateStudent()
        while(!endOfFile)
        {
            try
            {
                student = inputFile.readUTF();
                //Tried the following to display the ID but didn't work
                // int StudentID = inputFile.readInt();
                System.out.print(student + " ");
            }
            catch (EOFException e)
            {
                endOfFile = true;
            }
        }
        System.out.println("\nDone");
        
        inputFile.close();
    }

根据代码,我假设这是高中或学习任务。 然后最简单的方法是读取文件,将最高 id 存储在变量中。 当用户添加一个新学生时,将 1 添加到该变量并将其用作他的 id。

在更高级的应用程序中,您将使用 UUID,它基本上是一个非常大的随机 id,极不可能被复制。

在学生 class 上添加一个 static 变量,并在构造函数上增加它(这样每次创建新学生时它都会增加)例如:

private static int studentID = 1;
    
        public Student()
        {
            studentID++;
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM