Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Agriculture_task;
- public class Agriculture_Test {
- public static void main(String[] args) throws AreaException{
- try {
- Area parcel = new Area(4200, "Fruits", 2800);
- Area parcel1 = new Area(5000, "Vegetables", 3500);
- Worker worker = new Worker("Ivan", 24, 28.00, 36.00);
- System.out.println(worker.doWork(parcel) + " and for the second parcel \n" + worker.doWork(parcel1));
- System.out.println(parcel.toString() + "\n" + parcel1.toString());
- FocusedWorker focusedWorker = new FocusedWorker("Georgi", 66, 54, 78, "Specialist");
- FocusedWorker focusedWorker1 = new FocusedWorker("Petko", 78, 120, 250, "Manager");
- System.out.println(focusedWorker.doWork(parcel) + " and for the second parcel \n" + focusedWorker.doWork(parcel1));
- System.out.println(parcel.toString() + "\n" + parcel1.toString());
- }catch (AreaException e){
- System.out.println(e.getMessage());
- }
- }
- }
- package Agriculture_task;
- public class AreaException extends Exception {
- @Override
- public String getMessage() {
- return "Invalid amount";
- }
- }
- package Agriculture_task;
- public class FocusedWorker extends Worker {
- protected String specialCharacteristics;
- public FocusedWorker(String name, int id, double fruitsPower, double vegetablesPower, String specialCharacteristics) {
- super(name, id, fruitsPower, vegetablesPower);
- this.setSpecialCharacteristics(specialCharacteristics);
- }
- public String getSpecialCharacteristics() {
- return specialCharacteristics;
- }
- public void setSpecialCharacteristics(String specialCharacteristics) {
- this.specialCharacteristics = specialCharacteristics;
- }
- @Override
- public double doWork(Area object) throws AreaException {
- if(this.getSpecialCharacteristics().toLowerCase().equals("specialist")){
- this.setFruitsPower(getFruitsPower() + (getFruitsPower() / 0.9));
- this.setVegetablesPower(getVegetablesPower() + (getVegetablesPower() / 0.9));
- }else {
- this.setFruitsPower(getFruitsPower() + (getFruitsPower() / 0.6));
- this.setVegetablesPower(getVegetablesPower() + (getVegetablesPower() / 0.6));
- }
- if(object.getType().toLowerCase().equals("fruits")){
- if(this.getFruitsPower() > object.getAmount()){
- double amountOfKillograms = object.getAmount();
- object.setAmount(0);
- return amountOfKillograms;
- }else {
- object.setAmount(object.getAmount() - this.getFruitsPower());
- }
- return this.getFruitsPower();
- }else if(object.getType().toLowerCase().equals("vegetables")) {
- if (this.getFruitsPower() > object.getAmount()) {
- double amountOfKillograms = object.getAmount();
- object.setAmount(0);
- return amountOfKillograms;
- } else{
- object.setAmount(object.getAmount() - this.getVegetablesPower());
- }
- return this.getVegetablesPower();
- }else {
- return 0.0;
- }
- }
- public double doWork(Area object, boolean stickWithNet) throws AreaException {
- return super.doWork(object, stickWithNet);
- }
- @Override
- public String toString() {
- return "FocusedWorker{" +
- "specialCharacteristics='" + this.getSpecialCharacteristics() + '\'' +
- '}';
- }
- }
- package Agriculture_task;
- public class Worker {
- protected String name;
- protected int id;
- protected double fruitsPower; // in kilograms per day
- protected double vegetablesPower;
- public Worker(String name, int id, double fruitsPower, double vegetablesPower) {
- this.setName(name);
- this.setId(id);
- this.setFruitsPower(fruitsPower);
- this.setVegetablesPower(vegetablesPower);
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public double getFruitsPower() {
- return fruitsPower;
- }
- public void setFruitsPower(double fruitsPower) {
- this.fruitsPower = fruitsPower;
- }
- public double getVegetablesPower() {
- return vegetablesPower;
- }
- public void setVegetablesPower(double vegetablesPower) {
- this.vegetablesPower = vegetablesPower;
- }
- public double doWork(Area object) throws AreaException {
- if(object.getType().toLowerCase().equals("fruits")){
- if(this.getFruitsPower() > object.getAmount()){
- double amountOfKillograms = object.getAmount();
- object.setAmount(0);
- return amountOfKillograms;
- }else {
- object.setAmount(object.getAmount() - this.getFruitsPower());
- }
- return this.getFruitsPower();
- }else if(object.getType().toLowerCase().equals("vegetables")) {
- if (this.getFruitsPower() > object.getAmount()) {
- double amountOfKillograms = object.getAmount();
- object.setAmount(0);
- return amountOfKillograms;
- } else{
- object.setAmount(object.getAmount() - this.getVegetablesPower());
- }
- return this.getVegetablesPower();
- }else {
- return 0.0;
- }
- }
- public double doWork(Area object, boolean stickWithNet) throws AreaException {
- if(stickWithNet){
- this.setFruitsPower(getFruitsPower() + (getFruitsPower() / 0.9));
- this.setVegetablesPower(getVegetablesPower() + (getVegetablesPower() / 0.9));
- }else {
- this.setFruitsPower(getFruitsPower() + (getFruitsPower() / 0.6));
- this.setVegetablesPower(getVegetablesPower() + (getVegetablesPower() / 0.6));
- }
- if(object.getType().toLowerCase().equals("fruits")){
- if(this.getFruitsPower() > object.getAmount()){
- double amountOfKillograms = object.getAmount();
- object.setAmount(0);
- return amountOfKillograms;
- }else {
- object.setAmount(object.getAmount() - this.getFruitsPower());
- }
- return this.getFruitsPower();
- }else if(object.getType().toLowerCase().equals("vegetables")) {
- if (this.getFruitsPower() > object.getAmount()) {
- double amountOfKillograms = object.getAmount();
- object.setAmount(0);
- return amountOfKillograms;
- } else{
- object.setAmount(object.getAmount() - this.getVegetablesPower());
- }
- return this.getVegetablesPower();
- }else {
- return 0.0;
- }
- }
- @Override
- public String toString() {
- return "Worker{" +
- "name='" + this.getName() + '\'' +
- ", id=" + this.getId() +
- '}';
- }
- }
- package Agriculture_task;
- public class Area {
- private double size;
- private String type; //can be fruits or vegetables.
- private double amount;
- public Area(double size, String type, double amount) throws AreaException{
- this.setSize(size);
- this.setType(type);
- this.setAmount(amount);
- }
- public void setSize(double size) throws AreaException{
- if(size > 0) {
- this.size = size;
- }else{
- throw new AreaException();
- }
- }
- public double getSize(){
- return size;
- }
- public void setType(String type){
- this.type = type;
- }
- public String getType(){
- return type;
- }
- public void setAmount(double amount) throws AreaException{
- if(amount >=0 && amount < size * 100){
- this.amount = amount;
- }else{
- throw new AreaException();
- }
- }
- public double getAmount(){
- return amount;
- }
- @Override
- public String toString() {
- return "Area{" +
- "size=" + this.getSize() +
- ", type='" + this.getType() + '\'' +
- ", amount=" + this.getAmount() +
- '}';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement