Question: I'm looking for help with the client/main portion. I have the Employee and UniqueObject classes already done. Any help will be greatly appreciated. Here are
I'm looking for help with the client/main portion. I have the Employee and UniqueObject classes already done. Any help will be greatly appreciated.
Here are the instructions:




I've bolded the area I need help with. Basically if the user selects "1" it prints the employee list (which starts off empty). When the user chooses option "2", the user inputs all relevant info (first name, last name, hourly wage, and years working). That info is added to the employees ArrayList and the "employee.txt" file is updated. Sample output is listed above. Please let me know if you need any further info
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
public class Lab12 {
public static void main(String[] args) {
// Declare new employee list
ArrayList
// Init scanner
Scanner scan = new Scanner(System.in);
// Read in choice until user picks valid choice
int choice = -1;
do {
// Prompt user for choice
System.out.print("Please select from the following options (type number): "
+ "\t1. See the list of all employees " + "\t2. Add new employees ");
try {
choice = scan.nextInt(); // ...keep asking them for an int.
scan.nextLine(); // Clear extra whitespace left by nextInt()
} catch (Exception e) {
scan.next(); // If they don't enter an int, advance past the bad
// input
}
// Inform user that their choice was not valid
if (choice != 1 && choice != 2)
System.out.println("Invalid choice: Please try again... ");
} while (choice != 1 && choice != 2);
// Choice 1: List employees
if (choice == 1) {
// Your code here
} else // Choice 2: Add new employees
{
// Your code here.....
}
}
////////////////////////////////////////////////////////////////////////
// Reads Employees from a given file and stores them into the employees
// list. Returns true upon success, false upon failure.
private static boolean readEmployeesFromFile(String fileName, ArrayList
// Initialize input streams
FileInputStream fis = null;
ObjectInputStream ois = null;
boolean readFail = false;
ArrayList
try {
fis = new FileInputStream(fileName);
ois = new ObjectInputStream(fis);
// Read in employees from file
while (true) {
Employee e = (Employee) ois.readObject();
burritos.add(e);
}
} catch (EOFException e) {
System.out.println(fileName + " file successfully ready.");
} catch (Exception e) {
System.out.println("ERROR: " + e.getMessage());
} finally {
try {
ois.close();
fis.close();
} catch (IOException e) {
System.out.println("ERROR: " + e.getMessage());
}
}
// Return whether there was success or not
if (readFail)
return false;
else
return true; // If we made it here, then the file was read
// successfully. Return true.
}
////////////////////////////////////////////////////////////////////////
// Reads employee list from file and updates the file with any new
// employees. Returns true upon success; false upon failure
private static boolean updateEmployeeFile(String fileName, ArrayList
// Add employees from file into employees ArrayList
readEmployeesFromFile(fileName, employees);
// Initialize output streams
FileOutputStream fos = null;
ObjectOutputStream oos = null;
boolean readFail = false;
try {
fos = new FileOutputStream(fileName);
oos = new ObjectOutputStream(fos);
// Write objects to file
for (Employee e : employees)
oos.writeObject(e);
} catch (IOException e) {
System.out.println("ERROR: " + e.getMessage());
} finally {
try {
// Close files
oos.close();
fos.close();
} catch (IOException e) {
System.out.println("ERROR: " + e.getMessage());
}
}
// Return whether there was success or not
if (readFail)
return false;
else
return true; // If we made it here, then the file was read
// successfully. Return true.
}
}
b) Client This class has the following A static readEmployeesFromFile(String filename, ArrayList
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
