Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding student by ID  RSS feed

 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I am modifying my class assignments for the heck of it. So I have the program set up to save the student after they enter their first name, and last name. Later on it is going to show the student using javaFx. Instead of just looking up by name I want it to look up by the student ID, and show their names that way. The only thing I have in mind is to ask the student for their ID, and save it to the list with the first and last name. However I do have my Student Class code, and I was wondering how else I could look up the student by ID without having the user put in themselves. Im confused because I would not be able to look up the ID in the student list, because only the first name and last name get saved, so how would this work?




Here is the student Class


getStudentById and add my method that saves the student





 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't show how students gets declared, but if you declare it like this:

then you can use the same "loop through a list" logic you have in getStudentByName().

BTW, incrementing nextID in getStudentId() is a bad idea.  Increment it only when the student is created, that is, in the constructor.
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would it compare the getStudentId method? I have to change the getStudentByID method from void to students, because obviously the goal is to return a student. So I was thinking pretty much like my getStudentByName loop. But at the end at least what popped in my head was

if(s.getStudentId == Id)
return student.

The Id is what's going to be passed to the getStudentByName method

 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks to me like a task for a certain kind of data structure, which is designed for matching ID numbers to other data. You can do the same for names, but beware: you can have two different students with the same name.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cody Biggs wrote:The Id is what's going to be passed to the getStudentByName method

If you want to retrieve information of a student having a specific Id then I think you don't need to pass that Id to getStudentByName method to compare first and last name as Id would be unique.
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My bad. Meant to say that the ID is going to be passed to the getStudentByID method. Then the method sees to see if it's the same that's returned by the getStudentId method, and if so then return that student. If not then return null is my plan
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This morning, I wrote:That looks to me like a task for a certain kind of data structure, which is designed for matching ID numbers to other data. . . .
Please investigate the link I gave earlier; I still think that is the best way to implement your requirements in Java®.

A database might be better still, but that is much more complicated.
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your nextID processing seems weird. First, the constructor doesn't do anything with it, so, in theory, it is possible to create multiple student objects, back to back, and they will all have the same id. Second, the nextID is updated with the getStudentID() call. This means that it is possible to write code, that doesn't create any students, such as fetching a bunch of IDs to so some sorting, and the nextID will be updated many times.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!