LAB EXERCISE 21: Employee Data 1

Goal: In this exercise, you will learn how to:
1. Insert the Main Method (Main Entry Point to the Java Program)
2. Import the Library Classes which allow data to be written to an external text file
    a. import java.io.File
    b. import java.io.FileWriter
    c. import java.io.BufferedWriter
    d import java.io.IOException
3. Create a Superclass
4. Create a Subclass
5.  Declare Public Variables that can be accessed by a Subclass
6.  Create a Constructors
7.  Create a Void Method
8.  Use a Try-Catch block statement for error file handling and exceptions
9.  Create the Object from the Class
10.  Call the Object’s Variables


Program Specifications:
This program prompts the user to the input employee information and then write and store the data into a text file - employee.txt.
 
A. Pre-requisites:
1. Create a folder on your desktop Exercise-21

2. Launch Java EE- Eclipse
Note:  You will need to use the Java Perspective Workbench for this exercise

3. Setup your Eclipse Workspace to point to the Exercise-21 folder
a. Select File-> Switch Workspace
b. Browse and select your Exercie-21 folder as your Workspace.



B. Requirements:

1.  Create a Java Project and name it as EmployeeDataPart1

2.  Create the first Class that will have the Main Method

a.       Name the Class as MainApp

b.       Choose the main method to insert into the class

        

3.  Create a class called Employee.  This will be the superclass (parent)

4.  Create a class called Data.  This subclass that will inherit the methods and variables of the Employee superclass

 

C. Requirements for the Employee Class:

1.  Insert the import java.util.Scanner class which will allow data to be inputted from the console. 

2.  Create the public variables using String data type that will be used in the subclass
fName, lName, employeeID, department, phone and emailAddress

3.  Create the Constructor called Employee that will prompt the user to input the data
Below is the code for the Constructor.

// declare the scanner object that will be used to input the employee's first name
Scanner input1= new Scanner(System.in);
System.out.print("Enter First Name: ");
fName = input1.nextLine();  // assign the input value to the variable

// declare the scanner object that will be used to input the employee's last name
Scanner input2 = new Scanner(System.in);;  // assign the input value to the variable= new Scanner(System.in);
System.out.print("Enter Last Name: ");
lName = input2.nextLine();  // assign the input value to the variable

                 
// declare the scanner object that will be used to input the employee's ID
Scanner input3 = new Scanner(System.in);;  // assign the input value to the variable= new Scanner(System.in);
System.out.print("Enter Employee ID: ");
employeeID = input3.nextLine();  // assign the input value to the variable

// declare the scanner object that will be used to input the employee's department
Scanner input4 = new Scanner(System.in);;  // assign the input value to the variable= new Scanner(System.in);
System.out.print("Enter Department: ");
department = input4.nextLine();  // assign the input value to the variable

// declare the scanner object that will be used to input the employee's phone
Scanner input5 = new Scanner(System.in);;  // assign the input value to the variable= new Scanner(System.in);
System.out.print("Enter Telephone Number: ");
phone = input5.nextLine();  // assign the input value to the variable

                 
// declare the scanner object that will be used to input the employee's email address
Scanner input6 = new Scanner(System.in);;  // assign the input value to the variable= new Scanner(System.in);
System.out.print("Enter Employee Email Address: ");
emailAddress = input6.nextLine();  // assign the input value to the variable

// close the scanner input objects
input1.close(); input2.close(); input3.close();input4.close();input5.close();input6.close();

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


D. Requirements for the Data Class:

1.  Insert the following Library Classes which will write and store the data to an external text file:
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;

 

2.  Create a Void Method called writeData () that will execute the code to write the data to an external file – employee.txt

a.  Create instance of the object filename from the File library class & pass the file name “employee.txt” as a parameter

File filename = new File("employee.txt");

b.  Use Try-Catch block to perform error handling if the program cannot write to the external file
     b1. Create the instance of the object "fw" from the FileWriter library class
            & pass value of "filename" object as the parameter

FileWriter fw = new FileWriter(filename);


          b2.  Create instance of the object "br" from the BufferredWriter library class
                 & pass the value of the "fw" object as the parameter

BufferedWriter br = new BufferedWriter(fw);

          b3. Write the data to the employee.txt file using "br" object write method

br.write(fName);
br.newLine();
br.write(lName);
br.newLine();
br.write(employeeID);
br.newLine();
br.write(department);
br.newLine();
br.write(phone);
br.newLine();
br.write(emailAddress);

         b4. Close the text file - employee.txt

br.close();

 

     c. In the Catch-block parameter, add (IOException e)
         Insert the code below to display the error handling message

System.out.println("Unable to write to file " + filename.toString());

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


E. Requirements for the MainApp Class:

1.  Add comments (documentation)– Program Description, Author and Date

2.  Create an instance of the object - employeeInfo from the Data class:
Data employeeInfo = new Data()

3. Call the object's method- writeData:
    employeeInfo.writeData()

4. Display the output message to the console

System.out.println();
System.out.println("Data has been written to employee.txt file successfully");

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


F. Test:  

1.   Save your Java code

2.   Compile and run your Java program.

3.   Verify there is no syntax, logical or run-time errors.

4.   Use the following set of test data to determine if the application is running properly

 

 

 

5.  Expand the Project Folder.  Press F5 to Refresh.  Verify the employee.txt is in the root of the project folder.

 

 

 

 

 

 

 

 

 


G. Submit your exercise in the Canvas Lab Exercise #21 Drop Box.

1. Submit the screen shot of the Eclipse Workbench window showing the Console output screen.
You can use Paint (save as JPG) or Word to paste the screenshot.

2.  Submit the employee.txt data file.

3. Zip up and submit the compressed EmployeeDataPart1 subfolder that is in the Exercise-21 folder.
NOTE: Right click on the
subfolder and select Send to “Compress Folder”.  The file will have a file extension of .zip.

        NOTE:   You will need to upload the 3 files above separately.