Manager Class using Java
you are going to create a single class called Manager, which will extend NewWorker, you will also need MyDate, Name, NewWorker, Person, and TestManager . Manager is defined below. Manager:
The manager class has only two instance variables. The first is an array of size 10 of NewWorker base type, called employeesSupervised. The second is an int called numWorkersSupervised, which stores the number of workers supervised. Manager has the exact same constructors as NewWorker. Manager has a method called addWorker, which accepts a NewWorker as an argument, which adds the received argument to the instanceVariable employeesSupervised. This method must always add the argument to the first available index in the array, and it must also increment numWorkersSupervised. Manager has another method called deleteWorker, which accepts a NewWorker as an argument, finds the received NewWorker in employeesSupervised, and sets the indexed variable at its position to null. It must also decrease numWorkersSupervised. Further, deleteWorker must reorganized the array such that there are no gaps in it (as in, we can’t have 4 indices with NewWorker objects, two nulls, and then 4 more indices with NewWorker objects – it must have, instead, 8 indices with NewWorker objects followed by two null indices). Manager also has a getSalary method, and the Manager’s salary is its base salary with a bonus of $100 per worker supervised. Finally, the toString method of Manager must return the toString of NewWorker, with the list of employees supervised appended to the string (if any), as follows. It must first print, on a new line, the number of workers supervised followed by “Worker(s) supervised by this Manager:\n”. Finally, it returns each of the employees’ names on separate lines. If there are no workers supervised, it must return “Not supervising any employees”.
Sample output for TestManager:
Worker Number 4
Worker Name Roberts, Bob
Date Joined: February 5, 04
Supervisor: Doe, John
Salary: $60300.0
3 Worker(s) supervised by this Manager:
Hunter, Robert W.
Smith, John
Hull, Mary J.
Worker Number 5
Worker Name Doe, John
Date Joined: February 1, 03
No Supervisor
Salary: $65400.0
4 Worker(s) supervised by this Manager:
Hunter, Robert W.
Smith, John
Hull, Mary J.
Roberts, Bob
Worker Number 5
Worker Name Doe, John
Date Joined: February 1, 03
No Supervisor
Salary: $65200.0
2 Worker(s) supervised by this Manager:
Smith, John
Hull, Mary J.
Answer:
Person Class
public class Person{ private Name personName; private Person spouse; public Person(String fullName){ personName = new Name(fullName); } public void setSpouse(Person spouse){ this.spouse = spouse; } public Name getPersonName(){ return new Name(personName); } public Name getSpouseName(){ return new Name(spouse.personName); } public double getFamilyIncome(){ double incomePerson, incomeSpouse, familyIncome; if(this instanceof NewWorker){ incomePerson = ((NewWorker)this).getSalary(); } else{ incomePerson = 0.0; } if(spouse instanceof NewWorker){ incomeSpouse = ((NewWorker)spouse).getSalary(); } else{ incomeSpouse = 0.0; } familyIncome = incomePerson + incomeSpouse; return familyIncome; } public String toString(){ if(spouse != null){ return "Name is " + getPersonName() + "\nMarried to " + getSpouseName(); } else return "Name is " + getPersonName(); } }
Manager Class
public class Manager extends NewWorker{ private NewWorker employeesSupervised [] = new NewWorker[10]; private int numWorkersSupervised = 0; public Manager(String nameOfWorker, String joiningDate, double workerSalary){ super(nameOfWorker, joiningDate, workerSalary); } public Manager(String nameOfWorker, String joiningDate){ super(nameOfWorker, joiningDate); } public void addWorker(NewWorker w) { employeesSupervised[numWorkersSupervised] = w; numWorkersSupervised++; } public void deleteWorker(NewWorker w){ int i,j; boolean done = false; for(i=0; i<numWorkersSupervised && !done; i++){ if (employeesSupervised[i].equals(w)){ employeesSupervised[i]=null; numWorkersSupervised--; for(j=i; j<numWorkersSupervised; j++){ employeesSupervised[j]=employeesSupervised[j+1]; } employeesSupervised[j+1] = null; done = true; } } } public double getSalary(){ return super.getSalary() + (numWorkersSupervised * 100); } public String toString(){ int i; String output = super.toString() + "\n"; if(numWorkersSupervised > 0){ output += numWorkersSupervised + " Worker(s) supervised by this Manager:\n"; for(i=0; i< numWorkersSupervised; i++){ output += employeesSupervised[i].getPersonName() + "\n"; } } else{ return output + "Not supervising any employees\n"; } return output + "\n"; } }
NewWorker Class
public class NewWorker extends Person{ public static int howManyWorkers; private int workerNumber; private MyDate dateJoiningCompany; private double salary; private NewWorker supervisor; public NewWorker(String nameOfWorker, String joiningDate, double workerSalary){ super(nameOfWorker); this.dateJoiningCompany = new MyDate(joiningDate); this.salary = workerSalary; howManyWorkers++; this.workerNumber = howManyWorkers; } public NewWorker(String nameOfWorker, String joiningDate){ super(nameOfWorker); this.dateJoiningCompany = new MyDate(joiningDate); this.salary = 0; howManyWorkers++; this.workerNumber = howManyWorkers; } public void setSalary(double setWorkerSalary){ this.salary = setWorkerSalary; } public double getSalary(){ return salary; } public void setSupervisor(NewWorker setWorkerSupervisor){ this.supervisor = setWorkerSupervisor; } public static int getHowManyWorkers(){ return howManyWorkers; } public Name getSupervisor(){ return new Name(supervisor.getPersonName()); } public String toString(){ if(this.supervisor!= null){ return("Worker Number " + workerNumber + "\n" + "Worker Name " + getPersonName() + "\n" + "Date Joined: " + dateJoiningCompany + "\n" + "Supervisor: " + getSupervisor() + "\n"+ "Salary: $" + getSalary()); } else return("Worker Number " + workerNumber + "\n" + "Worker Name " + getPersonName() + "\n" + "Date Joined: " + dateJoiningCompany +" \nNo Supervisor\n" + "Salary: $" + getSalary()); } }
Name Class
import java.util.StringTokenizer; public class Name{ private String firstName = ""; private String middleName = ""; private String lastName = ""; public Name(String inputName){ StringTokenizer splitName = new StringTokenizer(inputName); int numTokens = splitName.countTokens(); firstName = splitName.nextToken(); if(numTokens == 3){ middleName = splitName.nextToken(); } else{ middleName = null; } lastName = splitName.nextToken(); } public Name(Name nameCopy){ this.firstName = nameCopy.firstName; this.middleName = nameCopy.middleName; this.lastName = nameCopy.lastName; } public void setName(String newFirstName, String newMiddleName, String newLastName){ this.firstName = newFirstName; this.middleName = newMiddleName; this.lastName = newLastName; } public String toString(){ if(middleName != null){ return (lastName +", " + firstName + " " + middleName.charAt(0) + "."); } else return (lastName +", " + firstName); } }
MyDate Class
import java.util.StringTokenizer; public class MyDate{ private int day; private int month; private int year; public MyDate(String unformattedDate){ StringTokenizer splitDate = new StringTokenizer(unformattedDate, "/"); this.day = Integer.parseInt(splitDate.nextToken()); this.month = Integer.parseInt(splitDate.nextToken()); this.year = Integer.parseInt(splitDate.nextToken()); } public MyDate(MyDate myDate){ this.day = myDate.day; this.month = myDate.month; this.year = myDate.year; } public String toString(){ String twoDigitYear = String.valueOf(this.year).substring(2,4); String monthName [] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; return monthName[month-1] + " " + day + ", " + twoDigitYear; } public boolean lessThan(MyDate myDate){ if(this.year < myDate.year){ return true; } else if (this.year > myDate.year){ return false; } else if (this.month < myDate.month){ return true; } else if (this.month > myDate.month){ return false; } else if (this.day < myDate.day){ return true; } else return false; } public boolean equals(MyDate myDate) { if (myDate == null) return false; if((this.day == myDate.day) && (this.month == myDate.month) && (this.year == myDate.year)){ return true; } else return false; } }
TestManager Class
public class TestManager{ public static void main(String[] args) { NewWorker w1 = new NewWorker("Robert William Hunter", "23/10/2005", 35000.00); NewWorker w2 = new NewWorker("John Smith", "15/11/2005", 25000.00); NewWorker w3 = new NewWorker("Mary Jane Hull", "06/09/2007"); Manager m1 = new Manager("Bob Roberts", "05/02/2004", 60000); Manager m2 = new Manager("John Doe", "01/02/2003", 65000); m1.addWorker(w1); m1.addWorker(w2); m1.addWorker(w3); m2.addWorker(w1); m2.addWorker(w2); m2.addWorker(w3); m2.addWorker(m1); m1.setSupervisor(m2); System.out.println(m1.toString()); System.out.println(m2.toString()); m2.deleteWorker(m1); m2.deleteWorker(w1); System.out.println(m2.toString()); } }
Leave a reply