2

So I'm creating a student database thing for a school project. My first issue is that upon creating a new student I should see "Application number ### has registered successfully". Now the problem is that we have to have that number generate (### referring to the number) sequentially from 1 every time a new application is recorded. How would I go about doing that?

So far this is what there is but I can't seem to get the number to generate incrementally.

public TestApplication(String Surname, String personalIdNo)
{
    if (isValidpersonalIdNo(personalIdNo) == true)
    {
        Student.add(Surname);
        Application.put(personalIdNo, Student);
        System.out.println("Application number ### " +  "has registered successfully");
    }
    else
    {
        System.out.println("Application has failed, Personal id: " + personalIdNo);
    }
}

Any help with this would be appreicated.

5
  • 3
    The most common approach for this would be to do it in the database. Most databases have the ability to have a column that is an auto-generated sequence number. When you insert the record for the first time, the next incremental number is inserted in that column. Commented Dec 4, 2013 at 19:27
  • You're not entirely clear on whether you need to (re)invent a Java based database system yourself or that you have to use an existing (relational/SQL) database system. For the former, AtomicLong may be interesting. For the latter, just use DB-generated PK (in e.g. MySQL, check its documentation using keyword "auto_increment") Commented Dec 4, 2013 at 19:28
  • 1
    also a recommendation for you: Use String surName (or surname). Per java convention, instances should be camelCased. Second, for your second parameter, use int or Integer for a numeric field. Commented Dec 4, 2013 at 19:30
  • How do you persist the recorded applications? Commented Dec 4, 2013 at 19:30
  • I highly recommend you learning object oriented language. int applicationNumber = 0 as an instance variable. And then you can increment applicationNumber++ on application registration method call. Commented Dec 4, 2013 at 20:16

3 Answers 3

1

Since you seem to be using lots of static methods, I believe the best thing for you to do in this case is to create a static field called latestId and a static method called generateId, both in the Student class. Then you can call the generateId method whenever you call Student.add.

However, please note that this solution does not work if your application is multithread.

public class Student {

private static int latestId = 0;

public static int generateId() {
   return ++latestId;
}

...
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can write a singleton class that will produce the ids for you:

class Generator {

  private AtomicInteger count = new AtomicInteger(1);
  private static Generator generator = new Generator();

  private Generator() { }
  public static Generator getInstance() {
    return generator;
  }

  public int generate() {
    return count.getAndIncrement();
  }

}

Now, when you need to get a new id, you just call the generate method. The AtomicInteger is used because you might need the id from multiple threads and it will make the concurrent access safe.

The singleton Generator provides a single entry point to the id-generating facility.

Comments

0

You can use your storage type to give you the amount of added students that were put into DB. I don't know what type you use to store your students. If it is hashmap or vector you can use size method to print students count. So I assume if you have Application.put you probably have a field in your Application type that is used to store each student. Then you can add a method like getStudentsCount to it and you should be all set. Since I don't know much about your Application type the above is all assumptions. Below you can find how I would solve that:

import java.util.HashMap;
import java.util.Vector;

class Student{
    private String name;
    private int personalID;
    public Student(String name, int personalID){
        this.name = name;
        this.personalID = personalID;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getPersonalID() {
        return personalID;
    }
    public void setPersonalID(int personalID) {
        this.personalID = personalID;
    }


}
class DB{
    private HashMap<Integer, Student> students = new HashMap<Integer, Student>();

    public boolean addStudent(Student student) {
        Integer studentId = new Integer(student.getPersonalID());
        if( !students.containsKey(studentId)){
            students.put(new Integer(studentId), student);
            return true;
        }
        else
            return false;


    }

    public int getStudentCount() {
        return students.size();
    }
}

class Operations{
    DB db;
    public Operations(DB db){
        this.db = db;
    }
    public boolean addStudent(String name, int personalID){
        Student student = new Student(name, personalID);
        return db.addStudent( student );
    }
}
public class SimpleStudentDB {

    public static void main(String [] args){
        DB db = new DB();
        Operations operations = new Operations(db);
        if( operations.addStudent( "Jason", db.getStudentCount()+1) )
            System.out.println("Student added successfully. DB contains ###"+db.getStudentCount()+" elements");
        else
            System.out.println("Operation failed");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.