After brainstorming (writing down single ideas),
make some categories of features.
Then organize the
ideas into these categories. Try to complete each category by adding
ideas that fit and augment the ideas that are already there. This
takes the form of an outline.
== E-Voting ==
A. Voting (input)
1. Show candidate names on [buttons]
2.
User presses a button
3. Show the vote on
the screen in a textField
4. Allow corrections/changes
( [Reset Button] )
5. Press [submit] button to finish
("cast vote")
B. Saving (storage)
1. Count vote(s)
2. At
end of program, save total counts in a file
C. Checking (processing)
1. Load list of voters
2.
Check voter-name - don't let them vote twice
3. If
they [submit] before voting, warn them and let them cancel
D. Show Resuts (output)
1. Check password so only a supervisor
can see the votes
2. Display totals in an output
box, so they don't stay on screen
3. Print
ballot on paper after [submit]
E. Robustness (error handling)
1. Check user-id (password) before
allowing voting
2. Don't allow voting for more than one
candidate
3. Check admin-password before showing totals
4.
Don't allow same person to vote twice
5. Don't
allow program shut-down before [submit]
6. Ensure saving
totals before shut-down
A good plan also includes a user-interface design, for example:
This could be drawn with a pencil or using MS-Word, or
by making
a simple prototype Java program.
We know we need to print paper ballots, save
totals, load names of voters, and produce
and output box,
but we need to find out how to program these in Java.
== Output (and Input) ==
javax.swing.JOptionPane.showMessageDialog(null,"You win!");
This displays "You win!" in a dialog box in the center of
the screen. If you had
an int variable named votes, you
could display it with:
javax.swing.JOptionPane.showMessageDialog(null,"" + votes);
Rather than always typing this very long command, you can encapsulate
it in a method,
and then call that whenever you want to output some
information.
public static void output(String message)
{
javax.swing.JOptionPane.showMessageDialog(null, message );
}
Now you can use the command output("You win!");
to display some text.
So it's easy to put lots of output
commands in your program.
Although it's not needed for the eVoting program, you might also want to
know how-to
make and input box.
public static String input(String message)
{
return javax.swing.JOptionPane.showInputDialog(null, message);
}
You can use this to ask a question and input the answer, like this:
String name = input("What is your name?");
== Saving in a File ==
Java has lots of different classes that can be used for file access.
They are all found in the java.io....
package. The following
uses a PrintWriter wrapped around a FileWriter to save text into
a file.
public static void save(String fileName, String message)
throws java.io.IOException
{
java.io.PrintWriter file =
new java.io.PrintWriter(new java.io.FileWriter(fileName));
file.println(message);
file.close();
}
The command file.println... is very similar to System.out.println
- but instead of printing on the
screen, it prints into the file. To
print more text into the file, just add more file.println...
commands
before closing the file.
== Loading from a File ==
public static String load(String fileName)
throws java.io.IOException
{
java.io.BufferedReader file =
new java.io.BufferedReader(new java.io.FileReader(fileName));
String food = file.readLine();
return food;
}
This method opens a file and reads one line of text. To read
more lines of text,
you could add more file.readLine... commands,
but you would need to put the
data into a bunch of different variables.
This would normally use a loop to do the
reading, and put the
various lines of text into an array.
== Printing on Paper ==
Printing is a horrible, messy problem. One of the big successes of
Windows 3.0 was the fact
that Windows would take care of the messy problems
of controlling printers. The original
Java version 1 had NO features
for printing - not even a sensible way of asking the operating
system to
do the printing. Later versions of Java have some printing features, but
they are
messy to use.
The simplest solution is to write the information into a file (see save
above), and then ask
another program to print it. Notepad will print
text files if you ask it nicely, like this:
...
runProgram("notepad.exe /p c:\\food.txt");
...
This is a very simple and limited solution. It only prints text-files,
and automatically sends
the job to the default printer without asking any
questions. In a network, this is not always
the best solution. And
remember you must first save your text into the file food.txt,
before you
can print it. In the case of a voting program, this is a bit of a security
issue,
as the voter probably doesn't want their name or vote saved in a file.
This also assumes the runProgram method below is available:
== Running Another Program ==
Even the best programmers cannot write every program they need. All
programmers share
and use the software written by other programmers. This
method runs a program -
the programName must tell the method where
to find the program (complete path),
unless the program is in a common folder
like the Windows OS folder.
public static Process runProgram(String programName)
{
Process handle = null;
try
{
handle = Runtime.getRuntime().exec(programName);
}
catch(java.io.IOException ex){ };
return handle;
}