Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your assignment is to track the corporate careers of some up-and-coming executiv

ID: 3559735 • Letter: Y

Question

Your assignment is to track the corporate careers of some up-and-coming executives who are

In this version of the corporate world, people wither belong to a company or are unemployed.

people may be introduced by the JOIN command. (see below).

compensation.

Input

1.

From file "company.txt": The company names are listed one per line. There are

at most 20 companies. Company names are at most 10 characters and do not

contain embedded blanks.

2.

are at most 10 characters and do not contain embedded blanks.

currently belong to another company. Remember that when a person joins

a company.

CHANGE <person> <company>

may assume that the person is currently employed. This command does not

tell you the person's current employer; you have to search the data

structure to find the person.

QUIT <person>

that the person is currently employed.

PAYDAY

Each person is paid his or her salary ($5000). (You must keep track of

the amount each person has earned from the start of the program).

EMPLOYEES <company>

The current list of employees should be printed for the specified

<company>. The employees must be printed in order of their name.

UNEMPLOYED

The list of unemployed people should be printed.

DUMP

Print the employees in each company, as specified under the EMPLOYEES

appropriately.

END

Stop accepting commands.

----------------------------------------------------------------------------------------------------------------------------

Ouput

After all the commands have been processed, write a list of employees to a file "employee.txt".

The list consists of all the people who have been mentioned in any command and the total

amount of money they have been accumulated.

The following are some sample data"

file Company.txt

Borland

Microsoft

IBM

Intel

Compaq

NEC

Oracle

Commands from keyboard:

JOIN David IBM

JOIN Peter Intel

PAYDAY

CHANGE Peter Microsoft

Explanation / Answer

Indented version looks and feels better

import java.awt.*;
import java.io.*;
import java.util.*;

class Employee {
String name;
String workingCompany;
boolean isEmployed;
long salary;

public Employee(String name, String workingCompany) {
this.name = name;
salary = 0;
isEmployed = true;
this.workingCompany = workingCompany;
}

}

public class Solution {

private static BufferedWriter writer;
private static BufferedReader reader;

// storing list of companies
private static ArrayList<String> companies;
// storing list of employees
private static ArrayList<Employee> employees;

// printing unemployed people
private static void printUnemployed() {
sortEmployees();
for(Employee e: employees) {
if(!e.isEmployed) {
System.out.println(e.name);
}
}
}

/*
Explicitly printing all employed persons in sorted order by there names
private static void printEmployed() {
sortEmployees();
for(Employee e: employees) {
if(e.isEmployed) {
writer.println(e.name);
}
}
}
*/

// pay salary to employees
private static void giveSalary() {
// two different salaries are mentioned taking second one
final long employeeSalary = 5000;
for(Employee e: employees) {
if(e.isEmployed) {
e.salary += employeeSalary;
} else {
if(e.salary > 0) {
e.salary -= 700;
}
}
}
}

private static void changeCompany(String employeeName, String companyName) {
for(Employee e: employees) {
if(e.name.equals(employeeName)) {
e.workingCompany = companyName;
break;
}
}
}

// outputing employees of particular company
private static void printCompanyEmployees(String companyName) throws IOException {
sortEmployees();
for(Employee e: employees) {
if(e.workingCompany != null && e.workingCompany.equals(companyName)) {
System.out.println(e.name);
}
}
}

private static void sortEmployees() {
Collections.sort(employees, new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
return o1.name.compareTo(o2.name);
}
});
}

private static void joinCompany(String name, String companyName) {
employees.add(new Employee(name, companyName));
}

private static void quitJob(String name) {
for (Employee e: employees) {
if(e.name.equals(name)) {
e.isEmployed = false;
e.workingCompany = null;
break;
}
}
}

public static void main(String[] args) throws Exception {
reader = new BufferedReader(new FileReader("company.txt"));
companies = new ArrayList<String>();
employees = new ArrayList<Employee>();
for(String companyName = reader.readLine(); companyName != null; companyName = reader.readLine()) {
companies.add(companyName);
// System.out.println(companyName);
}
reader.close();
reader = new BufferedReader(new InputStreamReader(System.in));
String command = next();
while(!command.equals("END")) {
if(command.equals("JOIN")) {
String employeeName = next();
String companyName = next();
joinCompany(employeeName, companyName);
} else if(command.equals("CHANGE")) {
String employeeName = next();
String companyName = next();
changeCompany(employeeName, companyName);
} else if(command.equals("QUIT")) {
String employeeName = next();
quitJob(employeeName);
} else if(command.equals("PAYDAY")) {
giveSalary();
} else if(command.equals("EMPLOYEES")) {
String companyName = next();
printCompanyEmployees(companyName);
} else if(command.equals("UNEMPLOYED")) {
printUnemployed();
} else if(command.equals("DUMP")) {
for(String company : companies) {
printCompanyEmployees(company);
}
printUnemployed();
}
command = next();
}
writer = new BufferedWriter(new FileWriter(new File("employee.txt")));
sortEmployees();
for(Employee e: employees) {
writer.write(e.name + " " + e.salary);
writer.newLine();
}
writer.close();
}

// fast input from keyboard
private static StringTokenizer st;

private static String next() throws Exception {
if (st == null || !st.hasMoreTokens())
st = new StringTokenizer(reader.readLine());
if(st == null || !st.hasMoreTokens()) return null;
else return st.nextToken();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote