I\'m looking for help with the client/main portion. I have the Employee and Uniq
ID: 3602634 • Letter: I
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 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<Employee> employees = new ArrayList<Employee>();
// 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): "
+ " 1. See the list of all employees " + " 2. 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<Employee> employees) {
// Initialize input streams
FileInputStream fis = null;
ObjectInputStream ois = null;
boolean readFail = false;
ArrayList<Employee> burritos = new ArrayList<Employee>();
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<Employee> employees) {
// 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 employees) method i. o Reads the file, using serialize techniques, and ADDS all employees to the employees ArrayList that is passed in A static updateEmployeeFile(String fileName, ArrayList employees) method ii. o First calls readEmployeesFromFile(String fileName, ArrayList employees) to add the current employees from the file into the employees ArrayList that is passed in Do not erase employees that have been added by user Then, writes the employees in the employees ArrayList (which will include new and old employees) to specified file using serialize techniques oExplanation / Answer
/**********************************Lab12.java***********************************/
import java.io.EOFException;
import java.io.File;
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;
/**
* The Class Lab12.
*/
public class Lab12 {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
// Declare new employee list
ArrayList<Employee> employees = new ArrayList<Employee>();
// File name
String fileName = "Employee.txt";
String ch = "n";
// 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): "
+ " 1. See the list of all employees " + " 2. 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) {
readEmployeesFromFile(fileName, employees);
System.out.println("Current Employee List:");
for (Employee employee : employees) {
System.out.println(employee.getFirstName() + " " + employee.getLastName() + "(#" + employee.getId()
+ ") has been with the company for " + employee.getYears() + " years and makes $"
+ employee.getHourlyRate() + "/hr");
}
} else // Choice 2: Add new employees
{
do {
System.out.println("Please Enter the new employee's info:");
String firstName, lastName;
double hourlyRate;
int years;
Employee employee = new Employee();
System.out.print("First Name:");
firstName = scan.nextLine();
System.out.print("Last Name:");
lastName = scan.nextLine();
System.out.print("Hourly Rate:");
hourlyRate = scan.nextDouble();
System.out.print("Years Working:");
years = scan.nextInt();
employee.setFirstName(firstName);
employee.setLastName(lastName);
employee.setHourlyRate(hourlyRate);
employee.setYears(years);
employee.setId(generateId(employees.size()));
employees.add(employee);
System.out.println("Employee Successfully added. Would you like to add another? (y/n)");
ch = scan.next();
scan.nextLine();
} while (ch.charAt(0) == 'y');
updateEmployeeFile(fileName, employees);
System.out.println("New Employee List:");
for (Employee employee : employees) {
System.out.println(employee.getFirstName() + " " + employee.getLastName() + "(#" + employee.getId()
+ ") has been with the company for " + employee.getYears() + " years and makes $"
+ employee.getHourlyRate() + "/hr");
}
}
}
////////////////////////////////////////////////////////////////////////
// Reads Employees from a given file and stores them into the employees
/**
* Read employees from file.
*
* @param fileName the file name
* @param employees the employees
* @return true, if successful
*/
// list. Returns true upon success, false upon failure.
private static boolean readEmployeesFromFile(String fileName, ArrayList<Employee> employees) {
// Initialize input streams
FileInputStream fis = null;
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
boolean readFail = false;
ArrayList<Employee> burritos = new ArrayList<Employee>();
try {
fis = new FileInputStream(fileName);
ois = new ObjectInputStream(fis);
// Read in employees from file
while (true) {
Employee e = (Employee) ois.readObject();
employees.add(e);
}
} catch (EOFException e) {
System.out.println(fileName + " file successfully ready.");
} catch (Exception e) {
System.out.println("ERROR: " + e.getMessage());
File file = new File(fileName);
try {
file.createNewFile();
oos = new ObjectOutputStream(new FileOutputStream(file));
for (Employee employee : burritos) {
oos.writeObject(employee);
}
} catch (IOException e1) {
}
} finally {
try {
if (ois != null) {
ois.close();
}
if (fis != null) {
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
/**
* Update employee file.
*
* @param fileName the file name
* @param employees the employees
* @return true, if successful
*/
// employees. Returns true upon success; false upon failure
private static boolean updateEmployeeFile(String fileName, ArrayList<Employee> employees) {
// 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.
}
/**
* Generate id.
*
* @param size the size
* @return the string
*/
public static String generateId(int size) {
String id = "";
int temp = size;
if (size == 0) {
temp = 1;
}
while (temp < 4) {
id = id + "0";
temp++;
}
return id + size;
}
}
/*****************************Run1*******************/
Please select from the following options (type number):
1. See the list of all employees
2. Add new employees
2
Please Enter the new employee's info:
First Name:Bill
Last Name:Gates
Hourly Rate:500.50
Years Working:29
Employee Successfully added. Would you like to add another? (y/n)
y
Please Enter the new employee's info:
First Name:Jonny
Last Name:Appleseed
Hourly Rate:.99
Years Working:128
Employee Successfully added. Would you like to add another? (y/n)
n
ERROR: Employee.txt (The system cannot find the file specified)
New Employee List:
Bill Gates(#0000) has been with the company for 29 and makes $500.5/hr
Jonny Appleseed(#0001) has been with the company for 128 and makes $0.99/hr
/****************************Run2****************************************************/
Please select from the following options (type number):
1. See the list of all employees
2. Add new employees
1
Employee.txt file successfully ready.
Current Employee List:
Bill Gates(#0000) has been with the company for 29 years and makes $500.5/hr
Jonny Appleseed(#0001) has been with the company for 128 years and makes $0.99/hr
/************************************Run3*******************************************************/
Please select from the following options (type number):
1. See the list of all employees
2. Add new employees
2
Please Enter the new employee's info:
First Name:LeBorn
Last Name:James
Hourly Rate:999.99
Years Working:14
Employee Successfully added. Would you like to add another? (y/n)
n
Employee.txt file successfully ready.
New Employee List:
LeBorn James(#0000) has been with the company for 14 years and makes $999.99/hr
Bill Gates(#0000) has been with the company for 29 years and makes $500.5/hr
Jonny Appleseed(#0001) has been with the company for 128 years and makes $0.99/hr
Thanks a lot. Please let me know if you have any doubt.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.