Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package a1;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.List;
- public class Company{
- private List<Employee> employees = new ArrayList<Employee>();
- public boolean hasEmployee(Employee employee){
- for(Employee e:employees)
- {
- if(e.equals(employee))
- return true;
- }
- return false;
- }
- public boolean addEmployee(Employee employee){
- if(this.hasEmployee(employee)){
- return false;
- }
- else
- {
- employees.add(employee);
- return true;
- }
- }
- public void printAllEmployees(){
- for(Employee employee:employees){
- System.out.println(employee);
- }
- }
- public boolean isInCompany(Strategy strategy){
- for(Employee employee: this.employees){
- if(strategy.isCondition(employee))
- return true;
- }
- return false;
- }
- }
- package a1;
- public abstract class Employee {
- private String name;
- private String ID;
- public Employee(String name, String ID){
- this.name=name;
- this.ID=ID;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getID() {
- return ID;
- }
- public void setID(String ID) {
- this.ID = ID;
- }
- public abstract double getSalary();
- public boolean equals(Object o){
- return o instanceof Employee && this.name.equals(((Employee)o).name) && this.ID.equals(((Employee)o).ID);
- }
- public String toString(){
- return "Name: " + this.name + ", ID: " + this.ID + ", Salary: " + this.getSalary();
- }
- }
- package a1;
- public class HourlyPaidEmployee extends Employee {
- private double hourlySalary;
- private int hoursWorked;
- public HourlyPaidEmployee(String name, String ID, double hourlySalary){
- super(name,ID);
- this.hourlySalary=hourlySalary;
- }
- public void setHoursWorked(int hoursWorked){
- this.hoursWorked=hoursWorked;
- }
- public void addHoursWorked(int hoursWorked){
- this.hoursWorked+=hoursWorked;
- }
- public double getSalary(){
- return this.hourlySalary*this.hoursWorked;
- }
- }
- package a1;
- public class Main {
- public static void main(String[] args) {
- Company c = new Company();
- c.addEmployee(new PermanentEmployee("Chuck Norris","1337",12345));
- c.addEmployee(new PermanentEmployee("Arnold Schwarzenegger","T800",0));
- c.addEmployee(new HourlyPaidEmployee("Florin Piersic","1420623123453",999));
- c.addEmployee(new PermanentEmployee("Clint Eastwood","303301",88));
- c.printAllEmployees();
- //no clones allowed!
- c.addEmployee(new PermanentEmployee("Arnold Schwarzenegger","T800",0));
- System.out.println("\n");
- c.printAllEmployees();
- Strategy s1 = new NameStrategy("Florin Piersic");
- Strategy s2 = new SalaryStrategy();
- Strategy s3 = new NameStrategy("Florin Piersicutul");
- Strategy s4 = new SalaryStrategy(999999999);
- System.out.println(c.isInCompany(s1));
- System.out.println(c.isInCompany(s2));
- System.out.println(c.isInCompany(s3));
- System.out.println(c.isInCompany(s4));
- }
- }
- package a1;
- public class NameStrategy implements Strategy {
- private String name;
- public NameStrategy(String name){
- this.name=name;
- }
- @Override
- public boolean isCondition(Employee employee) {
- return this.name.equals(employee.getName());
- }
- }
- package a1;
- public class PermanentEmployee extends Employee {
- private double salary;
- public PermanentEmployee(String name, String ID, double salary){
- super(name,ID);
- this.salary=salary;
- }
- public double getSalary(){
- return this.salary;
- }
- }
- package a1;
- public class SalaryStrategy implements Strategy {
- private double salary;
- public SalaryStrategy(double salary){
- this.salary = salary;
- }
- public SalaryStrategy(){
- this.salary=1000;
- }
- @Override
- public boolean isCondition(Employee employee) {
- return this.salary<=employee.getSalary();
- }
- }
- package a1;
- public interface Strategy {
- public boolean isCondition(Employee employee);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement