Skip to content

Instantly share code, notes, and snippets.

@mamiwinsfall93
Created January 18, 2020 17:17
Show Gist options
  • Save mamiwinsfall93/0bf900d0ae06ead8ead7651b1c460582 to your computer and use it in GitHub Desktop.
Save mamiwinsfall93/0bf900d0ae06ead8ead7651b1c460582 to your computer and use it in GitHub Desktop.
Calculates salary for different types of employees, and displays their information
/** Filename: Endingclass.java
* Author: Ndatta Fall
*
* Purpose of Program: Calculates salary for different types of employees, and displays their information
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Endingclass {
private static void display(String holder, Object[] employee, int len) {//method to display employee details
System.out.println(holder);
for(int x = 0; x < len; x++) {
if(employee[x] instanceof Employee) {
System.out.println(employee[x] + "\n");
System.out.println("------------------------\n");
}
else if(employee[x] instanceof Salesman) {
System.out.println(employee[x] + "\n");
System.out.println("------------------------\n");
}
else if(employee[x] instanceof Executive) {
System.out.println(employee[x] + "\n");
System.out.println("------------------------\n");
}
}
}
public static void main(String[] args) throws FileNotFoundException {
Object[] employee2014 = new Object[10];//array to store 2014's employees
Object[] employee2015 = new Object[10];//array to store 2015's employee
if(args.length>0){
File myFile = new File(args[0]);
Scanner inFile = new Scanner(myFile);//opening a file using scanner object
//Scanner inFile= new Scanner(new FileReader(args[0]));
String oneLine;
int x = 0, y = 0;
while (inFile.hasNextLine()) {
oneLine = inFile.nextLine();//reading line by line
String[] fileArray = oneLine.split("\\s+");//splitting with spaces
Object employee = null;
if(fileArray[1].equals("Employee")) {
employee = new Employee(fileArray[2], Integer.parseInt(fileArray[3]));
}
else if(fileArray[1].equals("Salesman")) {
employee = new Salesman(fileArray[2], Integer.parseInt(fileArray[3]), Integer.parseInt(fileArray[4]));
}
else if(fileArray[1].equals("Executive")) {
employee = new Executive(fileArray[2], Integer.parseInt(fileArray[3]), Integer.parseInt(fileArray[4]));
}
else
System.out.println(" error , unidentified " + fileArray[2]);
if(employee != null) {
if(fileArray[0].equals("2014"))
employee2014[x++] = employee;
else if(fileArray[0].equals("2015"))
employee2015[y++] = employee;
}
}
inFile.close();//closing the file
display("Employees of 2014", employee2014, x);
display("Employees of 2015", employee2015, y);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment