Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package companyemployees;
- public abstract class Employees {
- protected String name;
- protected String ID;
- public Employees(String name, String ID) {
- this.name = name;
- this.ID = ID;
- }
- public String getName() {
- return this.name;
- }
- public void setName(String n) {
- this.name = n;
- }
- public String getID() {
- return this.ID;
- }
- public void setID(String ID) {
- this.ID = ID;
- }
- public abstract double getSalary();
- public boolean equals(Object o) {
- return (o instanceof Employees) && ((Employees)o).name == name && ((Employees)o).ID == ID;
- }
- public String toString() {
- return "Name:" + this.name + " ID:" +this.ID + " Salary:"+ this.getSalary();
- }
- }
- package companyemployees;
- public class PermanentEmployee extends Employees{
- public double salary;
- public PermanentEmployee(String name, String ID, double salary) {
- super(name,ID);
- this.salary = salary;
- }
- public double getSalary() {
- return this.salary;
- }
- }
- package companyemployees;
- public class HourlyPaidEmployee extends Employees{
- int hoursWorked;
- int rate;
- public HourlyPaidEmployee(String name, String ID, int rate) {
- super(name, ID);
- this.rate = rate;
- }
- public void setHoursWorked(int hoursWorked) {
- this.hoursWorked = hoursWorked;
- }
- public double getSalary() {
- return this.hoursWorked * rate ;
- }
- }
- package companyemployees;
- import java.util.ArrayList;
- public class Company {
- ArrayList <Employees> list = new ArrayList <> ();
- public boolean hasEmployee(Employees employee) {
- for(Employees e: list) {
- if(e.equals(employee)) {
- return true;
- }
- }
- return false;
- }
- public boolean addEmployee(Employees employee) {
- if(this.hasEmployee(employee)) {
- return false;
- }
- else {
- list.add(employee);
- return true;
- }
- }
- public void printAllEmployess() {
- for(Employees e: list) {
- System.out.println(e);
- }
- }
- public boolean isInCompany(Strategy strategy) {
- for(Employees e: list) {
- if(strategy.isCondition(e))
- return true;
- }
- return false;
- }
- }
- package companyemployees;
- public interface Strategy {
- public boolean isCondition(Employees employee);
- }
- package companyemployees;
- public class StrategyName implements Strategy{
- private String name;
- public StrategyName (String name) {
- this.name = name;
- }
- @Override
- public boolean isCondition(Employees employee) {
- return this.name.equals(employee.getName());
- }
- }
- package companyemployees;
- public class StrategySalary implements Strategy{
- private double salary;
- public StrategySalary(double salary) {
- this.salary = salary;
- }
- public StrategySalary() {
- this.salary = 1000;
- }
- @Override
- public boolean isCondition(Employees employee) {
- return this.salary<=employee.getSalary();
- }
- }
- package companyemployees;
- public class Main {
- public static void main(String []args) {
- Company c = new Company();
- PermanentEmployee e1 = new PermanentEmployee("Sebastian","CNP123",234.54);
- HourlyPaidEmployee e2 = new HourlyPaidEmployee("Miki","CNP456",15);
- e2.setHoursWorked(15);
- //System.out.println(e1);
- //System.out.println(e2);
- c.addEmployee(e1);
- c.addEmployee(e2);
- c.printAllEmployess();
- PermanentEmployee e3 = new PermanentEmployee("Sebastian","CNP123",1234.54);
- HourlyPaidEmployee e4 = new HourlyPaidEmployee("Miki","CNP456",15);
- e4.setHoursWorked(15);
- c.addEmployee(e3);
- c.addEmployee(e4);
- c.printAllEmployess();
- Strategy s1 = new StrategyName("Miki");
- System.out.println(s1.isCondition(e4));
- Strategy s2 = new StrategyName(" ");
- System.out.println(s2.isCondition(e3));
- Strategy s3 = new StrategySalary(1000);
- System.out.println(s3.isCondition(e3));
- Strategy s4 = new StrategySalary();
- System.out.println(s4.isCondition(e4));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement