File handling for the payroll project

Document Sample
File handling for the payroll project Powered By Docstoc
					Project Notes 3

Note: These notes can be used as reference (in conjunction with the program listings) when
dealing with the last stage of the project (enhancement to the system). They can also be used
as a reference on how to handle binary files in java.

=========================================================================
File handling for the payroll project



The file will contain a "record" for each employee. When processing our payroll the input file will be
read. An employee will be treated as an object. Pay will be calculated and year to date values updated

To keep these values for the next time around we need to store them on an output file. The employees
will be in the same sequence on the output file. The output file from one run will become the input to
the next payroll run.




                                               Payroll
        Empfile1.dat
                                                                                        Empfile2.dat
        (binary file)
                                               Program                                  (binary file)
                                               (payapp.java)



                                                                                       payslip.txt
              Report1.txt                                                              (text file)
              (text file)




Note: The reason we need an output employee file is because we are using sequential files.
      With random access files each record would be updated in place.
      The output file from one month’s payroll run will be the input to the next.

See project notes 2 for a summary of the classes used in the payroll project

Note: There will also be 2 text files outputted from the payroll run, one for the payslips, and one for the
report. These are just temporary files to hold the data until the payslips and report are printed.




CA591/CA598 Assignment 2009 -project notes 3                                                    p 1 of 5
Processing Binary Files in Java (see Pohl 10.6)

Java Classes used

For Input :-

 DataInputStream
 FileInputStream

For Output:

 DataOutputStream
 FileOutputStream

Methods for reading
  readBoolean()
  readByte ()
  readChar()
  readDouble()
  readFloat()
  readInt()
  readLong()
  readShort()
  readUTF()

Methods for writing – same as above with write instead of read

    writeBoolean()
    writeByte ()
    …..etc.


In the project, EmpInFile and EmpOutFile are the classes used for handling the input from and
output to the employee files. Through these classes you will read from a java binary file and
write to a java binary file.


Reading and writing a binary file in the project

The method readRecord() in the class EmpInFile reads the details from the employee record and
sets the details up in the instance variables in the employee object. Below is the implementation of the
method which you should use.




CA591/CA598 Assignment 2009 -project notes 3                                                 p 2 of 5
//=====================================================================
//
// readRecord - reads the next record in sequence from the file
//
// Input Argument: reference to an Employee Object
// invokes:     methods from employee object to set up this
//            employee's values
//
//==============================================================
   public boolean readRecord(Employee pers)
   {
      boolean end = false; // set to true when eof exception recognised

    // input the values from the file

    try {

       pers.setId(input.readInt());
       pers.setFirst(input.readUTF());
       pers.setLast(input.readUTF());
       pers.setSalary(input.readDouble());
       pers.setTaxCreds(input.readDouble());
       pers.setTaxCode(input.readChar());
       pers.setDeptCode(input.readChar());
       pers.setYtdGross(input.readDouble());
       pers.setYtdTax(input.readDouble());

     }//end try block

     catch ( EOFException eof )
     {
        end = true;
     } // catch eof
     catch ( IOException e )
    {
        System.err.println( "Error during read from file\n" +
                    e.toString() );
        System.exit( 1 );
     } // catch error
     return end;            // return eof true or false
 } // read record

Note – An input parameter to this method is the reference to the employee object (pers)

pers.setId( input.readInt());

Note how the fields in the employee record are read and similarly passed to the employee object.
The order of these statements is extremely important as they must match the order in which these
fields were written to the file initially.




CA591/CA598 Assignment 2009 -project notes 3                                              p 3 of 5
In the EmpInFile class create an instance of a DataInputStream object and let’s call this variable
input. The java class DataInputStream contains methods for reading from a binary file. When
“opening” the file the object is associated with the FileInputStream object which makes the
connection with your physical file on disk. These should be instantiated in the method openfile() in
the class EmpInFile.


Writing to the employee file
Similar to the readRecord method in the EmpInFile class, the method addRecord() in the class
EmpOutFile and should use the get methods of the employee class to pick up the fields of the
employee record and write them out to the file with the write methods.

e.g output.writeInt(pers.getId())

output is the reference to a dataOutputStream object which is associated with the
FileOutputStream object which makes the connection with your p hysical file on disk.
These are instance variables instantiated in the method
 openFile () in the class EmpOutFile.



The PayApp class

PayApp.java - is the application class that drives the program. It contains a loop to process all the
employees on the file. It gets access to your classes inside the loop with the statement

slip.printPay(slipWrite)

This calls the printPay method of the payslip class which in turn calls the calcPay method of the
employee class.

There are no changes required in this for any part of the project. You may wonder why there is only
one employee reference (emp) even though there are lots of employees. As we are processing the file
in sequence each employee is completed before moving on to the next so there is no need to hold on
to other employee objects.

end_of_file = f1.readRecord(emp)

reads the next employee record from the file and fills the instance variables of the employee object
pointed to by emp. This replaces the details for the previous employee. (It returns true if end of file
has been reached.)

This differs from the petrol pump class where you might have more than one pump active at a time, so
you needed a number of PetrolPump objects.




CA591/CA598 Assignment 2009 -project notes 3                                                  p 4 of 5
Gui interface and event handling

The utility program CreateEmpfile () takes input in through text boxes on the screen and
writes the details to a file. Note it uses the package AWT (Abstract Windowing Toolkit)
import java.awt.*;
import java.awt.event.*;
It uses classes for event handling. This refers to actions which happen as a result of some
action, example clicking on a button.

Note in the constructor you will see the boxes being drawn on the screen.

There are two buttons added (enter and done) and these are associated with some action.

enter = new Button( "Enter" );
enter.addActionListener( this );

done = new Button( "Done" );
done.addActionListener( this );

The “ActionListener” will cause processing to branch to the method actionPerformed when
either button is pressed.

Note the first statement in actionPerformed() is a call to the method addRecord() which
writes to the employee file the details that were entered in the text boxes.

Converting text
All input through the screen comes in as text, so integer values and double values must be
converted to binary before writing to disk. Java wrapper classes are used for this. See below
for details.




CA591/CA598 Assignment 2009 -project notes 3                                         p 5 of 5
 empID = Integer.parseInt( IDField.getText() );

  if ( empID > 0 )
 {

           // input into temporary variables
            String tempFirst = firstNameField.getText() ;
            String tempLast = lastNameField.getText() ;
            double tempSal = Double.parseDouble(salaryField.getText()) ;
            double tempCred = Double.parseDouble(taxCredField.getText()) ;
            String tcod = (taxCode.getText());
            String dcod = (deptCode.getText());

           // write out to file - will not get here if error
             output.writeInt( empID);
             output.writeUTF( tempFirst );
             output.writeUTF( tempLast );

            output.writeDouble(tempSal);
            output.writeDouble( tempCred );
            output.writeChar(tcod.charAt(0));
            output.writeChar(dcod.charAt(0));


         // set year to date fields to zero

            output.writeDouble(0.00);
            output.writeDouble (0.00);

 }




CA591/CA598 Assignment 2009 -project notes 3                                 p 6 of 5
Wrapper Classes
 Java provides a way to wrap a primitive data type into an object so that it can be manipulated. The
corresponding class is called a wrapper class in Java terminology. The wrapper class provides
constructors, constants, and conversion methods for manipulating various data types. The name of the
class is the same as the basic type but begins with a capital letter. The class also has useful class methods
(static) which returns the integer or double value of a String.

In the example above note
empID = Integer.parseInt( IDField.getText() );
double tempSal = Double.parseDouble(salaryField.getText()) ;
double tempCred = Double.parseDouble(taxCredField.getText()) ;




These are later written to the file with the statements
output.writeInt(empId);
output.writeDouble(tempSal) etc.


CA591 students: When doing part 2 (Enhancements) you will need to add code to the provided classes
for input and output. Study the classes carefully and identify the relevant methods. See notes on part 2
in projnotes2. (Stage 4)




CA591/CA598 Assignment 2009 -project notes 3                                                     p 7 of 5

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:10
posted:4/4/2011
language:English
pages:7