views:

83

answers:

4
+5  Q: 

Java Voting System

Hi, I have to create a voting system for school with java, the system will be used to vote for movies. It will have all of the users and movies set up before hand by someone.

It is a console application and it will be ran on multiple computers. Some of the questions i have about this are:

  1. My teacher wants us to use a text file to maintain all of the data will this be a problem? Would it be better to switch to a database such as mysql and why?

  2. When the user types in their login information, should the program (a) just look up the person's credentials in the text file and then re-create the object for the person or (b) should it re-create all of the users then just check with the array list for the user and check the password?

Or for #2 Would there be a better way to do it ?

Thanks,

SR

+1  A: 

Since it's a school assignment/project, you should follow the teacher's instruction & specifications (to get better marks). If this was your own project, then my suggestion would be to avoid text file as database completely.

In real world scenario,

  • If you want to store passwords in a database, I suggest hashing the password with a good hashing algorithms such as SHA-512 (part of the SHA-2 family). Also, if you decided not make it a web application, you would send confidential to/from your application/server in a secured protocol like HTTPS.
The Elite Gentleman
thanks for the reply but the switching to a database won't be much of a problem, my teacher is very open with this assignment.
SR4801
Then by all means, go for the database option.
The Elite Gentleman
I would agree with the database solution, however we are now getting into the world of network transfers. Because it should be a central database that all clients connect to and enter data.
Woot4Moo
@Woot4Moo, that's if you're developing a middleware and backend system, I agree. But sending information from client to server should e secured too.
The Elite Gentleman
@TEG right I agree.
Woot4Moo
A: 

I suggest using XStream which is a really quick and easy to way to persist your data to disk. XStream just converts your POJOs to XML and allows for both marshalling/unmarshalling. The advantage of XStream is that your data will still be readable by humans which can be useful when you are learning to program and doing debugging.

You need to have large amounts of data before this turns into a performance bottleneck and since it is homework I doubt you will have that much. I don't know how much programming you know, but adding a proper relational database adds A LOT of complexity to your system. My advice is that you do one step at a time here. Plenty of time for databases :)

willcodejavaforfood
thanks for the reply, i will definitely use XStream
SR4801
It's really cool for smaller projects :)
willcodejavaforfood
A: 
  1. A database will normally have built-in functionality for keeping data integrity in a multi-user scenario. Consider in your application what will happen to the text file if two users vote at exactly the same time from two different computers. Unless you have a single "server" process handling text file updates, then you will need to somehow coordinate between the two users to make sure the text file never gets corrupted and that both votes are counted. It can be done (perhaps by using a "lock" file) and might even be worthwhile given the extra complexity that introducing a database requires.

  2. It's not very clear what you are asking, but if you can "scan/search" the text file for the user(name) that you are interested in, then that would seem to be more efficient at 1st glance than constructing every user object just to check for a matching password.

kaliatech
thanks for the reply, especially for #2 which i was a bit confused with in terms of efficiency
SR4801
+1  A: 

For #2. If you only need to know about one user at a time, then there would be no benefit to loading all users into memory. My approach for something like this would be to parse the username, find it in the file, then check if the parsed password matches the password recorded for that user. If it does, create the object. You should be checking for and catching errors at each stage (eg. inappropriate username provided, user does not exist, etc.)

Storing and transmitting plaintext passwords is always a bad idea. It might not be a requirement for this assignment, but consider hashing passwords before saving them. Then rather than looking for the actual password text provided, hash that and compare it to the saved hash.

deyur