Question: Using Java program ========== Employee class ========== import java.text.NumberFormat; public class Employee { private String name; private double salary; public Employee() { } public Employee(String

Using Java program Using Java program ========== Employee class ========== import java.text.NumberFormat; public class Employee ========== Employee class ==========

import java.text.NumberFormat;

public class Employee { private String name; private double salary;

public Employee() { }

public Employee(String name, double salary) { this.name = name; this.salary = salary; } public String toStringF() { return name + "|" + salary; }

public String toString() { NumberFormat nf = NumberFormat.getCurrencyInstance(); return name + " has a salary of " + nf.format(salary); }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public double getSalary() { return salary; }

public void setSalary(double salary) { this.salary = salary; } } ========== EmployeeDriver class ==========

import java.awt.FileDialog; import java.awt.Frame; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Scanner; import java.util.StringTokenizer;

public class EmployeeDriver {

public static void main(String[] args) throws Exception {

int choice = 0; ArrayList emp = new ArrayList();

while (choice >= 0) { choice = menu(); if (choice == 1) { inputData(emp); } else if (choice == 2) { emp = readData(); } else if (choice == 3) { printData(emp); } else if (choice == 4) { saveData(emp); }else if (choice ==5) emp.clear(); else if (choice == 6) { System.out.println("Bye!!!!!"); System.exit(0); } } // end while }

public static int menu() { Scanner keyboard = new Scanner(System.in); System.out.println(" Choice:"); System.out.println(" 1. Input employees from keyboard"); System.out.println(" 2. Read employees from file"); System.out.println(" 3. Print employees"); System.out.println(" 4. Save employee data"); System.out.println(" 5. Clear the data"); System.out.println(" 6. Quit!! "); System.out.println(" CHOICE:"); int value = keyboard.nextInt(); return value;

}

public static ArrayList inputData(ArrayList emp ) { Scanner keyboard = new Scanner(System.in); boolean more = true; while (more) { System.out.println("Employee name: "); String who = keyboard.nextLine(); System.out.println("Salary: "); double amt = keyboard.nextDouble(); Employee em = new Employee(who, amt); emp.add(em);

System.out.println("More (true/false)"); more = keyboard.nextBoolean(); keyboard.nextLine(); } return emp; }

public static void printData(ArrayList e) { System.out.println("Employees:"); for (int i = 0; i

public static ArrayList readData() throws IOException{ ArrayList emp; emp = new ArrayList(); BufferedReader bf = openRead();

String line = bf.readLine(); while (line !=null) { StringTokenizer st = new StringTokenizer(line, "|"); // Read a record from the file. String who = st.nextToken(); double amt = Double.parseDouble(st.nextToken().toString()); Employee e = new Employee(who, amt); emp.add(e); line = bf.readLine(); }

return emp;

}

public static BufferedReader openRead() throws IOException { Frame f = new Frame(); // decide from where to read the file FileDialog foBox = new FileDialog(f, "Pick location for reading your file", FileDialog.LOAD); System.out.println( "The dialog box will appear behind Eclipse. " + " Choose where you would like to read from."); foBox.setVisible(true); // get the absolute path to the file String foName = foBox.getFile(); String dirPath = foBox.getDirectory();

// create a file instance for the absolute path File inFile = new File(dirPath + foName); BufferedReader in = new BufferedReader(new FileReader(inFile)); return in;

}

public static PrintWriter openWrite() throws Exception { Frame f = new Frame(); // decide from where to read the file FileDialog foBox = new FileDialog(f, "Pick location for writing your file", FileDialog.SAVE); System.out.println("The dialog box will appear behind Eclipse. " + " Choose where you would like to write your data."); foBox.setVisible(true); // get the absolute path to the file String foName = foBox.getFile(); String dirPath = foBox.getDirectory();

// create a file instance for the absolute path File outFile = new File(dirPath + foName); PrintWriter out = new PrintWriter(outFile);

return out; }

public static void saveData(ArrayList e) throws Exception { PrintWriter employeeFile = openWrite();

// Get each employee's data and write it to the file. for (int counter = 0; counter

System.out.println("The data was saved to a file"); }

}

*******************************************************************

SAMPLE OUTPUT EMPLOYEEDRIVER Choice: 1. Input employees from keyboard 2. Read employees from file 3. Print employees 4. Save employee data 5. Clear the data 6. Quit!!

CHOICE: 1 Employee name: ---note - I just hit return here ---- Error: You did not enter a name Employee name: ----note - I typed a number of spaces here ---- Error: You did not enter a name Employee name: Mike Salary: -200 Error: Negative salary: -200.0 Employee name: Joe Salary: 200 More (true/false) true Employee name: Sam Salary: -200 Error: Negative salary: -200.0 Employee name: Sam Salary: 400 More (true/false) true Employee name: Doug Salary: 100 More (true/false) false

Choice: 1. Input employees from keyboard 2. Read employees from file 3. Print employees 4. Save employee data 5. Clear the data 6. Quit!!

CHOICE: 3 Employees: Joe has a salary of $200.00 Sam has a salary of $400.00 Doug has a salary of $100.00

Choice: 1. Input employees from keyboard 2. Read employees from file 3. Print employees 4. Save employee data 5. Clear the data 6. Quit!!

-----Note when I choose 4 to save, it gives me a FileDialog asking the location which I fill in and complete. I then end the program. I then start the program again, choose 2, and point to that location It returns the data from above. When I open the file, it looks like: Joe|200.0 Sam|400.0 Doug|100.0

**********************************************************************

Problem #2 Employee Exceptions Look at the Employee and EmployeeDriver programs. Convert it over to try/catch/finally statements instead of just throwing the exceptions. To get started. remove the throws IOException from the methods that have this. There are three of these in the driver. You will now see red Xs occur since IOExceptions are checked exceptions (ones that MUST be checked). But Eclipse will help you fix these. Go to the red mark in the readData() method. If you hover over the red mark to the left of one of the exception lines, you will see 104 Cones Unhandled exception type FileNotFoundException}w Scanner(inFile); na Annal tanlas. FileNotFoundException is a checked exception that must be handled. If you click on the red X. you will see the following 1103 System.exit(0); 100 dies Unhandled exception type FleNotFoundException Scanner(inFile) 106 107 18 109 // Read a record en new ArrayList

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!