• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMaps  RSS feed

 
Rao.K Reddy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey hi, I need a help urgently.
I want to create a students registration account(firstname,lastname,email,phno,username) using java where all those parameters are given as an input in Scanner(i.e: keyboard input) and stored directly into hashmaps. i have no idea how the logic works. The output should be like Hashmap<Character,Hashmap> hm=new Hashmap<Character, Hashmap>();
Character has a first character of firstname and Hashmap includes the other values such as firstname,lastname,email,phno,username..
Suppose Character 'A' in firstname "Amir" Maps to student records of "Amir" which is stored in another Hashmap. shouldn't use Lists or ArrayLists. How to do it? Please help!!


I wrote the program in this manner. But this is not the correct way to do this.. please help me out


 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, in your write up you have identified an object know as "student". I'd start by creating this class. Before jumping to a Hashmap I think you should be clear on what kinds of operations do you want to perform on students; sort, print, etc.. Also, hashmaps require a unique key, the first letter of the last name is not unique.
 
Rao.K Reddy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Well, in your write up you have identified an object know as "student". I'd start by creating this class. Before jumping to a Hashmap I think you should be clear on what kinds of operations do you want to perform on students; sort, print, etc.. Also, hashmaps require a unique key, the first letter of the last name is not unique.



Hey i want to do a sort operation..where first letter of firstname is a unique key and results to Class objects of values that consists of student records.
Can you help me out how to do it? I have no idea....can you send me video links how to perform it?
Thanks!
 
Andy Ball
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vinay,

I agree with Carey on this one, you probably want to create a class called Student & assign your values to a Student object, as looking at your existing implementation, - the 4 Arraylists, list1, list2 list3 & list4, have no correlation at all, they are 4 separate lists. so just because list2(1)'s value = hamilton, it doesn't have any relation to the values with the same index in list, list3 & list 4 ( other than they arbitrarily have the same index). but if you should accidentally remove an item ( such as list3.remove(3);) then now your items are all out of sync. What you should do is create a Student Class (see below) and then each time you want to add a Student you just create a new Student object using your constructor & you can then just have an array ( or ArrayList if your number of Students is unknown) of Student objects. Also, if you decide you wish to nest HashMaps, - the Inner Hashmap will still need it's element types specified (of which one must be the unique key)

At any point you just pick a Student object & that object will contain the Student's name, last name, phone # & username. all kept neatly together.



This Allows you to encapsulate all the information for one student within a single Student object.

You can then of course just create your List of Student objects within your Dawson class as such:



The last function I just added allows you to provide a firstname string ( such as "mike") and it will return you any Student object with that firstname, - from where you could then access the other details, or manipulate as required. - there is obviously no validation on this method as yet, so if no Student has the name "mike" the empty Student object would be returned, and if more than one 'mike' exists, only the last one would be returned.

As regards your object sorting, - perhaps you should implement the comparable interface into Student and then override the compareTo() method within the Student class to define how you wish to sort your objects. - then you can just call Collections.sort(myListOfStudents); - Doing it this way means that if in the future you decide to change to sort by lastname, - you just update the 'compareTo() method in Student & nothing else needs to change at all!!

i hope this helps you to start to progress your classes.

- Andy

 
Rao.K Reddy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Andy...I'll try using nested Hashmaps.
How am i supposed to contact you if i got any doubts?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vinay kumar bogum wrote:How am i supposed to contact you if i got any doubts?

You post a reply in this thread. That way everybody gets to benefit from the discussion. See UseTheForumNotEmail
 
Andy Ball
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vinay, - As I mentioned in my post, - nested HashMaps can get very complicated very quickly. - Take it easy if you're not that used to using nested iterators (which would probably be necessary).
Have a look through the Collections API and see if another collection may suit your needs better (perhaps in conjunction with the Student class I helped you out with).

Also, the getStudentByFirstName() method that I placed within the Dawson class above should alleviate the need to use HashMaps altogether. If you were planning to use the user's first name as the unique key, just to retrieve the student by supplying a firstname String, then the getStudentByFirstName() method does pretty much that already, without the restriction of possibly duplicating a key ( e.g if you use HashMaps and used first name as the key you could never have a second Student called 'mike'.)

And as Joanne stated, please post your further queries on here and someone will endeavour to assist. I'm fairly new here myself & I'm just trying to help out where I can. - I'd not like for you to be waiting on me for a response if you were in need of help quick & I was busy, especially when there's many other talented developers on here who can assist just as well (probably better!).

Thanks & good luck.

- Andy
 
Rao.K Reddy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah but in my assignment i have to use hashmaps to store Student records where key is first character of firstname and value is objects of Student class. You gave like firstname as a key and remaining as values. How should I use like if first character matches with firstname and displays records of that particular name which starts with character A and so on upto Z.. Is there any possibility of using Scanner as input values ?
 
Andy Ball
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vinay, - I see.

In that case if you use the Student class I helped you with you can easily utilise the Student.getFirstName() method to put any Student object into a HashMap along with it's associated String for firstname.

hint:



and yes, a Scanner would be a viable means of input. Here is a quick Scanner tutorial I just found via Google which may get you started..

P.S. feel free to give me a thumbs up if you feel I've assisted you, I am looking to hopefully gain a good reputation on this site for helping people wherever I can.

If it's for a school assignment, the best way to klearn is just to roll up your sleeves and start coding. try to achieve what you can & when you hit a wall, post your code on here explaining your issues, people can help you troubleshoot, which in turn helps to educate. :-)

Kindest regards,

-Andy
 
Rao.K Reddy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinay, you seem to be ignoring all the advice given to you. Andy Ball even gave you the source code for a Student class. Why aren't you using it? Also, it appears that you are trying to work out a class assignment without giving us all the criteria of the assignment, these would help us help you. Again, in your example, the first letter of the first name may not be unique which is required by the HashMap class. You could have a HashMap with the first character of the first name as a key but then your value should be a list, like: HashMap<Character,List<Student>>. And please use Code tags when posting source code, it makes it more readable and adds line numbers so that we can help you by referring to specific lines.
 
Rao.K Reddy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Carey ,do you want me to post my assignment to refer and suggest me how to do it?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, post the assignment.
We've given some suggestions now I think you should take your last code listing and modify it to use a Student object.
You should consider breaking up your main() method into small methods as main() will surly grow. E.g. inputStudents(), indexStudents(), printStudentsFromIndex().
 
Rao.K Reddy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Distributed Reservation Management System (DRMS) using Java RMI
In the assignments and project, you are going to implement a simple Distributed
Reservation Management System (DRMS) for libraries: a distributed system to manage a
group of library systems used by students and administrators to help them manage book
reservations. So you will need to implement both the student interface support and admin
interface support. That is, you need to implement the functionalities that will help the end
users (students and admin, in this case) make use of the reservation system to reserve
books available within the library systems.
Consider a group of libraries each associated with a unique educational institution. The
server for each library must maintain a collection of books. Each book object can be stored
in any container of your choice, but make sure the container gives the best efficiency for
the operations related to accessing the book object. The book structure should contain the
following fields:
• Name of the book
• Author of the book
• Number of copies available
Students who need to borrow a book from the library must have an account with the server
that supports the library. An account contains the following specific information about the
student:
• First Name
• Last Name
• Email Address
• Phone Number
• Unique Username (min 6 characters and max 15 characters)
• Password of at least 6 characters
• Reserved books and duration to return the books
• Fines accumulated
• Educational Institution
The accounts are placed in several lists that are stored in a hash table according to the first
character of the username indicated in the account. For example, all the accounts with the
username starting with an “A” will belong to the same list and will be stored in a hash
table (acting as the database) using the key “A”. Each server also maintains a log containing the history of all the operations that have been performed on that server. This
should be an external text file (one per server) and shall provide as much information as
possible about what operations are performed, at what time and who performed the
operation.
The system has two distinct types of users: Students and Administrators. Students can be
identified by their username, password and educational institution. Whenever a student
performs an operation, the system must identify the library that the student belongs to
based on his/her educational institution and thus direct all the operations to that specific
server. The students maintain a file (text file) of the actions they performed on the system
and the response from the system when available. For example, if you have 10 students
using your system, you should have a folder containing 10 files. Administrators can be
identified by their username, password and educational institution. For the sake of this
assignment all the administrators have the same username which is “Admin” and the
password is also “Admin”. The administrators also maintain a file.
The operations that can be performed are the following:
Operations performed by Students
• createAccount (FirstName, LastName, EmailAddress, PhoneNumber, Username,
Password, EducationalInstitution)
When a student invokes this method through a client program called StudentClient,
the server associated with the educational institution attempts to create an account
with the information passed if the username does not exist and that the passed
information is valid according to the problem description by inserting the account at
the appropriate location in the hash table. The server returns information to the
student whether the operation was successful or not and the student file is created
and updated on the server. For simplicity it is assumed that the student has access
to creating an account in the library only if he/she is enrolled in the educational
institution.
• reserveBook (Username, Password, BookName, AuthorName)
When a student invokes this method through the client program called the
StudentClient, the server associated with this student attempts to find if the book
exist (although the book entry is found, quantity should be greater than 0 to
successfully reserve). If it does, then the corresponding quantity is decremented
and the book is updated to the reserved book list of the student. Note that the
student has to provide valid credentials to reserve a book. The duration is set to 14
days by default. Student file is updated.
Operation performed by Administrators
• getNonRetuners (AdminUsername, AdminPassword, EducationalInstitution,NumDays)
When Admin runs the getNonReturners () though the client program AdminClient,
it lists all the students’ First Name, Last Name and Phone Number who have not
returned the books with “NumDays” past their loan date in all the three educational
institutions. The admin File is updated with this information. The admin can then pass that information to the customer care agent to place a call to all the customers
to remind them about the book loan.
For the sake of this assignment, you have to make sure the getNonReturners () functions
returns
Educational Institution 1 : FirstName1 LastName1 514xxxxxxx
FirstName2 LastName2 514xxxxxxx
FirstName3 LastName3 514xxxxxxx
……..
Educational Institution 2 : FirstName1 LastName1 514xxxxxxx
FirstName2 LastName2 514xxxxxxx
FirstName3 LastName3 514xxxxxxx
……..
Educational Institution 3 : FirstName1 LastName1 514xxxxxxx
FirstName2 LastName2 514xxxxxxx
FirstName3 LastName3 514xxxxxxx
……..


This function needs to be implemented by using UDP/IP sockets where the admin invoked
server communicates with other servers using UDP/IP messages to get their information,
once the information is received the admin invoked server sends the result string to the
admin. This function should never be static for obvious reasons.
 
Rao.K Reddy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please, give me an idea how to do it. It looks a bit complicated for me to do it. any possible video links how to do it? I have searched everywhere in youtube and google. can't find any solutions. please, help me out guys!
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

This seems to be a well defined project. Specifically what are you having difficulty with?

Thanks,

-steve
 
Rao.K Reddy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should use multi-threading in this project. I donno how to implement it using threads. creating students account and storing them into hashmaps is a tricky! I have no idea how to do it. can you suggest me how to do it and help me with some links?
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok,

it appears that this project (where you will not get a video to show you how to do it) is expecting you to have some background in java that might be beyond you.

What parts of the assignment are you comfortable with and what area are you lacking in (not trying to put you down, just trying to get you the
right level of help).

-steve
 
Rao.K Reddy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I have created the book database structure which consists of name ,author and numofbooks as fields. that was the first part. coming to second part I need to define an interface where the three methods such as createAccount(), reserveBook() and getNonreturners() are placed and it is implemented by the another ImplClass where the methods can be defined. Where am i supposed to use Multi-threading here? Should i implement Runnable to the Implementation class? and how am i supposed to create students account where each institution has a students record separately accessing libraries and admin checks the student activities done on reserving a book from that library.
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
perhaps:

how am i supposed to create students account where each institution has a students record separately accessing libraries and admin checks the student activities done on reserving a book from that library.


This might be an appropriate place to create separate threads....


Also it might help us if you define the architecture you are planning on implementing.

-steve
 
Rao.K Reddy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve, I have no clue how it works and i am lacking in logic...
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have added code tags to you post. Always use them: doesn't it look better I also had to break your long lines, and you can see how it should be done.
Don't use // comments for anything more than a few words. Use /* comments */ instead.

I think you have reached the point where you should hand it what you have got, or start deleting code page by page and start again. Don't write code until you know what you are going to do with it. And for that you need pencil and paper. Write down what you want to do and consider code later.

And welcome to the Ranch
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!