0% found this document useful (0 votes)
5 views17 pages

New Explanation

The document contains a Java implementation of a company management system that manages employees, including developers, testers, and team leaders. It provides functionalities for loading, adding, searching, and sorting employees, as well as saving employee data to files. The system uses a file I/O interface to read and write employee information in both text and binary formats.

Uploaded by

yoichikito
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views17 pages

New Explanation

The document contains a Java implementation of a company management system that manages employees, including developers, testers, and team leaders. It provides functionalities for loading, adding, searching, and sorting employees, as well as saving employee data to files. The system uses a file I/O interface to read and write employee information in both text and binary formats.

Uploaded by

yoichikito
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

package controller;

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class CompanyManagement {

private List<Employee> empList;


private double minSalary;

public CompanyManagement() {
[Link]();
if (empList == null) {
empList = new ArrayList<>();
}
}

private boolean load() {


try {
IFileReadWrite file = new EmployeeFileText();
empList = [Link]();
return true;
} catch (Exception ex) {
return false;
}
}

public List<Employee> getEmpList() {


return empList;
}

public boolean addEmplpoyee(Employee emp) {


if (emp == null) {
return false;
}
[Link](emp);
return true;
}

public Employee getEmployee(String Code) {


for (Employee emp : empList) {
if ([Link]().equalsIgnoreCase(Code)) {
return emp;
}
}
return null;
}

public boolean isExistCode(String code) {


return [Link](code) != null;
}
public boolean isExistTeamLeader(String name) {
for (Employee emp : empList) {
if (!(emp instanceof TeamLeader)) {
continue;
}
TeamLeader team = (TeamLeader) emp;
if ([Link]().equals(name)) {
return true;
}
}
return false;
}

public List<Employee> searchByName(String name) {


List<Employee> result = new ArrayList<>();
for (Employee emp : empList) {
if ([Link]().equalsIgnoreCase(name)) {
[Link](emp);
}
}
return result;
}

public Employee searchByCode(String ID) {


for (Employee emp : empList) {
if ([Link]().equalsIgnoreCase(ID)) {
return emp;
}
}
return null;
}

public List<Employee> searchBySalary(double salary) {


List<Employee> result = new ArrayList<>();
for (Employee emp : empList) {
if ([Link]() >= minSalary) {
[Link](emp);
}
}
return result;
}

public List<Employee> searchDeveloperByLanguage(String lang) {


List<Employee> result = new ArrayList<>();
for (Employee emp : empList) {
if (emp instanceof Developer) {
Developer dev = (Developer) emp;
if ([Link]().contains(lang)) {
[Link](dev);
}
}
}
return result;
}

public List<Employee> sortBySalaryName() {


List<Employee> result = new ArrayList<>(empList);
Comparator<Employee> com = (o1, o2) -> {
int c = [Link]([Link](), [Link]()); // Descending
salary
if (c == 0) {
c = [Link]().compareTo([Link]()); // Ascending name
}
return c;
};
[Link](result, com);
return result;
}

public Employee getHighestPaidTester() {


return [Link]()
.filter(e -> e instanceof Tester)
.max([Link](Employee::getSalary))
.orElse(null);
}

public boolean save() {


try {
IFileReadWrite file = new EmployeeFileText();
//IFileReadWrite file = new EmployeeFileBinary();
return [Link](empList);
} catch (Exception ex) {
return false;
}
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package fileio;

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

/**
*
* @author Admin
*/
public class EmployeeBinary implements IFileReadWrite<Employee> {

private String FILE_NAME = "src/fileio/[Link]";

@Override
public List<Employee> read() throws Exception {
List<Employee> list = null;

FileInputStream fileIn = null;


ObjectInputStream objectIn = null;
try {
fileIn = new FileInputStream(FILE_NAME);
objectIn = new ObjectInputStream(fileIn);

Object obj = [Link]();


if (obj != null && obj instanceof ArrayList) {
list = (ArrayList<Employee>) obj;
}
} catch (Exception ex) {
throw ex;
} finally {
if (objectIn != null) {
[Link]();
}
if (fileIn != null) {
[Link]();
}
}
return list;
}

@Override
public boolean write(List<Employee> list) throws Exception {
FileOutputStream fileOut = null;
ObjectOutputStream objectOut = null;
try {
fileOut = new FileOutputStream(FILE_NAME);
objectOut = new ObjectOutputStream(fileOut);

[Link](list);
} catch (IOException ex) {
throw ex;
} finally {
if (objectOut != null) {
[Link]();
}
if (fileOut != null) {
[Link]();
}
}
return true;
}
}
package fileio;

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class EmployeeFileText implements IFileReadWrite<Employee> {

private final String FILE_NAME = "src/fileio/[Link]";


private Employee emp;

@Override
public List<Employee> read() throws Exception {
List<Employee> list = new ArrayList<>();
File f = null;
FileInputStream file = null;
BufferedReader read = null;
try {
f = new File(FILE_NAME);
String fullpath = [Link]();
file = new FileInputStream(fullpath);
read = new BufferedReader(new InputStreamReader(file));
String line;

while ((line = [Link]()) != null) {


if ([Link]().isEmpty()) {
continue;
}
Employee emp = convertToEmployee(line);
if (emp != null) {
[Link](emp);
}
}
} catch (Exception ex) {
throw ex;
} finally {
if (read != null) {
[Link]();
}
if (file != null) {
[Link]();
}
}
return list;
}

@Override
public boolean write(List<Employee> list) throws Exception {
File f = null;
FileOutputStream file = null;
BufferedWriter write = null;
try {
f = new File(FILE_NAME);
String fullpath = [Link]();
file = new FileOutputStream(fullpath);
write = new BufferedWriter(new OutputStreamWriter(file));

for (Employee emp : list) {


[Link](convertToString(emp));
[Link]();
}
} catch (IOException ex) {
throw ex;
} finally {
if (write != null) {
[Link]();
}
if (file != null) {
[Link]();
}
}
return true;
}

private String convertToString(Employee emp) {


if (emp == null) {
return "";
}
String str = "";

if (emp instanceof Tester) {


str = [Link]("%s_%s_%s_%f_%f_%s", "Tester",
[Link](), [Link](), [Link](),
((Tester) emp).getBonusRate(), ((Tester) emp).getType());
} else if (emp instanceof TeamLeader) {
str = [Link]("%s_%s_%s_%2f_%2f_%s_%s", "TeamLeader",
[Link](), [Link](), [Link](),
((TeamLeader) emp).getBonusRate(),
((TeamLeader) emp).getTeamName(), ((TeamLeader)
emp).getExpYear());
} else if (emp instanceof Developer) {
str = [Link]("%s_%s_%s_%2f_%s_%s", "Developer",
[Link](), [Link](), [Link](),
((Developer) emp).getTeamName(), ((Developer)
emp).getExpYear());
}
return str;
}

private Employee convertToEmployee(String s) {


if (s == null || [Link]()) {
return null;
}
String[] split = [Link]("_");
if ("Tester".equalsIgnoreCase(split[0].trim())) {
String code = split[1].trim();
String name = split[2].trim();
double baseSal = [Link](split[3].trim());
double rate = [Link](split[4].trim());
String type = split[5].trim();
emp = new Tester(code, name, baseSal, rate, type);
} else if ("Developer".equalsIgnoreCase(split[0].trim())) {
String code = split[1].trim();
String name = split[2].trim();
double baseSal = [Link](split[3].trim());
String teamName = split[4].trim();
int expYear = [Link](split[5].trim());

List<String> programmingLanguages = new ArrayList<>();

if ([Link] > 6) {
programmingLanguages = [Link](split[6].trim().split(",\\s*"));
}
emp = new Developer(code, name, baseSal, teamName, programmingLanguages,
expYear);

} else if ("TeamLeader".equalsIgnoreCase(split[0].trim())) {
String code = split[1].trim();
String name = split[2].trim();
double baseSal = [Link](split[3].trim());
double bonusRate = [Link](split[4].trim());
String teamName = split[5].trim();
List<String> programmingLanguages =
[Link](split[6].trim().split(",\\s*"));
int expYear = [Link](split[7].trim());

emp = new TeamLeader(code, name, baseSal, bonusRate, teamName,


programmingLanguages, expYear);
}
return emp;
}
}
package fileio;

import [Link];

public interface IFileReadWrite<E> {


List<E> read()throws Exception;
boolean write(List<E> list)throws Exception;
}
package model;

import [Link];

public class Developer extends Employee {

protected String teamName;


protected List<String> programmingLanguages;
protected int expYear;
double getSalary;

public Developer(String empCode, String empName, double baseSal,


String teamName, List<String> programmingLanguages, int expYear) {
super(empCode, empName, baseSal);
[Link] = teamName;
[Link] = programmingLanguages;
[Link] = expYear;
}

public String getTeamName() {


return teamName;
}

public void setTeamName(String teamName) {


[Link] = teamName;
}

public List<String> getProgrammingLanguages() {


return programmingLanguages;
}
public void setProgrammingLanguages(List<String> programmingLanguages) {
[Link] = programmingLanguages;
}

public int getExpYear() {


return expYear;
}

public void setExpYear(int expYear) {


[Link] = expYear;
}

@Override
public double getSalary() {
double salary = baseSal;
if (expYear >= 10) {
salary *= expYear * 2000000;
} else if (expYear >= 3) {
salary += expYear * 1000000;
}
return salary;
}

/**
*
* @return
*/
@Override
public String toString() {
String s = [Link]("%s_%s_%d", [Link](), teamName, expYear);
return s;
}
}
package model;

import [Link];

public abstract class Employee implements Serializable {

protected String empCode;


protected String empName;
protected double baseSal;

public abstract double getSalary();

public Employee(String empCode, String empName, double baseSal) {


[Link] = empCode;
[Link] = empName;
[Link] = baseSal;
}

public String getEmpCode() {


return empCode;
}

public void setEmpCode(String empCode) {


[Link] = empCode;
}
public String getEmpName() {
return empName;
}

public void setEmpName(String empName) {


[Link] = empName;
}

public double getBaseSal() {


return baseSal;
}

public void setBaseSal(double baseSal) {


[Link] = baseSal;
}

@Override
public String toString() {
String s = [Link]("%S %S %.2f", empCode, empName, baseSal);
return s;
}

}
package model;

import [Link];

public class TeamLeader extends Developer {

protected double bonusRate;

public TeamLeader(String empCode, String empName, double baseSal, double


bonusRate, String teamName, List<String> programingLanguages, int expYear) {
super(empCode, empName, baseSal, teamName, programingLanguages, expYear);
[Link] = bonusRate;
[Link](teamName);
}

public double getBonusRate() {


return bonusRate;
}

public void setBonusRate(double bonusRate) {


[Link] = bonusRate;
}

@Override
public double getSalary() {
double salary = [Link]();
return salary *= (1 + bonusRate);
}

}
package model;

public class Tester extends Employee {

public double bonusRate;


public String type;
public Tester(String empCode, String empName, double baseSal, double bonusRate,
String type) {
super(empCode, empName, baseSal);
[Link] = bonusRate;
[Link] = type;
}

public double getBonusRate() {


return bonusRate;
}

public void setBonusRate(double bonusRate) {


[Link] = bonusRate;
}

public String getType() {


return type;
}

public void setType(String type) {


[Link] = type;
}

@Override
public double getSalary() {
double salary = baseSal + bonusRate * baseSal;
return salary;
}
}
package utilities;
import [Link];

public class Inputter {


public static Scanner sc = new Scanner([Link]);

// get an int between min and max


public static int inputInt(String msg, int min, int max) {
if (min > max) {
int t = min;
min = max;
max = t;
}
int data;
do {
[Link](msg);
data = [Link]([Link]());
} while (data < min || data > max);
return data;
}
public static double inputInt(String msg) {
[Link](msg);
int data = [Link]([Link]().trim());
return data;
}
public static double inputDouble(String msg) {
[Link](msg);
double data = [Link]([Link]().trim());
return data;
}

// get a string with no condition


public static String inputStr(String msg) {
[Link](msg);
String data = [Link]().trim();
return data;
}

// get a non-blank string


public static String inputNonBlankStr(String msg) {
String data;
do {
[Link](msg);
data = [Link]().trim();
} while ([Link]() == 0);
return data;
}

// get a string following a pattern


public static String inputPattern(String msg, String pattern) {
String data;
do {
[Link](msg);
data = [Link]().trim();
} while (![Link](pattern));
return data;
}

public static String inputString(String msg){


[Link](msg);
return [Link]().trim();
}
}
// Inputter class
package viewer;

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class Main {

CompanyManagement cm = new CompanyManagement();

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


// Menu options
String[] options = {"Show the Employee list",
"Add Employee", "Update Employee ",
"Search Employee", "Save",
"Sort Employees", "Exit"};
Main main = new Main();

int choice = 0;
[Link](
"Note: \nAll employee's salary based on the actual salary after
multiply with the bonus and casted into integer!!!");
do {
[Link]("\nCompany Employee Management Program");
choice = [Link](options); //show Menu options
switch (choice) {
case 1:
[Link]();
break;
case 2:
[Link]();
break;
case 3:
[Link]();
break;
case 4:
[Link]();
break;
case 5:
[Link]();
break;
case 6:
[Link]();
break;
default:
[Link]("Good bye!");
[Link]();
}
} while (choice > 0 && choice < [Link]);
}

private void printEmpList(List<Employee> list) {


if (list == null || [Link]()) {
return;
}
for (Employee employee : list) {
[Link](employee);
}
}

private void printEmpList() {


[Link]([Link]());
}

private void addEmployee() {


//Menu options
String[] options = {"Add Tester", "Add Developer",
"Add TeamLeader", "Return Main menu"};
int choice = 0;
Employee emp = null;
do {
[Link]("\nCompany Employee Management Program");
choice = [Link](options); // show Menu options
switch (choice) {
case 1:
[Link]("\nAdd new Tester");
String code = [Link]("Enter employee code:
");
String name = [Link]("Enter employee name:
");
double baseSal = [Link]("Enter base
salary: ");
double bonus = [Link]("Enter Tester
bonus: ");
String type = [Link]("Enter Tester type");
emp = new Tester(code, name, baseSal, bonus, type);
[Link](emp);
break;

case 2:
[Link]("\nAdd new Developer");
code = [Link]("Enter employee code: ");
name = [Link]("Enter employee name: ");
baseSal = [Link]("Enter base salary:
");
String team = [Link]("Enter Developer
team: ");
String pl = [Link]("Enter Developer
progaming languages: ");
List<String> programingLanguages = [Link]([Link](",
"));
int exp = (int) [Link]("Enter Developer
exp: ");
emp = new Developer(code, name, baseSal, team,
programingLanguages, exp);
[Link](emp);
break;
case 3:
String empCode = [Link]("Enter employee
code: ");
String empName = [Link]("Enter employee
name: ");
baseSal = [Link]("Enter base salary:
");
double bonusRate = [Link]("Enter bonus
rate: ");
String teamName = [Link]("Enter team name:
");

pl = [Link]("Enter programming languages


(separated by comma and space): ");
List<String> programmingLanguages = [Link]([Link](",
"));

int expYear = (int) [Link]("Enter


experience years: ");

emp = new TeamLeader(empCode, empName, baseSal, bonusRate,


teamName, programmingLanguages, expYear);
[Link](emp);
break;

}
[Link](emp);
} while (choice > 0 && choice < [Link]);
}

private void updateEmployee() {


String code = [Link]("Enter employee code to update:
");
Employee emp = [Link](code);
if (emp == null) {
[Link]("Employee does not exist.");
return;
}

[Link]("Updating: " + emp);

String name = [Link]("Enter new name: ");


if (![Link]()) {
[Link](name);
}
double baseSal = [Link]("Enter new base salary: ");
if (baseSal >= 0) {
[Link](baseSal);
}

if (emp instanceof [Link]) {


[Link] tester = ([Link]) emp;
double bonus = [Link]("Enter new bonus rate:
");
if (bonus >= 0) {
[Link](bonus);
}
String type = [Link]("Enter tester type: ");
if (![Link]()) {
[Link](type);
}

} else if (emp instanceof [Link]) {


[Link] tl = ([Link]) emp;
double bonus = [Link]("Enter new bonus rate:
");
if (bonus >= 0) {
[Link](bonus);
}
String team = [Link]("Enter new team: ");
if (![Link]()) {
[Link](team);
}
String pl = [Link]("Enter new programming
languages: ");
if (![Link]()) {
List<String> programmingLanguages = [Link]([Link](",\\
s*"));
[Link](programmingLanguages);
}
int exp = (int) [Link]("Enter new experience
years: ");
if (exp >= 0) {
[Link]((int) exp);
}

} else if (emp instanceof [Link]) {


[Link] dev = ([Link]) emp;
String team = [Link]("Enter new team: ");
if (![Link]()) {
[Link](team);
}
String pl = [Link]("Enter new programming
languages: ");
if (![Link]()) {
List<String> programmingLanguages = [Link]([Link](",\\
s*"));
[Link](programmingLanguages);
}
int exp = (int) [Link]("Enter new experience
years: ");
if (exp >= 0) {
[Link](exp);
}
}

[Link]("Update successful for: " + emp);


}

private void searchEmployee() {


String[] options = {
"Search by employee code",
"Search by name",
"Search salary",
"Search Developers by programming language",
"Find Tester with highest salary",
"Return to main menu"
};
int choice;
do {
choice = [Link](options);
switch (choice) {
case 1:
String code = [Link]("Enter employee code:
");
Employee emp = [Link](code);
if (emp != null) {
[Link](emp + " | salary=" + [Link]());
} else {
[Link]("No employee is matched.");
}
break;
case 2:
String name = [Link]("Enter name to
search: ");
List<Employee> list = [Link](name);
if ([Link]()) {
[Link]("No employee is matched.");
} else {
[Link](list);
}
break;
case 3:
double salary = [Link]("Enter minimum
salary: ");
List<Employee> listBySalary = [Link](salary);
if ([Link]()) {
[Link]("No employee is matched.");
} else {
[Link](listBySalary);
}
break;
case 4:
String lang = [Link]("Enter programming
language: ");
List<Employee> listByLang = [Link](lang);
if ([Link]()) {
[Link]("No employee is matched.");
} else {
[Link](listByLang);
}
break;
case 5:
Employee bestTester = [Link]();
if (bestTester != null) {
[Link]("Highest-paid Tester: " + bestTester + "
| salary = " + [Link]());
} else {
[Link]("No testers found.");
}
break;
case 6:
[Link]("Returning to main menu.");
break;

case 7:
[Link]("Returning to main menu.");
break;
}
} while (choice > 0 && choice < [Link]);
}

private void save() {


try {
[Link]();
[Link]("Data saved successfully.");
} catch (Exception ex) {
[Link]("Error saving data: " + [Link]());
}
}

private void sortEmployees() {


[Link]();
[Link]("Employee list sorted successfully.");
[Link]([Link]());
}
}
package viewer;

import [Link];

public class Menu {

public static int getChoice(Object[] options) {


for (int i = 0; i < [Link]; i++) {
[Link]((i + 1) + ". " + options[i]);
}
[Link]("Your options from 1 - " + [Link] + ": ");
Scanner sc = new Scanner([Link]);
return [Link]([Link]());
}
}

You might also like