I am working on a project that requires the main system, EmployeeSystem to be able to open a file with JFileChooser, and then write to that file, run a payroll system on it, and search it for a particular employee using a directory system. I am struggling right now to get the file output working. The method specifically that is not working is the addEmployee() method. If I comment that method out the class compiles perfectly, and I am able to select a file through JFilechooser. So I know that part is working fine. However when I attempt to compile with addEmployee in the code, I get compiler errors because it doesn't recognize outStream. I'm not very strong on try catch finally statements, as I just learned them today. I think that I set it up right but I'm not positive.
Help is appreciated.
Compiler errors
EmployeeSystem.java:122: cannot find symbol
symbol : method write(int,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,int,int)
location: class java.io.FileOutputStream
outStream.write(employeeType, employeeID, employeeName, birthDate, startDate, city, state, zip, numberHours, hourlyRate);
^
EmployeeSystem.java:126: cannot find symbol
symbol : variable outStream
location: class EmployeeSystem
outStream.close(); }
^
EmployeeSystem.java:146: cannot find symbol
symbol : method write(int,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,int)
location: class java.io.FileOutputStream
outStream.write(employeeType, employeeID, employeeName, birthDate, startDate, city, state, zip, monthlySalary);
^
EmployeeSystem.java:150: cannot find symbol
symbol : variable outStream
location: class EmployeeSystem
outStream.close(); }
^
4 errors
EmployeeSystem
/**
* EmployeeSystem class works as
* the main controlling system for
* accessing full time and casual employee objects
* CSCE 155 Fall 2008
* Assignment 3
* @author Jared Wiebelhaus
* @version
*/
// import statements
import java.io.*;
import java.util.*;
import javax.swing.*;
public class EmployeeSystem {
// private data members - variables
private int userChoice; // holds the input value for the control statement of 1 - 4
String current = System.getProperty("user.dir");
File mainFile, payroll;
JFileChooser chooser = new JFileChooser(current); // creates a new object of JFileChooser
private int status;
private int employeeType;
private String employeeID;
private String employeeName;
private String birthDate;
private String startDate;
private String city;
private String state;
private String zip;
private int numberHours;
private int hourlyRate;
private int monthlySalary;
/** Constructor
* initialize all private data members to appropriate values
*/
public EmployeeSystem() {
this.userChoice = 0;
} //end constructor
/** This method allows the user to select a file
* which will then be used in the rest of the program
*/
public void getFile() {
status = chooser.showOpenDialog(null);
if (status == JFileChooser.APPROVE_OPTION) {
mainFile = chooser.getSelectedFile();
} else {
System.exit(0);
}
}// end getFile
/** This method gets the input from the user
* using the readInteger() scanner method
*/
public void getInput() {
System.out.println("Please select one of the following options:");
System.out.println(" 1. Search current employee information");
System.out.println(" 2. Add new employee information");
System.out.println(" 3. Run payroll for employees");
System.out.println(" 4. Exit");
//obtain the actual value from the user
userChoice = readInteger();
}
public void runSystem() {
if (userChoice == 1) {
//run directory system
}
else if (userChoice == 2) {
//Reference a method in the EmployeeSystem itself to write data
}
else if (userChoice == 3) {
//Run payroll system and display the name of the created file to the user
}
else if (userChoice == 4) {
System.exit(0);
} else {
System.out.println("Please enter a number 1 - 4");
}
}//end runSystem
/** This method starts an output stream
* and allowes the user to write a new employee to the file
*/
public void addEmployee() {
System.out.println("Please enter the type of employee (enter 1 for casual laborer, 2 for full time):");
employeeType = readInteger();
if (employeeType == 1) {
System.out.println("Please enter the employee ID:");
employeeID = readString();
System.out.println("Please enter the employee name:");
employeeName = readString();
System.out.println("Please enter date of birth:");
birthDate = readString();
System.out.println("Please enter date of employment:");
startDate = readString();
System.out.println("Please enter city");
city = readString();
System.out.println("Please enter state");
state = readString();
System.out.println("Please enter ZIP");
zip = readString();
System.out.println("Please enter number of hours worked per day by employee");
numberHours = readInteger();
System.out.println("Please enter hourly rate of employee (in dollars):");
hourlyRate = readInteger();
try {
FileOutputStream outStream = new FileOutputStream(mainFile);
outStream.write(employeeType, employeeID, employeeName, birthDate, startDate, city, state, zip, numberHours, hourlyRate);
} catch (IOException e) {
//error statement
} finally {
outStream.close(); }
} else if (employeeType == 2) {
System.out.println("Please enter the employee ID:");
employeeID = readString();
System.out.println("Please enter the employee name:");
employeeName = readString();
System.out.println("Please enter date of birth:");
birthDate = readString();
System.out.println("Please enter date of employment:");
startDate = readString();
System.out.println("Please enter city");
city = readString();
System.out.println("Please enter state");
state = readString();
System.out.println("Please enter ZIP");
zip = readString();
System.out.println("Please enter monthly salary of employee (in dollars)");
monthlySalary = readInteger();
try {
FileOutputStream outStream = new FileOutputStream(mainFile);
outStream.write(employeeType, employeeID, employeeName, birthDate, startDate, city, state, zip, monthlySalary);
} catch (IOException e) {
//error statement
} finally {
outStream.close(); }
} else {
System.out.println("Please enter either 1 or 2");
}
}// end addEmployee()
/**
* This method displays a welcome message to the user.
*/
public void displayWelcome() {
/* Display a brief welcome message about the system */
System.out.println("Welcome to the CSE Employee Directory");
System.out.println("You can view employee information, add employees, and run the payroll using this system");
}
/**
* This method displays a goodbye message to the user.
*/
public void displayGoodbye() {
System.out.println("Thank you, Goodbye!");
} // end displayGoodbye
/**
* This method reads an integer and
* returns that integer to the caller of this method.
*/
private int readInteger() {
int temp = 0;
Scanner scanner = new Scanner(System.in);
try {
temp = scanner.nextInt(); // read integer
} catch (InputMismatchException ex) {
System.out.println(ex);
}
return temp;
} // end readInteger
/**
* This method reads in a string and returns that string to the caller of
* this method.
*/
private String readString() {
String userInput = "";
Scanner scanner = new Scanner(System.in);
userInput = scanner.nextLine();
return userInput;
} // end readString
/** This main method creates an object of
* the EmployeeSystem
*/
public static void main(String[] args) throws IOException {
EmployeeSystem mySystem = new EmployeeSystem();
mySystem.displayWelcome();
mySystem.getFile();
mySystem.getInput();
mySystem.runSystem();
mySystem.displayGoodbye();
} // end main
} // end Class PayrollSystem