Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Employee {
- private final String name;
- private final int salary;
- public Employee(final String name, final int salary) {
- this.name = name;
- this.salary = salary;
- }
- @Override
- public boolean equals(Object o) {
- if (o instanceof Employee) {
- Employee e = (Employee)o;
- return e.name.equals(name) && e.salary == salary;
- }
- return false;
- }
- @Override
- public String toString() {
- return "Emp " + name + " - " + salary;
- }
- public int getSalary() {
- return salary;
- }
- }
- class Company {
- private final String name;
- private final int budget;
- private final Employee[] employees;
- private int currLen;
- private final static int MAX_LEN = 30;
- public Company(String name, int budget) {
- this.name = name;
- this.budget = budget;
- employees = new Employee[MAX_LEN];
- }
- public boolean add(Employee employee) {
- if (MAX_LEN == currLen) {
- return false;
- }
- for (int i = 0; i < currLen; i++) {
- if (employees[i].equals(employee)) {
- return false;
- }
- }
- employees[currLen++] = employee;
- return true;
- }
- @Override
- public String toString() {
- String s = name;
- for (int i = 0; i < currLen; i++) {
- s += " " + employees[i];
- }
- return s;
- }
- public int pay() {
- int sum = 0;
- for (int i = 0; i < currLen; i++) {
- sum += employees[i].getSalary();
- }
- return budget - sum;
- }
- }
- public class Main {
- public static void main(String[] args) {
- Company c = new Company("intel", 100);
- Employee e1, e2, e3;
- e1 = new Employee("ion1", 30);
- e2 = new Employee("ion2", 50);
- e3 = new Employee("ion3", 30);
- System.out.println(c.add(e1));
- System.out.println(c.add(e2));
- System.out.println(c.add(e3));
- System.out.println(c);
- System.out.println(c.add(e2));
- System.out.println(c.pay());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement