Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am looking for someone to help me by writing the classes or part of the classes as i am lost. Everything that needs to

I am looking for someone to help me by writing the classes or part of the classes as i am lost. Everything that needs to be in the class is below.

I would like some help in completing this project. Please and thank you. Idea:

Most polling places now use computerized voting machines for this process. We'd like to get an idea of how a voting program might work (if your program is good, it can always be used next year!). In this program you will implement a simple yet flexible "voting machine" with the following abilities:

1) It can accept an arbitrary number of ballots, each with an arbitrary number of candidates, and allow users to vote for one candidate for each ballot.

2) It allows users to vote only if they are "registered" and allows them to vote only one time.

3) It keeps track of the number of votes for each candidate in each office in a well-organized way, by associating a file name with each ballot.

4) It updates all data files (vote count files and voter file) as soon as possible and in a way that (mostly) protects the data if the program crashes during the update.

Class Requirements / Details:

Your program is required to be implemented in a modular, object-oriented way. Specifically, you must write (minimally) the following 4 classes:

public class Voter

This class should encapsulate a Voter by storing a voter ID (String), voter name (String) and a record or whether or not a Voter has voted (boolean). It should have accessors to get each of these attributes, and a constructor and a toString() method. It should have a mutator (vote()) that will set its "voted" attribute to true. It should also have two static methods: one to allow a new Voter to be found / retrieved from a file and one to save a Voter back to the file in a safe way (see "Saving Files" section below for more information on this technique). Your Voter class must work with the provided main program TestVoter.java. See comments in TestVoter.java for more details on the required methods / functionality and see sample output in file TestVoter.txt.

public class LoginPanel extends JPanel

This class contains (minimally) two JLabels, a JTextField and a JButton. The LoginPanel will allow the user to type in a Voter ID and to search in a text file for the voter's id and to return a new Voter object associated with that id. Class LoginPanel should have a constructor that takes a voter file name (String) and a LoginInterface reference. The LoginInterface reference is a way for the LoginPanel to pass the Voter that it finds back to the main program. Your LoginPanel class must work with the provided main program TestLoginPanel.java. For more details on the required methods and functionality of LoginPanel.java see the program TestLoginPanel.java and see the snapshot file TestLogin.htm.

public class BallotPanel extends JPanel

This class can be organized in many different ways, but it must have the ability to store and utilize an arbitrary number of "ballots". Each "ballot" must encapsulate the functionality required for voting / processing the results in a given race / office. This includes selecting / reselecting candidates and updating the corresponding files (in a safe way) when the user is finished voting. To see how this class will be accessed and how it works, see the test program TestBallotPanel.java and the snapshot file TestBallot.htm. Carefully read over the code and comments in TestBallotPanel.java to see the methods that you must write and the functionality that they should have.

Note that the BallotPanel class is not trivial and it will require some thought to develop. One approach that I recommend is to write a Ballot class that encapsulates a single office / election. You can then write the BallotPanel class to contain an array of Ballot. The size of the array can be determined when the ballot file is parsed. Regardless of how you implement an individual ballot, the BallotPanel class will need to have event handlers to process each ballot and to finalize / cast the vote. It will also require some logic for accessing / updating the files.

public class Assig4

This is your main voting program. It should allow voters to login by entering their voter ids and then to vote for the offices in the ballot file. This program is (more or less, with some extra logic) combining the functionalities of the TestLoginPanel.java and TestBallotPanel.java programs. It will also utilize all of your other three classes. Because of this writing the Assig4 program should not be too difficult. To see the required overall behavior of the Assig4 program, see the snapshot file TestAssig4.htm.

In addition to the look, feel and functionality shown in the snapshot files above, your program MUST also follow the specifications below:

All interaction with the user should be handled graphically, using JButtons (within JPanels), JTextFields and JOptionPanes. No console I/O should be done (except for debugging purposes).

The ballots for a given election must be stored in a single text file, formatted as specified in TestBallot.htm. Some sample ballot files are provided for you.

The voters for a given election must be stored in a single text file, formatted as specified in TestVoter.txt. A sample voter file is provided for you.

The names of the voters and ballots files can be arbitrary and should be read in from the command line when the program is started. For details on reading from the command line, see Section 7.12 in the Gaddis text. The idea behind this is that the voter would not actually be starting the program - rather the voting coordinator / inspector would start it, knowing which files of voters and ballots to utilize.

As shown in the TestBallot.htm snapshots, voting must be done in three steps:

1) Selection of candidates from the ballots. The choices must be able to be repeatedly changed if desired.

2) Casting the ballots. This should be a button in BallotPanel that the user can click on at any time.

3) Confirmation of the choices. This should be a JOptionPane dialog that asks the voter to confirm his/her choices. If the voter confirms, the choices are recorded and the voted() method is called. If the voter does not confirm, he/she goes back to the ballots and can once again modify them if desired. Note that for a given ballot, it is possible that no choice is selected. This is ok and should be handled. For help with the confirmation dialog, see the API for the JOptionPane class.

After a user votes, the files (both for the results for each office and for the voters) should be updated immediately in a "safe" way. Specifically the static saveVoter() method in the Voter class and a method to save the results in your BallotPanel class should work in the following way:

Read in the original file line by line

If a line needs to be modified (ex: a vote count or voter status), do so in your program

Write the lines back to a new file (i.e. you are copying the file, but with the appropriate modifications). The temporary name for this new file should be something that is not likely to be a name of a regular file. For example: _temptemp.txt or something similar should work.

When the new file is complete, delete the original file and rename the new file with the original file name

The file name for each ballot should be the id number for the ballot followed by the ".txt" extension. For example, for ballot "12345" the file should be "12345.txt".

The idea is that if something bad happens while you are writing the new file, the original file will not be affected, so only the most recent vote will be lost. For some help with this, see the API for the File class and its various methods. Provided Code: TestVoter.java

// Driver program to test Voter class // This program should run without any changes utilizing your Voter class. // Note both the instance methods and the static methods that are used here. // To see the results of this program execution, see file TestVoter.txt import java.util.*; import java.io.*; public class TestVoter { public static void main(String [] args) { Voter V; String [] tests = {"1234", "5678", "7777"}; for (String S: tests) { // getVoter() is a static method and thus called from the class directly. // getVoter() takes two arguments -- the String name of a voters file and // a String voter id value. If the voter id is found in the file it will // return a new Voter object created from the data in the file. If the // voter id is not found it will return null. V = Voter.getVoter("voters.txt", S); if (V == null) System.out.println("Sorry, but voter " + S + " is not registered "); else { // Note some Voter instance methods that are called below String name = V.getName(); String id = V.getId(); System.out.println("Welcome " + name + " with Id " + id + "!"); System.out.println("Here is your info:"); System.out.println(V.toString()); if (V.hasVoted()) System.out.println("Sorry, " + name + " but you have already voted "); else { System.out.println(name + " you are eligible to vote in this election!"); System.out.println("We will now mark you as having voted"); V.vote(); System.out.println("...and save this information back to the file "); // saveVoter() takes two arguments -- the String name of a voters file // and a Voter reference. It will rewrite the voters file in a "safe" // way, updating the record corresponding to V and leaving the other // records unchanged. For more information on "safe" saving of the file // see the Assignment 4 document. Voter.saveVoter("voters.txt", V); } } } } } LoginInterface.java 
// This interface is used by the LoginPanel to make a call back to the object // that initiated it. See how the initiating object is passed into the LoginPanel // and see the code for it in TestLoginPanel.java. public interface LoginInterface { public void setVoter(Voter newV); } TestLoginPanel.java 
// This program tests the LoginPanel class, which is required for Assignment 4 // Note how it is used and also note the snapshots shown in TestLogin.htm import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TestLoginPanel implements LoginInterface { private JFrame theWindow; private String vFileName; private Voter P; private LoginPanel logPan; private JButton login; public TestLoginPanel() { theWindow = new JFrame("Testing Login Panel"); theWindow.setLayout(new GridLayout(1,2)); vFileName = "voters.txt"; login = new JButton("Click to Login"); login.setFont(new Font("Serif", Font.ITALIC + Font.BOLD, 30)); login.addActionListener(new LoginListener()); theWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); theWindow.add(login); theWindow.pack(); theWindow.setVisible(true); } // This method is from LoginInterface and will be called from the // LoginPanel. The idea is that information obtained from LoginPanel // will be encapsulated within that Panel, and this method enables the // Voter to be passed back to this program. Once this method is called // the LoginPanel is removed from the display. Then we can use the // returned Voter object however we see fit. In this case we simply // call the vote() method and save it back to the file. However, in // your main program you will first have the Voter make the ballot // choices and actually vote. public void setVoter(Voter newVoter) { P = newVoter; theWindow.remove(logPan); theWindow.add(login); theWindow.pack(); JOptionPane.showMessageDialog(theWindow, "Voter: " + P.toString()); JOptionPane.showMessageDialog(theWindow, "Now voting..."); P.vote(); JOptionPane.showMessageDialog(theWindow, "Voter: " + P.toString()); JOptionPane.showMessageDialog(theWindow, "Saving back to file..."); Voter.saveVoter(vFileName, P); } // When the login button is clicked we add the LoginPanel to the window // and allow it to "do its job". When it is finished, it will pass the // new Player back with the setPlayer method. For details on the expected // functionality of LoginPanel, see the snapshot file TestLogin.htm private class LoginListener implements ActionListener { public void actionPerformed(ActionEvent e) { // Note that the TestLoginPanel object is passed as an argument to // the LoginPanel. Since TestLoginPanel implements LoginInterface // it can be accessed in that way from LoginPanel. In other words, // the header for this constructor should be: // public LoginPanel (String voterFile, LoginInterface L) // In your LoginPanel class you should store this reference as an // instance variable since you will need it to call the setVoter() // method once the login is complete. logPan = new LoginPanel(vFileName, TestLoginPanel.this); theWindow.remove(login); theWindow.add(logPan); theWindow.pack(); } } public static void main(String [] args) { new TestLoginPanel(); } } TestBallotPanel.java  
// Driver program to test BallotPanel class. Your BallotPanel class must work with this // program with no changes. To see how the execution should work, see the file // TestBallot.htm import java.util.*; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestBallotPanel implements VoteInterface { // You must implement the BallotPanel class. For some details on the required // functionality see Assignment 4 and TestBallot.htm private BallotPanel ballots; private JButton startButton; private JFrame theWindow; private String ballotFile; private ActionListener startListener; public TestBallotPanel(String bFile) { ballotFile = bFile; // Constructor takes a String ballot file name and a reference back to the // TestBallotPanel object. This back reference allows the BallotPanel object // to signal back to this panel when it is "done" with the voting. This // back reference is implemented via the VoteInterface interface. See also // some comments in file VoteInterface.java // // The constructor should parse the ballot file make a new graphical ballot // for each office and also a new results file for each office. See details // in TestBallotPanel.htm. Because this will create a new file for each office, // you only want to create this BallotPanel one time when the program is first // started. However, (as demonstrated here) the panel can be shown and hidden // and can be reset for each voter. ballots = new BallotPanel(ballotFile, this); startButton = new JButton("See your ballots"); startButton.setFont(new Font("Serif", Font.BOLD, 30)); startButton.setEnabled(true); startListener = new StartListenType(); startButton.addActionListener(startListener); theWindow = new JFrame("BallotPanel Test Program"); theWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); theWindow.setLayout(new FlowLayout()); theWindow.add(startButton); theWindow.pack(); theWindow.setVisible(true); // Note that the BallotPanel has not been added to the window yet. This will // only happen in response to a click of the startButton. } // This method will be called FROM the BallotPanel to indicate that the voter has // finished voting. This should be called after the votes have been finalized and // the results files have been updated. Note that we are simply removing the // BallotPanel from the window and adding back the startButton. To see how this // works see TestBallotPanel.htm public void voted() { theWindow.remove(ballots); theWindow.add(startButton); theWindow.pack(); } private class StartListenType implements ActionListener { public void actionPerformed(ActionEvent e) { // User has clicked on the startButton, so show the BallotPanel by adding // it to theWindow and call the resetBallots() method to get it ready for // the next voter. if (e.getSource() == startButton) { theWindow.remove(startButton); theWindow.add(ballots); ballots.resetBallots(); theWindow.pack(); } } } // A "one line" main here just creates a Lab8 object public static void main(String [] args) throws IOException { new TestBallotPanel(args[0]); } } VoteInterface.java 
// This interface is used by the BallotPanel to make a call back to the object // that initiated it. This method simply signals to the linked // object that the voting has been completed. public interface VoteInterface { public void voted(); } 
Initial Text Files~~~ voters.txt 
1234:Herb Weaselman:false 9876:Marge Margificent:true 4444:Ingmar Inglenook:false 8888:Hector Heroman:false 5678:Agnes Angular:true 

ballots.txt

3 11111:Best Punk:Ramones,Clash,Black Flag,X,Bad Brains 22222:Best Movie:Princess Bride,Dead Poets Society,Casablanca 33333:Best Phone:iPhone,Galaxy,Pixel,OnePlus 

ballots2.txt

4 44444:Best Sci Fi:Avatar,Star Wars,Alien,The Matrix,Dark City 55555:Favorite Computer:Mac,PC 66666:Best Tablet:iPad,Kindle,Nexus,Surface 77777:Best Marvel Villain:Galactus,Onslaught,Doctor Doom,Magneto,Thanos 

Im so confused on this project and would appreciate any help given. Please do not mark this question as need more details or give a spam answer, as thats all ive been getting. I cannot provide more details as this is what i was provided. To those of you seriously willing to help thankyou from the bottom of my heart.

Snapshots:

Assig4:

image text in transcribed

image text in transcribed

TestLogin: image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

TestBallot:

image text in transcribed

image text in transcribed

image text in transcribed

TestVoter:

image text in transcribed

Welcome to E-V Click to Login Welconme to E-Vote Please log into the site Voter ID: 1234 Submit Welcome Herb Weasel ma Click to Vote Herb Weaselman please make your choices Best Punk Best Phone Best Movie Priacess Bride iPhone Galaxy Pisel One Plus Clash Black Flag Cast Your Vote Dead Poets Society Bad Brains

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago