Last active
January 25, 2018 08:43
-
-
Save Brutt/bb6adf968987deddbd6f823fad6f9f74 to your computer and use it in GitHub Desktop.
25.01.2018
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.NoSuchElementException; | |
| public class ArrayQueue extends Queue { | |
| private Object[] array = new Object[5]; | |
| private int pointStart; | |
| private void addCapacity() { | |
| Object[] newArray = new Object[(int) (1.5 * array.length)]; | |
| System.arraycopy(array, 0, newArray, 0, size); | |
| array = newArray; | |
| } | |
| @Override | |
| public void enqueue(Object value) { | |
| if (array.length==size) { | |
| addCapacity(); | |
| } | |
| array[size] = value; | |
| size++; | |
| } | |
| @Override | |
| public Object dequeue() { | |
| if (pointStart == size) { | |
| throw new NoSuchElementException("Queue is empty"); | |
| } | |
| Object firstQueueObject = array[pointStart]; | |
| pointStart++; | |
| return firstQueueObject; | |
| } | |
| @Override | |
| public Object peek() { | |
| if (pointStart == size) { | |
| throw new NoSuchElementException("Queue is empty"); | |
| } | |
| return array[pointStart]; | |
| } | |
| @Override | |
| public boolean contains(Object value) { | |
| for (int i = pointStart; i < size; i++) { | |
| if(array[i].equals(value)){ | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| @Override | |
| public int getSize() { | |
| return size-pointStart; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Cleaner extends Employee{ | |
| private double rate; | |
| private int workedDays; | |
| @Override | |
| public double calculateSalary() { | |
| return salary + rate * workedDays; | |
| } | |
| public void setRate(double rate){ | |
| this.rate = rate; | |
| } | |
| public void setWorkedDays(int workedDays){ | |
| this.workedDays = workedDays; | |
| } | |
| @Override | |
| public String toString() { | |
| String result = super.toString(); | |
| return result + " rate = " + rate + " workedDays = " + workedDays; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Random; | |
| public class Developer extends Employee { | |
| private int fixedBugs; | |
| private double rate; | |
| @Override | |
| public double calculateSalary() { | |
| Random random = new Random(); | |
| return salary + fixedBugs * rate + (random.nextBoolean()?2:0); | |
| } | |
| public void setFixedBugs(int fixedBugs){ | |
| this.fixedBugs = fixedBugs; | |
| } | |
| public void setRate(double rate){ | |
| this.rate = rate; | |
| } | |
| @Override | |
| public String toString() { | |
| String result = super.toString(); | |
| return result + "fixedBugs = " + fixedBugs; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // id, name, salary, gender | |
| public abstract class Employee { | |
| protected long id; | |
| protected String name; | |
| protected int age; | |
| protected double salary; | |
| protected char gender; | |
| public Employee() { | |
| } | |
| public double calculateSalary(){ | |
| return salary; | |
| }; | |
| public Employee(long id, String name, double salary, char gender, int age) { | |
| this.id = id; | |
| this.name = name; | |
| this.salary = salary; | |
| this.gender = gender; | |
| this.age = age; | |
| } | |
| public String toString() { | |
| String result = "id = " + id + " name = " + name; | |
| result += " salary =" + salary + " gender = " + gender + " age = " + age; | |
| return result; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Random; | |
| class EmployeeService2 { | |
| Employee[] users; | |
| void generateEmployees(int size) { | |
| String[] maleNames = {"Anatoliy","Oleg","Vladimir","Yuriy"}; | |
| String[] femaleNames = {"Anna","Olha","Vlada","Yulia"}; | |
| Employee[] positions = {new Developer(), new Cleaner(), new Manager()}; | |
| Random random = new Random(); | |
| users = new Employee[size]; | |
| for (int i = 0; i < users.length; i++) { | |
| users[i] = positions[random.nextInt(3)]; | |
| users[i].age = random.nextInt(65-18+1)+18; | |
| users[i].gender = users[i].age % 2==0 ? 'M' : 'F'; | |
| users[i].salary = random.nextInt(3000-500+1)+500; | |
| users[i].id = i+1; | |
| String personName = maleNames[random.nextInt(maleNames.length)]; | |
| if (users[i].gender == 'F') { | |
| personName = femaleNames[random.nextInt(femaleNames.length)]; | |
| } | |
| users[i].name = personName; | |
| if (users[i].getClass() == Developer.class){ | |
| ((Developer)users[i]).setFixedBugs(10); | |
| ((Developer)users[i]).setRate(0.5); | |
| } | |
| else if (users[i].getClass() == Cleaner.class){ | |
| ((Cleaner)users[i]).setWorkedDays(22); | |
| ((Cleaner)users[i]).setRate(0.5); | |
| } | |
| } | |
| } | |
| // print all users in array | |
| void print() { | |
| for (int i = 0; i < users.length; i++) { | |
| System.out.println(users[i].getClass() + ": "+ users[i].toString()); | |
| } | |
| } | |
| // ASC | |
| void sortByName() { | |
| } | |
| // if names are equal, compare by salary | |
| void sortByNameAndSalary() { | |
| } | |
| // return sum of all salaries | |
| double calculateSalary() { | |
| double result = 0.0; | |
| for (Employee user : users) { | |
| result += user.calculateSalary(); | |
| } | |
| return result; | |
| } | |
| Employee[] searchByName(String name) { | |
| return null; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.NoSuchElementException; | |
| public class LinkedQueue extends Queue{ | |
| private Node head; | |
| @Override | |
| public int getSize() { | |
| return size; | |
| } | |
| private Node getLastNode() { | |
| Node temp = head; | |
| while (temp.next != null) { | |
| temp = temp.next; | |
| } | |
| return temp; | |
| } | |
| @Override | |
| public void enqueue(Object value) { | |
| if (head == null){ | |
| head = new Node(); | |
| head.value = value; | |
| } | |
| else { | |
| Node lastNode = getLastNode(); | |
| lastNode.next = new Node(value); | |
| } | |
| size++; | |
| } | |
| @Override | |
| public Object dequeue() { | |
| if (size == 0) { | |
| throw new NoSuchElementException("Queue is empty"); | |
| } | |
| Node tempNode = head; | |
| head = head.next; | |
| size--; | |
| return tempNode.value; | |
| } | |
| @Override | |
| public Object peek() { | |
| if (size == 0) { | |
| throw new NoSuchElementException("Queue is empty"); | |
| } | |
| return head.value; | |
| } | |
| @Override | |
| public boolean contains(Object value) { | |
| if (size == 0) { | |
| throw new NoSuchElementException("Queue is empty"); | |
| } | |
| Node temp = head; | |
| while (temp.next != null) { | |
| if (temp.value == value){ | |
| return true; | |
| } | |
| temp = temp.next; | |
| } | |
| return false; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Manager extends Employee { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public abstract class Queue { | |
| protected int size; | |
| public abstract int getSize(); | |
| public abstract void enqueue(Object value); | |
| public abstract Object dequeue(); | |
| public abstract Object peek(); | |
| public abstract boolean contains(Object value); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestEmployee { | |
| public static void main(String[] args) { | |
| /*Cleaner cleaner = new Cleaner(); | |
| cleaner.salary = 100; | |
| cleaner.setRate(0.25); | |
| cleaner.setWorkedDays(20); | |
| System.out.println(cleaner.calculateSalary());*/ | |
| EmployeeService2 employeeService2 = new EmployeeService2(); | |
| employeeService2.generateEmployees(10); | |
| employeeService2.print(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class TestQueue { | |
| public static void main(String[] args) { | |
| /*ArrayQueue arrayQueue = new ArrayQueue(); | |
| arrayQueue.enqueue("Qqqq"); | |
| arrayQueue.enqueue(1); | |
| arrayQueue.enqueue("aa"); | |
| arrayQueue.enqueue('C'); | |
| arrayQueue.enqueue("Qqqq1"); | |
| arrayQueue.enqueue(12); | |
| arrayQueue.enqueue("aa3"); | |
| arrayQueue.enqueue('D'); | |
| System.out.println(arrayQueue.contains(12)); | |
| System.out.println(arrayQueue.dequeue()); | |
| System.out.println(arrayQueue.dequeue()); | |
| System.out.println(arrayQueue.dequeue()); | |
| System.out.println(arrayQueue.dequeue()); | |
| System.out.println(arrayQueue.contains('C')); | |
| System.out.println(arrayQueue.peek()); | |
| System.out.println(arrayQueue.peek()); | |
| System.out.println(arrayQueue.getSize()); | |
| System.out.println(arrayQueue.dequeue()); | |
| System.out.println(arrayQueue.dequeue()); | |
| System.out.println("Size: "+arrayQueue.getSize()); | |
| arrayQueue.enqueue("qaz"); | |
| System.out.println("Size: "+arrayQueue.getSize()); | |
| System.out.println(arrayQueue.peek()); | |
| System.out.println(arrayQueue.dequeue()); | |
| System.out.println("Size: "+arrayQueue.getSize());*/ | |
| LinkedQueue linkedQueue = new LinkedQueue(); | |
| linkedQueue.enqueue("first"); | |
| linkedQueue.enqueue("second"); | |
| linkedQueue.enqueue("third"); | |
| System.out.println("contains: " + linkedQueue.contains("first")); | |
| System.out.println("el peek: " + linkedQueue.peek()); | |
| System.out.println("size: " + linkedQueue.getSize()); | |
| System.out.println("el dequeue: " + linkedQueue.dequeue()); | |
| System.out.println("el dequeue: " + linkedQueue.dequeue()); | |
| System.out.println("contains: " + linkedQueue.contains("first")); | |
| System.out.println("size: " + linkedQueue.getSize()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment