Question: For this lab, we will write a Company Simulator where different employees or different levels will attempt to program stuff. However, there's a chance they

For this lab, we will write a "Company Simulator" where different employees or different levels will attempt to "program" stuff. However, there's a chance they might break something, and will have to call their Manager to fix it.

For this lab, we will write a "Company Simulator" where different employees

Task 1: Implement the Missing Code

* Employee.java

* `equals()`

CODE:

public abstract class Employee {

private String name;

private int id;

public Employee() {

name = "";

id = -1;

}

public Employee(String name, int id) {

this.name = name;

this.id = id;

}

public String getName() { return name; }

public int getId() { return id; }

public abstract boolean doWork();

public String toString() { return name + ", ID: " + id; }

public boolean equals(Object o) {

System.out.println("Incomplete - implement me");

return false;

}

}

* SoftwareEngineer.java

* `doWork()`

* `toString()`

* Add the job title to supers `toString` method's value.

CODE:

import java.util.Random;

public class SoftwareEngineer extends Employee {

public SoftwareEngineer() { super(); }

public SoftwareEngineer(String name, int id) { super(name, id); }

public boolean doWork() {

Random rand = new Random();

System.out.println("Incomplete - implement me");

// get a random number between 0 and 99

// if it's less than 10

// print "I broke something..."

// return false

// otherwise

// print "I am programming."

// return true

return true;

}

public String toString() {

System.out.println("Incomplete - Implement me");

return "";

}

}

* SoftwareManager.java

* Also includes the additional method `fixProblem()`.

* Essentially the same as Software Engineer, but with a different name.

CODE:

same as Software Engineer

* Executive.java

* `toString()`

* Add the job title to supers `toString` method's value.

CODE:

public class Executive extends Employee implements Manager {

public Executive() { super(); }

public Executive(String name, int id) { super(name, id); }

public boolean doWork() {

System.out.println("Playing golf...");

return true;

}

public void fixProblem() {

System.out.println("You pulled me away from golf for this?");

}

public String toString() {

System.out.println("Incomplete - implement me");

return "";

}

}

* CompanySimulator.java

* Constructor method

* `initCompany()`

* Add SoftwareEngineer's and SoftwareManager's to the company.

* `handleProblem()`

CODE:

import java.util.ArrayList;

import java.util.Random;

public class CompanySimulator {

private ArrayList employees = new ArrayList();

// Randomly generated from some website... 24 in total

private String[] randomNames = {

"Dolores Dunn", "Sadie Lambert", "Ed Vega",

"Kristin Rose", "Tommie Burns", "Devin Newman",

"Norma Page", "Rafael Willis", "Esther James",

"Matt Garcia", "Brendan Gilbert", "Gail Rowe",

"Sabrina Hamilton", "Gwendolyn Harrison", "Mack Osborne",

"Leon Moody", "Derek Carter", "Garrett Lowe",

"Bridget Fox", "Donnie Hampton", "Clinton Mclaughlin",

"Tami Erickson", "Sonja Peterson", "Marian Lawrence"};

public CompanySimulator() {

System.out.println("Incomplete - implement me");

// call initCompany

// call displayEmployees

}

public void initCompany() {

// Hire people, assign Manager/Software Engineer positions

// Let's start by adding the CEO

employees.add(new Executive("Kevin Scrivnor", 1));

// Add the rest based on the index of i.

for (int i = 0; i

int id = i + 2;

String name = randomNames[i];

// if the i is divisible by 6, add a new manager

// otherwise, add a software engineer

}

}

public void displayEmployees() {

System.out.println("Displaying the employees:");

// Print out the list of employees

for (Employee e : employees) {

System.out.println(e);

}

System.out.println("");

}

public void performWork() {

for (Employee e : employees) {

System.out.print(e.getName() + ": ");

boolean res = e.doWork();

if (!res) {

handleProblem(e);

}

}

}

public void handleProblem(Employee e) {

System.out.println("Incomplete - implement me");

// if the employee is a Software Engineer

// Print "Looks like [name] broke something. Looking for their manager..."

// get the index of the the employee from the employees ArrayList

// starting from that index, while you haven't found a mananger

// keep looking backwards (lower indexes) till you find one

// Now that you have found their manager, the manager should fix the problem

// otherwise, if the employee is a SoftwareManager then

// the executive should fix the problem.

}

public void simulate(int time) {

int currentTime = 0;

Random rand = new Random();

while (currentTime

int timeIncrease = rand.nextInt(10) + 1;

currentTime += timeIncrease;

System.out.println("Current time: " + currentTime);

performWork();

System.out.println("");

}

}

public static void main(String[] args) {

CompanySimulator cs = new CompanySimulator();

cs.simulate(100);

}

}

Task 2: Make the Output Pretty

Modify the CompanySimulator.java's `displayCompany()` method to print the company in tab format (use `printf`) so it looks pretty. Your output should match:

or different levels will attempt to "program" stuff. However, there's a chance

Task 3: Read the Names From a File

In `CompanySimulator.java`, remove the field `randomNames`.

In `CompanySimulator.java`, modify the method `initCompany()` to assign the names of the Employees from reading the file instead. The same rules as to who is a Software Manager vs. Software Engineer, using the line numbers as an index.

Recall that there may be some Exceptions you need to handle when reading from a file. You should test reading from files that do not exist as well.

??Java Class>> SoftwareEngineer edu.csuci.comp150.L06 FSoftwareEngineer) SoftwareEngineer(String,int) doWork():boolean > ??Java Class GEmployee C CompanySimulator edu.csuci.comp150.L06 o toString0:String edu.csuci.comp150.L06 a name: String a id: int a randomNames: Stringl > CompanySimulator) o initCompany():void e displayEmployees(:void o performWork():void e handleProblem(Employee):void o simulate(int):void SoftwareManager SoftwareManager() doWork):boolean Employee) edu.csuci.comp150.LO6 Employee(String,int) o getName():String o getld():int d doWork):boolean o toString):String o equals (Object):boolean FSoftwareManager(String,int) e fixProblem(:void e toString):String main(Stringl):void > C Executive edu.csuci.comp150.L06 > Manager edu.csuci.compl 50.?06 Executive() Executive(String,int) o fixProblem():void o doWork():boolean o fixProblem():void o toString0:String

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!