Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can some one help me with this i want to Update Lab 8 by using Array List instead of arrays. my code for lab 8

can some one help me with this

i want to Update Lab 8 by using Array List instead of arrays.

my code for lab 8 is :

employees.txt

r 1020 John Doe doe@test.com 123456 85000c 1100 Tom William tom@test.com 321456 25 40r 1824 Matt James matt@test.com 213546 78550

Explanation:

Person.java

package Lab8;import java.util.Scanner;/** * Person class (abstract) * Instance variables: firstName(String), lastName(String), email(String), phoneNumber (long). * Methods: * readInfo(): accepts a Scanner object, returns nothing. Reads personal information. * printInfo(): abstract method. Definition should be provided in the classes that inherit this class. */public abstract class Person { // Declare variable as private members // Therefore we require getters and setters for the variables; String firstName; String lastname; String email; long phoneNumber; // Constructor public Person() { firstName = null; lastname = null; email = null; phoneNumber = 0; } // Parameterised constructor to set the values. public Person(String first, String last, String email, long phone) { firstName = first; lastname = last; this.email = email; phoneNumber = phone; } /** * Function to read information for the object * * @param sc Scanner object */ public void readInfo(Scanner sc) { System.out.print("Enter First Name: "); firstName = (sc.nextLine()); System.out.print("Enter Last Name: "); lastname = sc.nextLine(); System.out.print("Enter Email : "); email = sc.nextLine(); System.out.print("Enter Phone Number: "); phoneNumber = (Long.parseLong(sc.nextLine().trim())); } /** * abstract methods to print the information */ public abstract void printInfo();}

Employee.java

package Lab8;import java.util.Scanner;/** * Employee class (extends Person) * Instance variables: employeeNumber (int) * Constructor: parameterized constructor that initializes an employee with employee number and all personal properties. As you have a parameterized constructor for Person class, invoke that to set personal properties. * Methods: * readInfo():accepts a Scanner object, returns nothing. Reads all employee information. For reading personal information, you need to invoke readInfo() method of the super class. * printInfo(): accepts nothing, returns nothing. This method prints details of an employee using formatted output (use printf). */public class Employee extends Person { private int employeeNumber; public Employee(String first, String last, String email, long phone, int emplNumber) { super(first, last, email, phone); this.employeeNumber = (emplNumber); } public Employee() { super(); employeeNumber = (0); } @Override public void readInfo(Scanner sc) { System.out.print("Enter Employee Number: "); employeeNumber = (Integer.parseInt(sc.nextLine().trim())); super.readInfo(sc); } @Override public void printInfo() { String fullName = (firstName + " " + lastname); System.out.printf("%10s |", employeeNumber); System.out.printf(" %15s | %15s | %15s |", fullName, email, phoneNumber + ""); }}

Regular.java

package Lab8;import java.util.Scanner;/** * Regular class (extends Employee) * Instance variables: salary (double) * Methods: * 1. readInfo(): accepts a Scanner object, returns nothing. Overrides the readInfo() method. In the overridden definition, make a call to the readInfo() method of the parent class. Then, reads annual salary from the user, converts it to monthly salary and store it in salary instance variable. * 2. printInfo(): accepts nothing, returns nothing. Overrides the printInfo() method. In the overridden definition, make a call to the printInfo() of the parent class. Then, prints salary info (formatted output). */public class Regular extends Employee { double salary; public Regular(String first, String last, String employee, long phone, int emplNumber, double salary) { super(first, last, employee, phone, emplNumber); this.salary = (salary); } public Regular() { super(); } @Override public void readInfo(Scanner sc) { super.readInfo(sc); System.out.print("Enter annual Salary: "); salary = (Double.parseDouble(sc.nextLine().trim())); } @Override public void printInfo() { super.printInfo(); System.out.printf(" %15.2f | ", (salary / 12)); }}

Contractor.java

package Lab8;import java.util.Scanner;/** * Contractor class (extends Employee) * Instance variables: hourlyRate (double), numHours (double) * Methods: * 1. readInfo(): accepts a Scanner object, returns nothing. Overrides the readInfo() method. Make a call to the readInfo() method of the parent class. Then, reads hourly rate and number of hours worked. * 2.printInfo(): accepts nothing, returns nothing. Overrides the printInfo() method. Make a call to the printInfo() of the parent class. Then, prints salary, which is the product of hourly rate and the number of hours worked (formatted output). */public class Contractor extends Employee { private double hourlyRate; private double numHours; // Set values using parameterised constructor public Contractor(String first, String last, String email, long phone, int emplNum, double numHrs, double rate) { super(first, last, email, phone, emplNum); hourlyRate = (rate); numHours = (numHrs); } public Contractor() { super(); } @Override public void readInfo(Scanner sc) { super.readInfo(sc); System.out.print("Enter Hourly Rate: "); hourlyRate = (Double.parseDouble(sc.nextLine().trim())); System.out.print("Enter Number of hours: "); numHours = (Double.parseDouble(sc.nextLine().trim())); } @Override public void printInfo() { super.printInfo(); System.out.printf(" %15.2f | ", (numHours * hourlyRate)); }}

Store.java

package Lab8;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.util.InputMismatchException;import java.util.Scanner;/** * Store class * Instance variables: an array of Employee named employees * Constructor: parameterized constructor that creates the array of employees with the given size (this size will be read in main(), and will be sent here when creating the Store object) * Methods: * 1. readDetails(): accepts nothing, returns nothing. In a for loop, read details of all employees. First, read the type of the employee. Based on the type of the employee, corresponding array object needs to be created (Polymorphism). Then, call readInfo() method. * 2. printDetails(): accepts nothing, returns nothing. In a for loop, call printInfo() to print details of all employees. * 3. printLine(): static method that prints a line using "=" * 4. printTitle(): static method that prints the title of the output. This method gets the name of the store as a parameter, which will be used in the formatted print statement. printLine() method will be called from this method to print lines. */public class Store { // count variable to keep tract of number of employees public static int count; // Create array of employees which get its size by user private Employee[] employees = {}; // Constructor to define number of employees public Store(int number) { try { employees = new Employee[number]; }catch (NegativeArraySizeException ex) { System.out.println("*****Array Size cannot be Negative*****"); } count = 0; } /** * Methods to print the line formed of = */ public static void printLine() { System.out.println("===================================================================================="); } /** * Method to print the title * * @param name */ public static void printTitle(String name) { try { if (count == 0) throw new ArrayIndexOutOfBoundsException(); } catch (ArrayIndexOutOfBoundsException ex) { System.out.println("***** No elements in the array *****"); return; } String name1 = name + " Store Management System"; printLine(); System.out.printf("%50s ", name1); printLine(); System.out.printf("%10s | %15s | %15s | %15s | %15s | ", "Emp#", "Name", "Email", "Phone", "Salary"); printLine(); } /** * Function to read the details of employees from the employees.txt file * as given in the question */ public void readDetailsFromFile() { Scanner fileRead = null; boolean flag = false; if(count try { // Change the file path according to you fileRead = new Scanner(new FileInputStream("employees.txt")); // read from the file till we have data in file while (fileRead.hasNext()) { // read type char type = fileRead.next().charAt(0); // read employee num int empNo = Integer.parseInt(fileRead.next()); // Read first name last name and email String fName = fileRead.next(); String lName = fileRead.next(); String email = fileRead.next(); // read phone number long phno = Long.parseLong(fileRead.next()); // varialbes for sal , num, hourly rate double hrly, num, sal; // if employee is regualr employee if (type == 'r') { sal = Double.parseDouble(fileRead.next()); // create object of Regular employee employees[count] = new Regular(fName, lName, email, phno, empNo, sal); // make mfalg true that employee is added flag = true; // if employee is a contractor. } else if (type == 'c') { // read the hourly rate hrly = Double.parseDouble(fileRead.next()); // read the number of hours num = Double.parseDouble(fileRead.next()); // Create object of contractor employee employees[count] = new Contractor(fName, lName, email, phno, empNo, num, hrly); flag = true; } // if employee added successfully then increase employee count. if (flag) { flag = false; count++; } } // catch file not found exxception } catch (FileNotFoundException e) { System.out.println("***** There is no such file *****"); } } else { // if array is full then tell System.out.println("***** Array us full. cannot add more elements *****"); } } /** * Read Employee details from user */ public void readDetails() { Scanner sc = new Scanner(System.in); boolean flag = false; // if user tries to enter more users then employe number then print error try { if (count // prompt for type of employee System.out.println("Enter Details of Employee " + (count + 1)); System.out.println("1. Regular"); System.out.println("2. Contractor"); System.out.print("Enter the type of employee: "); int type = sc.nextInt(); sc.nextLine(); switch (type) { case 1 -> { // read Regular employee employees[count] = new Regular(); employees[count].readInfo(sc); flag = true; } case 2 -> { // read the contractor employee details employees[count] = new Contractor(); employees[count].readInfo(sc); flag = true; } default -> System.out.println("Invalid option.... please try again"); } if(flag) count++; } else { // if array is full tell the user System.out.println("***** Array us full. cannot add more elements *****"); } } catch (ArrayIndexOutOfBoundsException ex) { // if array is full System.out.println("***** Array us full. cannot add more elements *****"); } catch (InputMismatchException | NumberFormatException inputException) { System.out.println("*****Input Mismatch Exception while reading Selection of process*****"); String _A = sc.nextLine(); } } /** * Print the details */ public void printDetails() // print the details { try { for (int i = 0; i catch (ArrayIndexOutOfBoundsException ex) { System.out.println("***** No elements in the array *****"); } }}

Lab8.java

package Lab8;import java.util.InputMismatchException;import java.util.Scanner;/** * Driver class */public class Lab8 { public static void main(String[] args) { // Create object of scanner to accept inputs Scanner sc = new Scanner(System.in); // Prompt for number of employees System.out.print("Enter the name of the Store: "); String name = sc.nextLine(); int num = 0; do { try { System.out.print("Enter the number of Employees: "); num = sc.nextInt(); } catch (InputMismatchException | NumberFormatException inputException) { System.out.println("*****Input Mismatch Exception while reading the number of Employees*****"); String _A = sc.nextLine(); continue; // continue to run code } } while (num 0); // Create object of store. Store store = new Store(num); int choice = 0; // Read info from the user do { do { try { System.out.println("1. Read Employee Details from the user"); System.out.println("2. Read Details from the file"); System.out.println("3. Print Employee Details"); System.out.println("4. Quit"); System.out.print("Enter Your option: "); choice = sc.nextInt(); switch (choice) { case 1 -> store.readDetails(); case 2 -> { store.readDetailsFromFile(); } case 3 -> { Store.printTitle(name); store.printDetails(); } case 4 -> { System.out.println("Goodbye... Have a nice day!"); System.exit(0); } default -> System.out.println("Invalid option... please try again..."); } } catch (InputMismatchException | NumberFormatException inputException) { System.out.println("*****Input Mismatch Exception while reading Selection of process*****"); String _A = sc.nextLine(); } } while (choice != 1 && choice != 2 && choice != 3 && choice != 4); } while (choice != 4); }}

and my out put should look like

image text in transcribed
\f

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Unity From Zero To Proficiency Beginner A Step By Step Guide To Coding Your First Game

Authors: Patrick Felicia

1st Edition

1091872023, 978-1091872028

More Books

Students also viewed these Programming questions