/*Using Java write a program that reads in a css file and parses each line into
ID: 3825541 • Letter: #
Question
/*Using Java write a program that reads in a css file and parses each line into a string array using string split.
You will create an employee scheduler program. From the array read in the employee's first name, last name,
skill, wage, and ID number. These items will be needed to populate an Employee object.
The Employee object will have a string for the first name, a string for the last name, a string for skill,
a double for wage, and an integer for employee ID. Create another class called the EmplyeeScheduler.
The scheduler class will allow a user to schedule the list of employees (provided from a file) over a 40-hour workweek.
The output from this program will be the 40-hour employee schedule.*/
The CSS file would be uploaded from the user and not included in the java program
import java.io.*;
import java.util.*;
public class EmployeeScheduler
{
public static void main(String[] args) throws IOException, FileNotFoundException
{
Scanner obj =new Scanner(System.in);
System.out.println("Enter file name");
String fileName = obj.next();
BufferedReader br =new BufferedReader(new FileReader(new File(fileName)));
String str;
while((str=br.readLine())!=null){
String arr[] = str.trim().split(" ");
System.out.println(Arrays.toString(arr));
}
}
}
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
######### Employee.java ######
public class Employee{
// instance variables
private String firstName;
private String lastName;
private String skill;
private double wage;
private int id;
private int hoursWeek;
// constructor
public Employee(String firstName, String lastName, String skill, double wage, int id) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.skill = skill;
this.wage = wage;
this.id = id;
}
// getters and setters
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getSkill() {
return skill;
}
public double getWage() {
return wage;
}
public int getId() {
return id;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setSkill(String skill) {
this.skill = skill;
}
public void setWage(double wage) {
this.wage = wage;
}
public void setId(int id) {
this.id = id;
}
public void setHoursWeek(int h){
hoursWeek = h;
}
public int getHoursWeek(){
return hoursWeek;
}
}
###########
import java.io.*;
import java.util.*;
public class EmployeeScheduler
{
public static void scheduleEmployees(Employee[] emps){
// setting 40 hours week work
for(Employee e : emps)
e.setHoursWeek(40);
}
public static void main(String[] args) throws IOException, FileNotFoundException
{
Scanner obj =new Scanner(System.in);
System.out.println("Enter file name");
String fileName = obj.next();
BufferedReader br =new BufferedReader(new FileReader(new File(fileName)));
String str;
ArrayList<Employee> empArray = new ArrayList<>();
while((str=br.readLine())!=null){
String arr[] = str.trim().split(","); // comma saparated data
// creating employee object
Employee emp = new Employee(arr[0], arr[1], arr[2],
Double.parseDouble(arr[3]), Integer.parseInt(arr[4]));
empArray.add(emp); // adding in array
System.out.println(Arrays.toString(arr));
}
br.close();
obj.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.