Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am looking for help in witing 2 classes in this project, 2 of them are already written albiet with some small errors that need

I am looking for help in witing 2 classes in this project, 2 of them are already written albiet with some small errors that need fixing that can be seen below.

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. image text in transcribed

My Code: Voter.java import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class Voter { private String voterID; private String voterName; private boolean voted; public Voter() { } public Voter(String voterID, String voterName, boolean hasVoted) { super(); this.voterID = voterID; this.voterName = voterName; this.voted = hasVoted; } public String getVoterID() { return voterID; } public void setVoterID(String voterID) { this.voterID = voterID; } public String getVoterName() { return voterName; } public void setVoterName(String voterName) { this.voterName = voterName; } public boolean hasVoted() { return voted; } public void vote() { this.voted = true; } public void setVoted(boolean voted) { this.voted = voted; } public static Voter getVoter(String fileName, String voterID) { BufferedReader br = null; FileReader fr = null; Voter voter = null; try { fr = new FileReader(fileName /* "E://Voter.txt" */); br = new BufferedReader(fr); String sCurrentLine; while ((sCurrentLine = br.readLine()) != null) { String[] tabValues = sCurrentLine.split(":"); if (tabValues[0].equals(voterID)) { voter = new Voter(); voter.setVoterID(tabValues[0]); voter.setVoterName(tabValues[1]); voter.setVoted(Boolean.parseBoolean(tabValues[2])); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); if (fr != null) fr.close(); } catch (IOException ex) { ex.printStackTrace(); } } return voter; } public static void saveVoter(String fileName, Voter votor) { BufferedWriter bw = null; FileWriter fw = null; try { BufferedReader file = new BufferedReader(new FileReader(fileName)); String line; StringBuffer inputBuffer = new StringBuffer(); while ((line = file.readLine()) != null) { inputBuffer.append(line); inputBuffer.append(' '); } String inputStr = inputBuffer.toString(); file.close(); String[] array = inputStr.split(" "); for (int i = 0; i LoginPanel.java import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginPanel extends JPanel { String fileName; JLabel messageLbl; JLabel voterIDLbl; JTextField voterIDTxt; JButton button; // This class contains (minimally) two JLabels, a JTextField and a JButton public LoginPanel(final String fileName, LoginInterface lInterface) { super(); this.fileName = fileName; this.setLayout(new GridLayout(3, 1)); JPanel middlePanel = new JPanel(); middlePanel.setLayout(new GridLayout(1, 2)); messageLbl = new JLabel("Please Log into the site"); voterIDLbl = new JLabel("VoterID"); voterIDTxt = new JTextField(""); button = new JButton("Submit"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("Hello"); JFrame f = null; String voterID = voterIDTxt.getText(); System.out.println(voterID); Voter voter = Voter.getVoter("E://"+fileName, voterID); if (voter != null) { if (voter.hasVoted()) { f = new JFrame(); JOptionPane.showMessageDialog(f, voter.getVoterName() + " has already voted !"); } else { f = new JFrame(); JOptionPane.showMessageDialog( f, "Voter ID :" + voter.getVoterID() + " " + voter.getVoterName() + " Voted ? false"); JOptionPane.showMessageDialog(f, "Now Voting"); JOptionPane.showMessageDialog( f, "Voter ID :" + voter.getVoterID() + " " + voter.getVoterName() + " Voted ? true"); JOptionPane.showMessageDialog(f, "Saving Back To file..."); voter.setVoted(true); Voter.saveVoter("E://"+fileName, voter); } } else { f = new JFrame(); JOptionPane.showMessageDialog(f, "You are not ragistered sorry !"); } } }); this.add(messageLbl); middlePanel.add(voterIDLbl); middlePanel.add(voterIDTxt); this.add(middlePanel); this.add(button); } }

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 

When attempting to compile TestVoter.java i get this error

image text in transcribed

When running TestLoginPanel.java, no matter what voter Id you put it it always says that you are not registered and am unsure of how to fix this. Please provide screenshots of any code to prove that it works. Thank you for your time and effort. Note, this is important.

Classes with Test in their name cannot be changed. My program has to work with those. All changes must be made in those.

Here are some snapshots of what the program is suppose to look like while running.

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

To be clear, i am looking for Assig4.java and BallotPanel.java to be written and then the error in Voter.java and Login Panel to be fixed.

Overview: Initially the contents of voters.txt are s The TestVoter program is then executed, and t of voters.txt are shown again. Finally, Test' executed again. Note the difference with id two executions assig4> cat voters.txt 1234:Herb Weaselman:false 9876:Marge Margificent:true 4444 : Ingmar Inglenook:false 8888 Hector Heroman:false 5678:Agnes Angular:true assig4> assig4> java TestVoter Welcome Herb Weaselman with Id 1234! Here is your info: ID: 1234 Name: Herb Weaselman Voted? false Herb Weaselman you are eligible to vote in this elect We will now mark you as having voted ...and save this information back to the file Welcome Agnes Angular with Id 5678! Here is your info: ID: 5678 Name: Agnes Angular Voted? true Sorry, Agnes Angular but you have already voted Sorry, but voter 7777 is not registered assig4> assig4> cat voters.txt 1234: Herb Weaselman: true 9876:Marge Margificent:true 4444 : Ingmar Inglenook:false 8888 Hector Heroman:false 5678:Agnes Angular:true assig4> assig4> java TestVoter Welcome Herb Weaselman with Id 1234! Here is your info: ID: 1234 Name: Herb Weaselman Voted? true Sorry, Herb Weaselman but you have already voted Welcome Agnes Angular with Id 5678! Here is your info: ID: 5678 Name: Agnes Angular Voted? true Sorry, Agnes Angular but you have already voted Sorry, but voter 7777 is not registered Overview: Initially the contents of voters.txt are s The TestVoter program is then executed, and t of voters.txt are shown again. Finally, Test' executed again. Note the difference with id two executions assig4> cat voters.txt 1234:Herb Weaselman:false 9876:Marge Margificent:true 4444 : Ingmar Inglenook:false 8888 Hector Heroman:false 5678:Agnes Angular:true assig4> assig4> java TestVoter Welcome Herb Weaselman with Id 1234! Here is your info: ID: 1234 Name: Herb Weaselman Voted? false Herb Weaselman you are eligible to vote in this elect We will now mark you as having voted ...and save this information back to the file Welcome Agnes Angular with Id 5678! Here is your info: ID: 5678 Name: Agnes Angular Voted? true Sorry, Agnes Angular but you have already voted Sorry, but voter 7777 is not registered assig4> assig4> cat voters.txt 1234: Herb Weaselman: true 9876:Marge Margificent:true 4444 : Ingmar Inglenook:false 8888 Hector Heroman:false 5678:Agnes Angular:true assig4> assig4> java TestVoter Welcome Herb Weaselman with Id 1234! Here is your info: ID: 1234 Name: Herb Weaselman Voted? true Sorry, Herb Weaselman but you have already voted Welcome Agnes Angular with Id 5678! Here is your info: ID: 5678 Name: Agnes Angular Voted? true Sorry, Agnes Angular but you have already voted Sorry, but voter 7777 is not registered

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_2

Step: 3

blur-text-image_3

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

Object Databases The Essentials

Authors: Mary E. S. Loomis

1st Edition

020156341X, 978-0201563412

More Books

Students also viewed these Databases questions

Question

What is linear transformation? Define with example

Answered: 1 week ago

Question

What do Dimensions represent in OLAP Cubes?

Answered: 1 week ago