Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.43 KB | None | 0 0
  1. package a1;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.List;
  6.  
  7. public class Company{
  8.  
  9.     private List<Employee> employees = new ArrayList<Employee>();
  10.  
  11.     public boolean hasEmployee(Employee employee){
  12.         for(Employee e:employees)
  13.         {
  14.             if(e.equals(employee))
  15.                 return true;
  16.         }
  17.         return false;
  18.     }
  19.  
  20.     public boolean addEmployee(Employee employee){
  21.         if(this.hasEmployee(employee)){
  22.             return false;
  23.         }
  24.         else
  25.         {
  26.             employees.add(employee);
  27.             return true;
  28.         }
  29.     }
  30.  
  31.     public void printAllEmployees(){
  32.         for(Employee employee:employees){
  33.             System.out.println(employee);
  34.         }
  35.     }
  36.  
  37.     public boolean isInCompany(Strategy strategy){
  38.         for(Employee employee: this.employees){
  39.             if(strategy.isCondition(employee))
  40.                 return true;
  41.         }
  42.         return false;
  43.     }
  44.  
  45. }
  46.  
  47. package a1;
  48.  
  49. public abstract class Employee {
  50.     private String name;
  51.     private String ID;
  52.  
  53.     public Employee(String name, String ID){
  54.         this.name=name;
  55.         this.ID=ID;
  56.     }
  57.  
  58.     public String getName() {
  59.         return name;
  60.     }
  61.  
  62.     public void setName(String name) {
  63.         this.name = name;
  64.     }
  65.  
  66.     public String getID() {
  67.         return ID;
  68.     }
  69.  
  70.     public void setID(String ID) {
  71.         this.ID = ID;
  72.     }
  73.  
  74.     public abstract double getSalary();
  75.  
  76.     public boolean equals(Object o){
  77.         return o instanceof Employee && this.name.equals(((Employee)o).name) && this.ID.equals(((Employee)o).ID);
  78.     }
  79.  
  80.     public String toString(){
  81.         return "Name: " + this.name +  ", ID: " + this.ID + ", Salary: " + this.getSalary();
  82.     }
  83. }
  84.  
  85. package a1;
  86.  
  87. public class HourlyPaidEmployee extends Employee {
  88.     private double hourlySalary;
  89.     private int hoursWorked;
  90.     public HourlyPaidEmployee(String name, String ID, double hourlySalary){
  91.         super(name,ID);
  92.         this.hourlySalary=hourlySalary;
  93.     }
  94.     public void setHoursWorked(int hoursWorked){
  95.         this.hoursWorked=hoursWorked;
  96.     }
  97.     public void addHoursWorked(int hoursWorked){
  98.         this.hoursWorked+=hoursWorked;
  99.     }
  100.     public double getSalary(){
  101.         return this.hourlySalary*this.hoursWorked;
  102.     }
  103. }
  104.  
  105. package a1;
  106.  
  107. public class Main {
  108.     public static void main(String[] args) {
  109.         Company c = new Company();
  110.         c.addEmployee(new PermanentEmployee("Chuck Norris","1337",12345));
  111.         c.addEmployee(new PermanentEmployee("Arnold Schwarzenegger","T800",0));
  112.         c.addEmployee(new HourlyPaidEmployee("Florin Piersic","1420623123453",999));
  113.         c.addEmployee(new PermanentEmployee("Clint Eastwood","303301",88));
  114.         c.printAllEmployees();
  115.         //no clones allowed!
  116.         c.addEmployee(new PermanentEmployee("Arnold Schwarzenegger","T800",0));
  117.         System.out.println("\n");
  118.         c.printAllEmployees();
  119.  
  120.         Strategy s1 = new NameStrategy("Florin Piersic");
  121.         Strategy s2 = new SalaryStrategy();
  122.         Strategy s3 = new NameStrategy("Florin Piersicutul");
  123.         Strategy s4 = new SalaryStrategy(999999999);
  124.  
  125.         System.out.println(c.isInCompany(s1));
  126.         System.out.println(c.isInCompany(s2));
  127.         System.out.println(c.isInCompany(s3));
  128.         System.out.println(c.isInCompany(s4));
  129.  
  130.     }
  131. }
  132.  
  133. package a1;
  134.  
  135. public class NameStrategy implements Strategy {
  136.     private String name;
  137.     public NameStrategy(String name){
  138.         this.name=name;
  139.     }
  140.  
  141.     @Override
  142.     public boolean isCondition(Employee employee) {
  143.         return this.name.equals(employee.getName());
  144.     }
  145. }
  146.  
  147. package a1;
  148.  
  149. public class PermanentEmployee extends Employee {
  150.     private double salary;
  151.     public PermanentEmployee(String name, String ID, double salary){
  152.         super(name,ID);
  153.         this.salary=salary;
  154.     }
  155.     public double getSalary(){
  156.         return this.salary;
  157.     }
  158. }
  159.  
  160. package a1;
  161.  
  162. public class SalaryStrategy implements Strategy {
  163.     private double salary;
  164.     public SalaryStrategy(double salary){
  165.         this.salary = salary;
  166.     }
  167.     public SalaryStrategy(){
  168.         this.salary=1000;
  169.     }
  170.  
  171.     @Override
  172.     public boolean isCondition(Employee employee) {
  173.         return this.salary<=employee.getSalary();
  174.     }
  175. }
  176.  
  177. package a1;
  178.  
  179. public interface Strategy {
  180.     public boolean isCondition(Employee employee);
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement