Assignment: Write a java class(es) with methods to manage employment for a small
ID: 3781518 • Letter: A
Question
Assignment: Write a java class(es) with methods to manage employment for a small bakery. All classes should have at least a null constructor and copy constructor. Other constructors would be up to your discretion. Include all necessary accessors and modifiers. To test your classes, create a Container class with simply a main() method. The Container class will have attributes that are class objects of each of the classes you create. The main() method will create the class objects, perform some normal business for that store, and produce appropriate output.
Possibilities:
-Add new employee and personal info
-Time Card Check – this method will check to see if the employee worked any hours this week
Time Card Add – this method will add hours to the employees time card.
Time Card Total – this method will return the total hours worked by an employee.
Employee Shift – this method will assign employees to a number of shifts.
Empty Shifts – this method will determine if any shift is empty.
Shift Add – This will add an employee to a shift.
Shift Min – this method will set the minimum number of workers needed for a shift
Explanation / Answer
Please find the below solution for the bakery application:
package org.learning.chegg.bakery;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class Bakery {
private List<Employee> employees;
private Map<ShiftName, List<Employee>> shifts;
private int minNumOfEmployeesInAShift;
public Bakery() {
employees = new ArrayList<>();
shifts = new HashMap<>();
shifts.put(ShiftName.MORNING, new ArrayList<>());
shifts.put(ShiftName.EVENING, new ArrayList<>());
shifts.put(ShiftName.NIGHT, new ArrayList<>());
minNumOfEmployeesInAShift = 2;
}
public void addEmployee(Employee e){
employees.add(e);
}
public List<ShiftName> listEmptyShifts(){
List<ShiftName> emptyShifts = new ArrayList<>();
for(Entry<ShiftName, List<Employee>> entry: shifts.entrySet()){
if(entry.getValue().isEmpty()){
emptyShifts.add(entry.getKey());
}
}
return emptyShifts;
}
public boolean isShiftEmpty(ShiftName shiftName){
if(shifts.get(shiftName).isEmpty())
return true;
else
return false;
}
public void addEmployeeToShift(Employee emp, ShiftName shiftName){
shifts.get(shiftName).add(emp);
emp.shiftAdd(shiftName);
}
public void shiftMin(){
for(Entry<ShiftName, List<Employee>> entry: shifts.entrySet()){
int empWorkingReqForShift = minNumOfEmployeesInAShift - entry.getValue().size();
if(empWorkingReqForShift > 0){
for(Employee emp: employees){
if(!emp.timeCardCheck()){
addEmployeeToShift(emp, entry.getKey());
empWorkingReqForShift--;
}
if(empWorkingReqForShift == 0){
break;
}
}
}
}
}
}
package org.learning.chegg.bakery;
import java.util.Map;
public class Employee {
private int employeeId;
private String name;
private int age;
private int salary;
private Map<ShiftName, Integer> shiftsWorking;
public Employee() {
}
public Employee(int employeeId, String name, int age, int salary) {
super();
this.employeeId = employeeId;
this.name = name;
this.age = age;
this.salary = salary;
}
public Employee(Employee other) {
super();
this.employeeId = other.getEmployeeId();
this.name = other.getName();
this.age = other.getAge();
this.salary = other.getSalary();
}
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public boolean timeCardCheck(){
boolean res = false;
for(ShiftName shiftNames: shiftsWorking.keySet()){
if(shiftsWorking.get(shiftNames).intValue() > 0){
res = true;
break;
}
}
return res;
}
public void timeCardAdd(ShiftName shiftName, int NumOfHours){
int hoursWorking = shiftsWorking.get(shiftName).intValue();
hoursWorking += hoursWorking;
shiftsWorking.put(shiftName, new Integer(hoursWorking));
}
public void timeCardAdd(int NumOfHours){
for(ShiftName shiftName: shiftsWorking.keySet()){
int hoursWorking = shiftsWorking.get(shiftName).intValue();
hoursWorking += hoursWorking;
shiftsWorking.put(shiftName, new Integer(hoursWorking));
break;
}
}
public int timeCardTotal(){
int total = 0;
for(ShiftName shiftNames: shiftsWorking.keySet()){
total += shiftsWorking.get(shiftNames).intValue();
}
return total;
}
public void shiftAdd(ShiftName shiftName){
shiftsWorking.put(shiftName, new Integer(0));
}
}
package org.learning.chegg.bakery;
public enum ShiftName {
MORNING("8AM","4PM"),
EVENING("4PM","12AM"),
NIGHT("12AM","8AM");
private String fromTime;
private String toTime;
public String getFromTime() {
return fromTime;
}
public String getToTime() {
return toTime;
}
private ShiftName(String fromTime, String toTime) {
this.fromTime = fromTime;
this.toTime = toTime;
}
}
package org.learning.chegg.bakery;
public class Application {
public static void main(String[] args) {
Bakery bakery = new Bakery();
Employee e1 = new Employee(100, "Emp1", 28, 3000);
Employee e2 = new Employee(100, "Emp2", 22, 2000);
Employee e3 = new Employee(100, "Emp3", 26, 3000);
Employee e4 = new Employee(100, "Emp4", 28, 3000);
Employee e5 = new Employee(100, "Emp5", 30, 4000);
Employee e6 = new Employee(100, "Emp6", 25, 2500);
Employee e7 = new Employee(100, "Emp7", 32, 4200);
Employee e8 = new Employee(100, "Emp8", 27, 2900);
bakery.addEmployee(e1);
bakery.addEmployee(e2);
bakery.addEmployee(e3);
bakery.addEmployee(e4);
bakery.addEmployee(e5);
bakery.addEmployee(e6);
bakery.addEmployee(e7);
bakery.addEmployee(e8);
//Add minimum number of employees to different shifts
bakery.shiftMin();
e1.timeCardAdd(10);
e2.timeCardAdd(9);
e3.timeCardAdd(8);
e4.timeCardAdd(11);
e5.timeCardAdd(12);
e6.timeCardAdd(16);
e7.timeCardAdd(14);
e8.timeCardAdd(9);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.