Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.76 KB | None | 0 0
  1. /**
  2.  * Created by Scy on 03/12/2016.
  3.  */
  4. public class Main3 {
  5.  
  6.     public static void main(String[] args){
  7.         Company company=new Company();
  8.         company.addEmployee(new Employee("Imie1", "Nazwisko1"));
  9.         company.addEmployee(new Employee("Imie2", "Nazwisko2"));
  10.         company.addEmployee(new Employee("Imie3", "Nazwisko3"));
  11.         company.addEmployee(new Employee("Imie4", "Nazwisko4"));
  12.         company.addEmployee(new Employee("Imie5", "Nazwisko5"));
  13.         company.addEmployee(new Employee("Imie6", "Nazwisko6"));
  14.         company.addEmployee(new Employee("Imie7", "Nazwisko7"));
  15.         company.addEmployee(new Employee("Imie8", "Nazwisko8"));
  16.         company.addEmployee(new Employee("Imie9", "Nazwisko9"));
  17.         company.addEmployee(new Employee("Imie10", "Nazwisko10"));
  18.         company.printCompanyEmployees();
  19.         company.addEmployee(new Employee("Imie11", "Nazwisko11"));
  20.         company.deleteEmployee(new Employee("Imie5", "Nazwisko5"));
  21.         company.addEmployee(new Employee("Imie11", "Nazwisko11"));
  22.         company.printCompanyEmployees();
  23.         company.fireWholeCompany();
  24.         company.printCompanyEmployees();
  25.         company.deleteEmployee(new Employee("Imie11", "Nazwisko11"));
  26.         company.addEmployee(new Employee("Imie9", "Nazwisko9"));
  27.         company.addEmployee(new Employee("Imie10", "Nazwisko10"));
  28.         company.addEmployee(new Employee("Imie3", "Nazwisko3"));
  29.         company.addEmployee(new Employee("Imie4", "Nazwisko4"));
  30.         company.addEmployee(new Employee("Imie5", "Nazwisko5"));
  31.         company.addEmployee(new Employee("Imie6", "Nazwisko6"));
  32.         company.addEmployee(new Employee("Imie7", "Nazwisko7"));
  33.         company.printCompanyEmployees();
  34.     }
  35.  
  36. }
  37.  
  38.  
  39. /**
  40.  * Created by Scy on 03/12/2016.
  41.  */
  42. public class Employee {
  43.  
  44.     private String imie;
  45.     private String nazwisko;
  46.  
  47.     public Employee(String imie, String nazwisko)
  48.     {
  49.         this.imie=imie;
  50.         this.nazwisko=nazwisko;
  51.     }
  52.  
  53.     public boolean compareTo(Employee emp)
  54.     {
  55.         if(this.imie.compareToIgnoreCase(emp.getImie())==0 && this.nazwisko.compareToIgnoreCase(emp.getNazwisko())==0)
  56.         {
  57.             return true;
  58.         }
  59.         else return false;
  60.     }
  61.  
  62.     public String getImie() {
  63.         return imie;
  64.     }
  65.  
  66.     public String getNazwisko() {
  67.         return nazwisko;
  68.     }
  69.  
  70.     public String toString()
  71.     {
  72.         return this.imie+" "+this.nazwisko;
  73.     }
  74. }
  75.  
  76.  
  77. /**
  78.  * Created by Scy on 03/12/2016.
  79.  */
  80. public class Company {
  81.  
  82.     private int maxEmployees=10;
  83.     private int currentEmployeeCount=0;
  84.     private Employee[] company= new Employee[this.getMaxEmployees()];
  85.  
  86.     public int getMaxEmployees()
  87.     {
  88.         return this.maxEmployees;
  89.     }
  90.  
  91.     private boolean isFull()
  92.     {
  93.         if(this.currentEmployeeCount==this.maxEmployees)
  94.         {
  95.             return true;
  96.         }
  97.         else return false;
  98.     }
  99.  
  100.     private boolean isEmpty()
  101.     {
  102.         if(this.currentEmployeeCount==0)
  103.         {
  104.             return true;
  105.         }
  106.         else return false;
  107.     }
  108.  
  109.     public void addEmployee(Employee emp)
  110.     {
  111.         if(this.isFull())
  112.         {
  113.             System.out.println("Company is full.");
  114.         }
  115.         else
  116.         {
  117.             this.company[this.findFirstEmptySlot()] = emp;
  118.             this.currentEmployeeCount++;
  119.         }
  120.     }
  121.  
  122.     public void deleteEmployee(Employee emp)
  123.     {
  124.         int counter=0;
  125.         if(!this.isEmpty()) {
  126.             for (int i = 0; i < this.company.length; i++) {
  127.                 if (emp.compareTo(this.company[i])) {
  128.                     counter++;
  129.                     this.company[i] = null;
  130.                 }
  131.             }
  132.             System.out.println("Usunieto "+counter+" pracowników.");
  133.             this.currentEmployeeCount-=counter;
  134.         }
  135.         else System.out.println("Firma jest pusta");
  136.     }
  137.  
  138.     public void fireWholeCompany()
  139.     {
  140.         if(!this.isEmpty()) {
  141.             for (int i = 0; i < this.company.length; i++) {
  142.                 this.company[i] = null;
  143.             }
  144.             this.currentEmployeeCount = 0;
  145.         }
  146.     }
  147.  
  148.     private int findFirstEmptySlot()
  149.     {
  150.         int result=this.maxEmployees+1;
  151.         for(int i=0; i<this.company.length; i++)
  152.         {
  153.             if(company[i]==null)
  154.             {
  155.                 result=i;
  156.                 return result;
  157.             }
  158.         }
  159.         return result;
  160.     }
  161.  
  162.     public void printCompanyEmployees()
  163.     {
  164.         if(!this.isEmpty()) {
  165.             for (Employee emp : this.company) {
  166.                 if (emp != null)
  167.                     System.out.println(emp.toString());
  168.             }
  169.         }
  170.         else System.out.println("Firma jest pusta");
  171.     }
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement