Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.98 KB | None | 0 0
  1. package companyemployees;
  2.  
  3. public abstract class Employees {
  4.      protected String name;
  5.      protected String ID;
  6.      
  7.      public Employees(String name, String ID) {
  8.          this.name = name;
  9.          this.ID = ID;
  10.      }
  11.      
  12.  
  13.      public String getName() {
  14.          return this.name;
  15.      }
  16.      
  17.      public void setName(String n) {
  18.          this.name = n;
  19.      }
  20.      
  21.      public String getID() {
  22.          return this.ID;
  23.      }
  24.      
  25.      public void setID(String ID) {
  26.          this.ID = ID;
  27.      }
  28.      
  29.      public abstract double getSalary();
  30.      
  31.      public boolean equals(Object o) {
  32.          return (o instanceof Employees) && ((Employees)o).name == name && ((Employees)o).ID == ID;
  33.      }
  34.      
  35.      public String toString() {
  36.          return "Name:" + this.name + " ID:" +this.ID + " Salary:"+ this.getSalary();
  37.      }
  38. }
  39.  
  40.  
  41. package companyemployees;
  42.  
  43. public class PermanentEmployee extends Employees{
  44.  
  45.     public double salary;
  46.    
  47.     public PermanentEmployee(String name, String ID, double salary) {
  48.         super(name,ID);
  49.         this.salary = salary;
  50.     }
  51.    
  52.     public double getSalary() {
  53.         return this.salary;
  54.     }
  55. }
  56.  
  57.  
  58. package companyemployees;
  59.  
  60. public class HourlyPaidEmployee extends Employees{
  61.    
  62.     int hoursWorked;
  63.     int rate;
  64.    
  65.     public HourlyPaidEmployee(String name, String ID, int rate) {
  66.         super(name, ID);
  67.         this.rate = rate;
  68.     }
  69.    
  70.     public void setHoursWorked(int hoursWorked) {
  71.         this.hoursWorked = hoursWorked;
  72.     }
  73.    
  74.     public double getSalary() {
  75.         return this.hoursWorked * rate ;
  76.     }
  77.    
  78. }
  79.  
  80.  
  81. package companyemployees;
  82.  
  83. import java.util.ArrayList;
  84.  
  85.  
  86. public class Company {
  87.    
  88.     ArrayList <Employees> list = new ArrayList <> ();
  89.    
  90.     public boolean hasEmployee(Employees employee) {
  91.         for(Employees e: list) {
  92.             if(e.equals(employee)) {
  93.                 return true;
  94.             }
  95.         }
  96.         return false;
  97.     }
  98.    
  99.     public boolean addEmployee(Employees employee) {
  100.         if(this.hasEmployee(employee)) {
  101.             return false;
  102.         }
  103.         else {
  104.             list.add(employee);
  105.             return true;
  106.         }
  107.     }
  108.    
  109.     public void printAllEmployess() {
  110.         for(Employees e: list) {
  111.             System.out.println(e);
  112.         }
  113.     }
  114.    
  115.     public boolean isInCompany(Strategy strategy) {
  116.             for(Employees e: list) {
  117.                 if(strategy.isCondition(e))
  118.                     return true;
  119.                
  120.                
  121.             }
  122.             return false;
  123.     }
  124.    
  125. }
  126.  
  127.  
  128. package companyemployees;
  129.  
  130. public interface Strategy {
  131.    
  132.     public boolean isCondition(Employees employee);
  133.  
  134. }
  135.  
  136.  
  137.  
  138.  
  139. package companyemployees;
  140.  
  141. public class StrategyName implements Strategy{
  142.  
  143.     private String name;
  144.    
  145.     public StrategyName (String name) {
  146.         this.name = name;
  147.     }
  148.    
  149.     @Override
  150.     public boolean isCondition(Employees employee) {
  151.         return this.name.equals(employee.getName());
  152.     }
  153.    
  154. }
  155.  
  156.  
  157.  
  158. package companyemployees;
  159.  
  160. public class StrategySalary implements Strategy{
  161.    
  162.     private double salary;
  163.    
  164.     public StrategySalary(double salary) {
  165.         this.salary = salary;
  166.     }
  167.    
  168.     public StrategySalary() {
  169.         this.salary = 1000;
  170.     }
  171.    
  172.     @Override
  173.     public boolean isCondition(Employees employee) {
  174.         return this.salary<=employee.getSalary();
  175.     }
  176.    
  177. }
  178.  
  179.  
  180. package companyemployees;
  181.  
  182. public class Main {
  183.    
  184.     public static void main(String []args) {
  185.         Company c = new Company();
  186.        
  187.         PermanentEmployee e1 = new PermanentEmployee("Sebastian","CNP123",234.54);
  188.        
  189.         HourlyPaidEmployee e2 = new HourlyPaidEmployee("Miki","CNP456",15);
  190.         e2.setHoursWorked(15);
  191.        
  192.         //System.out.println(e1);
  193.         //System.out.println(e2);
  194.        
  195.         c.addEmployee(e1);
  196.         c.addEmployee(e2);
  197.        
  198.         c.printAllEmployess();
  199.        
  200.         PermanentEmployee e3 = new PermanentEmployee("Sebastian","CNP123",1234.54);
  201.         HourlyPaidEmployee e4 = new HourlyPaidEmployee("Miki","CNP456",15);
  202.         e4.setHoursWorked(15);
  203.        
  204.         c.addEmployee(e3);
  205.         c.addEmployee(e4);
  206.        
  207.         c.printAllEmployess();
  208.        
  209.         Strategy s1 = new StrategyName("Miki");
  210.         System.out.println(s1.isCondition(e4));
  211.        
  212.         Strategy s2 = new StrategyName(" ");
  213.         System.out.println(s2.isCondition(e3));
  214.        
  215.         Strategy s3 = new StrategySalary(1000);
  216.         System.out.println(s3.isCondition(e3));
  217.        
  218.         Strategy s4 = new StrategySalary();
  219.         System.out.println(s4.isCondition(e4));
  220.        
  221.     }
  222.    
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement