Untitled
unknown
plain_text
3 years ago
5.4 kB
4
Indexable
Item.java public class Item { // Private data members private int id; public static int numberOfItems; // Parameterized constructor public Item(int id) { this.id = id; numberOfItems++; } } Payroll.java public class Payroll { // Private data members private int workHour; private int itemCount; // Parameterized constructor public Payroll(int workHour, int itemCount) { this.workHour = workHour; this.itemCount = itemCount; } // Public method to calculate salary public int calculateSalary() { int workingHourSalary = workHour * 3; int itemsProducedSalary = itemCount * 2; return workingHourSalary + itemsProducedSalary; } // String representation of the object @Override public String toString() { return "The work hour is " + workHour + " and the produced item count is " + itemCount + "."; } } Employee.java public class Employee { // Private data members private int id; private String name; private String surname; private int workHour; private int speed; private Item[] items; private Payroll payroll; // Parameterized constructor public Employee(int id, String name, String surname, int workHour, int speed) { this.id = id; this.name = name; this.surname = surname; this.workHour = workHour; this.speed = speed; } // Public method to generate items public Item[] startShift() { int numberOfItems = speed * workHour; items = new Item[numberOfItems]; for(int i = 0; i < numberOfItems; i++) { items[i] = new Item(1 + (int)(100 * Math.random())); } return items; } // Public method to calculate payroll public Payroll endShift() { int numberOfItems = speed * workHour; payroll = new Payroll(workHour, numberOfItems); return payroll; } // String representation of the object @Override public String toString() { return "This is the employee with id " + id + " speed " + speed + ". The work hour is " + workHour + " and the produced item count is " + speed * workHour; } // Public getter for private data member id public int getId() { return id; } } Storage.java import java.util.Arrays; public class Storage { // Private data member private int capacity; private Item[] items; private int numberOfItems; // Parameterized constructor public Storage(int capacity) { this.capacity = capacity; this.numberOfItems = 0; this.items = new Item[this.capacity]; } // Public method to add item to items array public void addItem(Item item) { items[numberOfItems] = item; numberOfItems++; } // Public method to get number of items in storage public int getNumberOfItems() { return numberOfItems; } } Factory.java import java.util.Arrays; public class Factory { // Private data members private String name; private Employee[] employees; private Storage storage; private Payroll[] payrolls; private double itemPrice; // Parameterized constructor public Factory(String name, Storage storage, double itemPrice) { this.name = name; this.storage = storage; this.itemPrice = itemPrice; } // Public method to calculate revenue public double getRevenue() { return storage.getNumberOfItems()* itemPrice; } // Public method to calculate salary public double getPaidSalaries() { double salary = 0; for(Payroll payroll: payrolls) { salary += payroll.calculateSalary(); } return salary; } // Public method to add employee public void addEmployee(Employee employee) { if(employees == null) { employees = new Employee[1]; employees[0] = employee; } else { employees = Arrays.copyOf(employees, employees.length + 1); } Item[] items = employees[employees.length - 1].startShift(); for(Item item: items) { storage.addItem(item); } } // Public method to remove employee public Employee removeEmployee(int id) { if(employees.length == 0) { System.out.println("There are no employees!"); return null; } int employeeIndex = -1; for(int i = 0; i < employees.length; i++) { if(employees[i].getId() == id) { employeeIndex = i; break; } } if(employeeIndex == -1) { System.out.println("Employee does not exist!"); return null; } Employee removedEmployee = employees[employeeIndex]; addPayroll(removedEmployee.endShift()); System.arraycopy(employees, employeeIndex + 1, employees, employeeIndex, employees.length - employeeIndex - 1); return removedEmployee; } // Private method to add payroll private void addPayroll(Payroll payroll) { if(payrolls == null) { payrolls = new Payroll[1]; } else { payrolls = Arrays.copyOf(payrolls, payrolls.length + 1); } payrolls[payrolls.length - 1] = payroll; } }
Editor is loading...