简体   繁体   中英

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

I am working on a project in JAVA where an user types information and the program writes that data in a binary file, you can display students and you can edit students, it all works fine, the only thing I am missing and I am stuck, is there a way that every time the user creates a student, that student will be given a unique ID like 1, 2, 3, etc. I could let the user enter the own ID at creation but they don't know how many students there is in the database already, I found a possible solution but I don't know if that was the best way to do it, look in the code below in the commented section. I would appreciate any help with this. Here is the code. Thanks

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();
    }

Based on the code, I'm assuming this is a high school or learning assignment. Then the easiest way to do this is to read in the file, store the highest id in a variable. When the user adds in a new student, add 1 to that variable and use that as his id.

In a more advanced application, you'd use a UUID, which is basically a very large random id that would be seriously unlikely to be replicated.

add a static variable on student class, and increment it on constructor (so that it will be incremented every time you create a new student) ex:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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