Advertisement
STANAANDREY

employee

Oct 27th, 2023
1,498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1.  
  2. class Employee {
  3.     private final String name;
  4.     private final int salary;
  5.  
  6.     public Employee(final String name, final int salary) {
  7.         this.name = name;
  8.         this.salary = salary;
  9.     }
  10.  
  11.     @Override
  12.     public boolean equals(Object o) {
  13.         if (o instanceof Employee) {
  14.             Employee e = (Employee)o;
  15.             return e.name.equals(name) && e.salary == salary;
  16.         }
  17.         return false;
  18.     }
  19.  
  20.     @Override
  21.     public String toString() {
  22.         return "Emp " + name + " - " + salary;
  23.     }
  24.  
  25.     public int getSalary() {
  26.         return salary;
  27.     }
  28. }
  29.  
  30. class Company {
  31.     private final String name;
  32.     private final int budget;
  33.     private final Employee[] employees;
  34.     private int currLen;
  35.     private final static int MAX_LEN = 30;
  36.  
  37.     public Company(String name, int budget) {
  38.         this.name = name;
  39.         this.budget = budget;
  40.         employees = new Employee[MAX_LEN];
  41.     }
  42.  
  43.     public boolean add(Employee employee) {
  44.         if (MAX_LEN == currLen) {
  45.             return false;
  46.         }
  47.         for (int i = 0; i < currLen; i++) {
  48.             if (employees[i].equals(employee)) {
  49.                 return false;
  50.             }
  51.         }
  52.         employees[currLen++] = employee;
  53.         return true;
  54.     }
  55.  
  56.     @Override
  57.     public String toString() {
  58.         String s = name;
  59.         for (int i = 0; i < currLen; i++) {
  60.             s += " " + employees[i];
  61.         }
  62.         return s;
  63.     }
  64.  
  65.     public int pay() {
  66.         int sum = 0;
  67.         for (int i = 0; i < currLen; i++) {
  68.             sum += employees[i].getSalary();
  69.         }
  70.         return budget - sum;
  71.     }
  72. }
  73.  
  74. public class Main {
  75.     public static void main(String[] args) {
  76.         Company c = new Company("intel", 100);
  77.         Employee e1, e2, e3;
  78.         e1 = new Employee("ion1", 30);
  79.         e2 = new Employee("ion2", 50);
  80.         e3 = new Employee("ion3", 30);
  81.         System.out.println(c.add(e1));
  82.         System.out.println(c.add(e2));
  83.         System.out.println(c.add(e3));
  84.         System.out.println(c);
  85.         System.out.println(c.add(e2));
  86.         System.out.println(c.pay());
  87.  
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement