Advertisement
Todorov99

Untitled

Nov 15th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.52 KB | None | 0 0
  1. package Agriculture_task;
  2.  
  3. public class Agriculture_Test {
  4.  
  5.     public static void main(String[] args) throws AreaException{
  6.  
  7.         try {
  8.             Area parcel = new Area(4200, "Fruits", 2800);
  9.             Area parcel1 = new Area(5000, "Vegetables", 3500);
  10.  
  11.             Worker worker = new Worker("Ivan", 24, 28.00, 36.00);
  12.  
  13.             System.out.println(worker.doWork(parcel) + " and for the second parcel \n" + worker.doWork(parcel1));
  14.  
  15.             System.out.println(parcel.toString() + "\n" + parcel1.toString());
  16.  
  17.             FocusedWorker focusedWorker = new FocusedWorker("Georgi", 66, 54, 78, "Specialist");
  18.             FocusedWorker focusedWorker1 = new FocusedWorker("Petko", 78, 120, 250, "Manager");
  19.  
  20.             System.out.println(focusedWorker.doWork(parcel) + " and for the second parcel \n" + focusedWorker.doWork(parcel1));
  21.  
  22.             System.out.println(parcel.toString() + "\n" + parcel1.toString());
  23.  
  24.         }catch (AreaException e){
  25.             System.out.println(e.getMessage());
  26.         }
  27.  
  28.     }
  29. }
  30.  
  31.  
  32. package Agriculture_task;
  33.  
  34. public class AreaException extends Exception {
  35.  
  36.     @Override
  37.     public String getMessage() {
  38.         return "Invalid amount";
  39.     }
  40. }
  41.  
  42.  
  43. package Agriculture_task;
  44.  
  45. public class FocusedWorker extends Worker {
  46.  
  47.     protected String specialCharacteristics;
  48.  
  49.     public FocusedWorker(String name, int id, double fruitsPower, double vegetablesPower, String specialCharacteristics) {
  50.         super(name, id, fruitsPower, vegetablesPower);
  51.         this.setSpecialCharacteristics(specialCharacteristics);
  52.     }
  53.  
  54.     public String getSpecialCharacteristics() {
  55.         return specialCharacteristics;
  56.     }
  57.  
  58.     public void setSpecialCharacteristics(String specialCharacteristics) {
  59.         this.specialCharacteristics = specialCharacteristics;
  60.     }
  61.  
  62.     @Override
  63.     public double doWork(Area object) throws AreaException {
  64.  
  65.         if(this.getSpecialCharacteristics().toLowerCase().equals("specialist")){
  66.             this.setFruitsPower(getFruitsPower() + (getFruitsPower() / 0.9));
  67.             this.setVegetablesPower(getVegetablesPower() + (getVegetablesPower() / 0.9));
  68.         }else {
  69.             this.setFruitsPower(getFruitsPower() + (getFruitsPower() / 0.6));
  70.             this.setVegetablesPower(getVegetablesPower() + (getVegetablesPower() / 0.6));
  71.         }
  72.  
  73.         if(object.getType().toLowerCase().equals("fruits")){
  74.  
  75.             if(this.getFruitsPower() > object.getAmount()){
  76.                 double amountOfKillograms = object.getAmount();
  77.                 object.setAmount(0);
  78.                 return amountOfKillograms;
  79.             }else {
  80.                 object.setAmount(object.getAmount() - this.getFruitsPower());
  81.             }
  82.  
  83.             return this.getFruitsPower();
  84.  
  85.         }else if(object.getType().toLowerCase().equals("vegetables")) {
  86.  
  87.             if (this.getFruitsPower() > object.getAmount()) {
  88.  
  89.                 double amountOfKillograms = object.getAmount();
  90.                 object.setAmount(0);
  91.  
  92.                 return amountOfKillograms;
  93.             } else{
  94.                 object.setAmount(object.getAmount() - this.getVegetablesPower());
  95.             }
  96.  
  97.             return this.getVegetablesPower();
  98.         }else {
  99.  
  100.             return 0.0;
  101.         }
  102.     }
  103.  
  104.    public double doWork(Area object, boolean stickWithNet) throws AreaException {
  105.         return super.doWork(object, stickWithNet);
  106.    }
  107.  
  108.  
  109.     @Override
  110.     public String toString() {
  111.         return "FocusedWorker{" +
  112.                 "specialCharacteristics='" + this.getSpecialCharacteristics() + '\'' +
  113.                 '}';
  114.     }
  115. }
  116.  
  117.  
  118. package Agriculture_task;
  119.  
  120. public class Worker {
  121.  
  122.     protected String name;
  123.     protected int id;
  124.     protected double fruitsPower; // in kilograms per day
  125.     protected double vegetablesPower;
  126.  
  127.     public Worker(String name, int id, double fruitsPower, double vegetablesPower) {
  128.         this.setName(name);
  129.         this.setId(id);
  130.         this.setFruitsPower(fruitsPower);
  131.         this.setVegetablesPower(vegetablesPower);
  132.     }
  133.  
  134.     public String getName() {
  135.         return name;
  136.     }
  137.  
  138.     public void setName(String name) {
  139.         this.name = name;
  140.     }
  141.  
  142.     public int getId() {
  143.         return id;
  144.     }
  145.  
  146.     public void setId(int id) {
  147.         this.id = id;
  148.     }
  149.  
  150.     public double getFruitsPower() {
  151.         return fruitsPower;
  152.     }
  153.  
  154.     public void setFruitsPower(double fruitsPower) {
  155.         this.fruitsPower = fruitsPower;
  156.     }
  157.  
  158.     public double getVegetablesPower() {
  159.         return vegetablesPower;
  160.     }
  161.  
  162.     public void setVegetablesPower(double vegetablesPower) {
  163.         this.vegetablesPower = vegetablesPower;
  164.     }
  165.  
  166.     public double doWork(Area object) throws AreaException {
  167.  
  168.         if(object.getType().toLowerCase().equals("fruits")){
  169.  
  170.             if(this.getFruitsPower() > object.getAmount()){
  171.                 double amountOfKillograms = object.getAmount();
  172.                 object.setAmount(0);
  173.                 return amountOfKillograms;
  174.             }else {
  175.                 object.setAmount(object.getAmount() - this.getFruitsPower());
  176.             }
  177.  
  178.             return this.getFruitsPower();
  179.  
  180.         }else if(object.getType().toLowerCase().equals("vegetables")) {
  181.  
  182.             if (this.getFruitsPower() > object.getAmount()) {
  183.  
  184.                 double amountOfKillograms = object.getAmount();
  185.                 object.setAmount(0);
  186.  
  187.                 return amountOfKillograms;
  188.             } else{
  189.                 object.setAmount(object.getAmount() - this.getVegetablesPower());
  190.         }
  191.  
  192.             return this.getVegetablesPower();
  193.         }else {
  194.  
  195.             return 0.0;
  196.         }
  197.     }
  198.  
  199.     public double doWork(Area object, boolean stickWithNet) throws AreaException {
  200.  
  201.         if(stickWithNet){
  202.             this.setFruitsPower(getFruitsPower() + (getFruitsPower() / 0.9));
  203.             this.setVegetablesPower(getVegetablesPower() + (getVegetablesPower() / 0.9));
  204.         }else {
  205.             this.setFruitsPower(getFruitsPower() + (getFruitsPower() / 0.6));
  206.             this.setVegetablesPower(getVegetablesPower() + (getVegetablesPower() / 0.6));
  207.         }
  208.  
  209.         if(object.getType().toLowerCase().equals("fruits")){
  210.  
  211.             if(this.getFruitsPower() > object.getAmount()){
  212.                 double amountOfKillograms = object.getAmount();
  213.                 object.setAmount(0);
  214.                 return amountOfKillograms;
  215.             }else {
  216.                 object.setAmount(object.getAmount() - this.getFruitsPower());
  217.             }
  218.  
  219.             return this.getFruitsPower();
  220.  
  221.         }else if(object.getType().toLowerCase().equals("vegetables")) {
  222.  
  223.             if (this.getFruitsPower() > object.getAmount()) {
  224.  
  225.                 double amountOfKillograms = object.getAmount();
  226.                 object.setAmount(0);
  227.  
  228.                 return amountOfKillograms;
  229.             } else{
  230.                 object.setAmount(object.getAmount() - this.getVegetablesPower());
  231.             }
  232.  
  233.             return this.getVegetablesPower();
  234.         }else {
  235.  
  236.             return 0.0;
  237.         }
  238.     }
  239.  
  240.     @Override
  241.     public String toString() {
  242.         return "Worker{" +
  243.                 "name='" + this.getName() + '\'' +
  244.                 ", id=" + this.getId() +
  245.                 '}';
  246.     }
  247. }
  248.  
  249.  
  250. package Agriculture_task;
  251.  
  252. public class Area {
  253.  
  254.     private double size;
  255.     private String type; //can be fruits or vegetables.
  256.     private double amount;
  257.  
  258.     public Area(double size, String type, double amount) throws AreaException{
  259.         this.setSize(size);
  260.         this.setType(type);
  261.         this.setAmount(amount);
  262.     }
  263.  
  264.     public void setSize(double size) throws AreaException{
  265.         if(size > 0) {
  266.             this.size = size;
  267.         }else{
  268.             throw new AreaException();
  269.         }
  270.     }
  271.  
  272.     public double getSize(){
  273.         return size;
  274.     }
  275.  
  276.     public void setType(String type){
  277.         this.type = type;
  278.     }
  279.  
  280.     public String getType(){
  281.         return type;
  282.     }
  283.  
  284.     public void setAmount(double amount) throws AreaException{
  285.         if(amount >=0 && amount < size * 100){
  286.             this.amount = amount;
  287.         }else{
  288.             throw new AreaException();
  289.         }
  290.     }
  291.  
  292.     public double getAmount(){
  293.         return amount;
  294.     }
  295.  
  296.  
  297.     @Override
  298.     public String toString() {
  299.         return "Area{" +
  300.                 "size=" + this.getSize() +
  301.                 ", type='" + this.getType() + '\'' +
  302.                 ", amount=" + this.getAmount() +
  303.                 '}';
  304.     }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement