Results 1 to 1 of 1
- 12-05-2012, 05:57 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 22
- Rep Power
- 0
Reading from a file, Writing data to it and other problems
Hi, I am having trouble writing code for this particular part of my assignment. Would much appreciate it any help
This is my Panel.
Java Code:import javax.swing.*; import javax.swing.border.TitledBorder; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** * @author Prabhdeep Singh- CS 103 * Driver class * Gets information from the user as to what sort of employee */ public class WindowPanel extends JPanel { private JButton B1, B2, B3, B4, B5; // buttons private JRadioButton RB1, RB2, RB3, RB4; // radiobuttons private JTextField TF1, TF2,TF3; // textfields private JLabel L1, L2, L3; // labels private EmployeesManager Moneyroll; // Class EmployeesManager /** * Provides all the buttons, radio buttons, text fields, and bordered titles which are used in the gui */ public WindowPanel() // Constructor { this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); JPanel Panel1 = new JPanel(); TitledBorder Titled1 = BorderFactory.createTitledBorder("Type of employee"); RB1 = new JRadioButton("Commission Worker", true); RB2= new JRadioButton("Piece Worker"); RB3 = new JRadioButton("Manager"); RB4 = new JRadioButton("Hourly Worker"); ButtonGroup BigGroup = new ButtonGroup(); BigGroup.add(RB1); BigGroup.add(RB2); BigGroup.add(RB3); BigGroup.add(RB4); RB1.addActionListener(new RadioButtonListener()); RB2.addActionListener(new RadioButtonListener()); RB3.addActionListener(new RadioButtonListener()); RB4.addActionListener(new RadioButtonListener()); Panel1.setBorder(Titled1); Panel1.add(RB1); Panel1.add(RB2); Panel1.add(RB3); Panel1.add(RB4); add(Panel1); JPanel Panel2 = new JPanel(); L1 = new JLabel("Employee ID: "); L2 = new JLabel("Weekly Sales"); L3 = new JLabel(); TF1 = new JTextField(15); TF2 = new JTextField(10); TF3 = new JTextField(5); L3.setVisible(false); TF3.setVisible(false); Panel2.add(L1); Panel2.add(TF1); Panel2.add(L2); Panel2.add(TF2); Panel2.add(L3); Panel2.add(TF3); add(Panel2); JPanel Panel3 = new JPanel(); B1 = new JButton("Add Employee"); B2 = new JButton("Write Report to File"); B3 = new JButton("Display Summary Pay Report"); B4 = new JButton("Read From File"); B5 = new JButton("Exit"); B1.addActionListener(new JButtonListener()); B2.addActionListener(new JButtonListener()); B3.addActionListener(new JButtonListener()); B4.addActionListener(new JButtonListener()); B5.addActionListener(new JButtonListener()); Panel3.add(B1); Panel3.add(B2); Panel3.add(B3); Panel3.add(B4); Panel3.add(B5); add(Panel3); Moneyroll=new EmployeesManager(); } private class RadioButtonListener implements ActionListener // RadioButton ActionListener Class { public void actionPerformed(ActionEvent e) { Object Clicker = e.getSource(); if (Clicker == RB1 ) { L2.setText("Weekly Sales"); TF3.setVisible(false); L3.setVisible(false); } else if (Clicker == RB4) { L2.setText("PayRate"); TF3.setVisible(true); L3.setVisible(true); L3.setText("Hours"); } else if (Clicker == RB3) { L2.setText("Salary"); TF3.setVisible(false); L3.setVisible(false); } else if (Clicker == RB2) { L2.setText("Piece Rate"); L3.setText("# pieces"); TF3.setVisible(true); L3.setVisible(true); } } } private class JButtonListener implements ActionListener // ButtonListener Class { public void actionPerformed(ActionEvent e) { if (e.getSource()==B1) { if (RB1.isSelected()) { Moneyroll.addEmployee(3, Double.parseDouble(TF2.getText()), 0, Integer.parseInt(TF1.getText())); } else if(RB3.isSelected()) { Moneyroll.addEmployee(1, Double.parseDouble(TF2.getText()), 0, Integer.parseInt(TF1.getText())); } else if (RB4.isSelected()) { Moneyroll.addEmployee(2, Double.parseDouble(TF2.getText()), Integer.parseInt(TF3.getText()), Integer.parseInt(TF1.getText())); } else if (RB2.isSelected()) { Moneyroll.addEmployee(4, Double.parseDouble(TF2.getText()), Integer.parseInt(TF3.getText()), Integer.parseInt(TF1.getText())); } } else if (e.getSource() == B2) { } else if(e.getSource()==B3) { try { PrintWriter outputFile = new PrintWriter("outputfile.txt"); outputFile.println(Moneyroll.generateWeeklyReport()); outputFile.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } JOptionPane.showMessageDialog(null, Moneyroll.generateWeeklyReport()); } else if(e.getSource()==B4) { JFileChooser fileChooser = new JFileChooser(); int status = fileChooser.showOpenDialog(null); if(status == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); try { Scanner scan = new Scanner(selectedFile); int EmployeeID, Position, Ticker; //Creates variable that will hold the employee id, hours, or number of pieces double Payment; //Creates a variable that will hold the salary, weekly sales, payrate, or piece rate while(scan.hasNextLine()) { Position = scan.nextInt(); //Gets the position choice from the file if(Position==4 || Position==2) { Payment = scan.nextDouble(); //Gets the payrate or the piece rate from the file Ticker = scan.nextInt(); //Gets the hours or num of pieces EmployeeID = scan.nextInt(); //Get the employee ID } else { Payment = scan.nextDouble(); //Gets the weekly sales or salary from the file Ticker = 0; EmployeeID = scan.nextInt(); //Get the employee ID } Moneyroll.addEmployee(Position, Payment, Ticker, EmployeeID); //Adds the data from the file to the array } scan.close(); String filename = selectedFile.getPath(); JOptionPane.showMessageDialog(null, "You selected " + filename); } catch (FileNotFoundException e1) { e1.printStackTrace(); } //Creates a new scanner that will read from a file } } else if (e.getSource()==B5) { System.exit(0); } } } }This is my Employee Manager class. The problem i am having is, as u can see the code under if(e.getsource()==B2) being empty, is that after reading in a file using the read from file button, i am suppose to put data for another worker using the gui, and when i click write report to file, its suppose to write the data to the file i read in, and when i click the display summary pay report, it should show the new worker data i added using the gui and its format should fit the generate weekly report format in the employee manager class i included. Any help would be appreciated. i have tried uploading my assignment file but it just doesnt seem to be working.Java Code:import java.util.ArrayList; import java.text.NumberFormat; /** * This EmployeesManager implements EmployeesManagerInterface provided by the teacher * @author Prabhdeep Singh * */ public class EmployeesManager implements EmployeesManagerInterface { private ArrayList<Employee> Moneyroll; // ArrayLists private NumberFormat form=NumberFormat.getCurrencyInstance(); // Formats the number private int NumberOfHourly; // hourly works private int NumberOfCommission; // commission works private int NumberOfPiece; // piece works private int NumberOfManagers; // manager works /** * EmployeesManager Constructor */ public EmployeesManager() // constructor which initializes all values { NumberOfHourly=0; NumberOfManagers=0; Moneyroll=new ArrayList<Employee>(); NumberOfCommission=0; NumberOfPiece=0; } /** * @param paycode * @param firstParam * @param SecondParam * @param empNum */ public void addEmployee(int paycode, double firstParam, int secondParam, int empNum) // Gets the different paycodes then implements them { if (paycode==1) { NumberOfManagers++; Moneyroll.add(new Manager(empNum, firstParam)); } else if(paycode==2) { NumberOfHourly++; Moneyroll.add(new HourlyWorker(empNum, firstParam, secondParam)); } else if (paycode==3) { NumberOfCommission++; Moneyroll.add(new CommissionWorker(empNum, firstParam)); } else if(paycode==4) { NumberOfPiece++; Moneyroll.add(new PieceWorker(empNum, firstParam, secondParam)); } } /** * @return NumberOfHourly */ public int getNumHourlyWorker() // getter { return NumberOfHourly; } /** * @return NumberOfManagers */ public int getNumManager() // getter { return NumberOfManagers; } /** * @return NumberOfComission */ public int getNumCommissionWorker() // getter { return NumberOfCommission; } /** * @return NumberOfPiece */ public int getNumPieceWorker() // getter { return NumberOfPiece; } /** * @return FinalReport - A huge Report of what happened in the program. */ public String generateWeeklyReport() // Weekly Report of the entire thing { String FinalReport="WEEKLY PAY REPORT FOR WIDGET COMPANY\n\nEMPLOYEE WEEKLYPAY\n "; for (Employee y: Moneyroll) { FinalReport+=y.getId()+" "+form.format(y.getWeeklyPay())+"\n "; } FinalReport+="\n"; FinalReport+="Total payroll: "+form.format(this.calculateTotalWeeklyPay())+"\n"; FinalReport+="Total number of Managers paid:" +NumberOfManagers+"\n"; FinalReport+="Total number of Hourly Workers paid:" +NumberOfHourly+"\n"; FinalReport+="Total number of Commission Workers paid:" +NumberOfCommission+"\n"; FinalReport+="Total number of Piece Workers paid:" +NumberOfPiece+"\n"; return FinalReport; } /** * @return Final Total, which is the totalWeeklyPay */ public double calculateTotalWeeklyPay() // calculates the total weeklypay { double FinalTotal=0; for (Employee x: Moneyroll) { FinalTotal+=x.getWeeklyPay(); } return FinalTotal; } }
Similar Threads
-
Reading from and writing to file
By dyelax in forum New To JavaReplies: 3Last Post: 10-17-2012, 08:32 PM -
Writing and Reading Data from a file without BufferedReader
By son012189 in forum New To JavaReplies: 5Last Post: 12-02-2011, 07:15 PM -
Writing and Reading from a file?
By SilentCoder in forum New To JavaReplies: 5Last Post: 06-10-2011, 03:12 AM -
Writing and Reading data to and from xml file to JTable
By ramihemang90 in forum Advanced JavaReplies: 3Last Post: 03-15-2011, 02:07 PM -
Reading data from Micrsoft excel and writing to notepad
By abhishek.jain in forum New To JavaReplies: 4Last Post: 01-29-2009, 08:12 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks