Advertisement
coasterka

Employee

Dec 12th, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. public class Employee {
  2.    
  3.     private String name;
  4.     private Task currentTask;
  5.     private double hoursLeft;
  6.    
  7.     public Employee(String name) {
  8.         this.name = name;
  9.         // or this.setName(name); ?
  10.     }
  11.    
  12.     public String getName() {
  13.         return this.name;
  14.     }
  15.    
  16.     public void setName(String name) {
  17.         if (name.isEmpty()) {
  18.             throw new IllegalArgumentException("Employee's name can not be empty!");
  19.         }
  20.         else {
  21.             this.name = name;
  22.         }
  23.     }
  24.    
  25.     public Task getCurrentTask() {
  26.         return this.currentTask;
  27.     }
  28.    
  29.     public void setCurrentTask(Task task) {
  30.         if (task == null) {
  31.             throw new IllegalArgumentException("Task is not defined!");
  32.         }
  33.         else {
  34.             this.currentTask = task;
  35.         }
  36.     }
  37.    
  38.     public double getHoursLeft() {
  39.         return this.hoursLeft;
  40.     }
  41.    
  42.     public void setHoursLeft(double hoursLeft) {
  43.         if (hoursLeft <= 0) {
  44.             throw new IllegalArgumentException("Hours left can not be negative or = 0!");
  45.         }
  46.         else {
  47.             this.hoursLeft = hoursLeft;
  48.         }
  49.     }
  50.    
  51.     public void work() {
  52.         if (this.currentTask == null) {
  53.             System.out.println("Employee doesn't have a current task.");
  54.             return;
  55.         }
  56.         if (this.hoursLeft <= 0) {
  57.             System.out.println("Employee doesn't have any hours left for today.");
  58.             return;
  59.         }
  60.         if (this.currentTask.getWorkingHours() <= 0) {
  61.             System.out.println("Task is already finished.");
  62.             return;
  63.         }
  64.         double taskHours = this.currentTask.getWorkingHours();
  65.         double employeeHours = this.hoursLeft;
  66.         if (taskHours > employeeHours) {
  67.             taskHours -= employeeHours;
  68.             employeeHours = 0;
  69.         }
  70.         else if (employeeHours > taskHours) {
  71.             employeeHours -= taskHours;
  72.             taskHours = 0;
  73.         }
  74.         else {
  75.             employeeHours = 0;
  76.             taskHours = 0;
  77.         }
  78.         this.currentTask.setWorkingHours(taskHours);
  79.         this.hoursLeft = employeeHours;
  80.         showReport();
  81.     }
  82.    
  83.     public void showReport() {
  84.         String report = "Employee name: " + this.name + ", Task name: " + this.currentTask.getName() +
  85.                 "\nEmployee hours left: " + this.hoursLeft + ", Task hours left: " +
  86.                 this.currentTask.getWorkingHours();
  87.         System.out.println(report);
  88.     }  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement