Guest User

Untitled

a guest
Jan 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. package com.knowledgeblackbelt.students.ideynega.exercises;
  2.  
  3. public class Exercise3541 {
  4.     public static void main(String[] args) {
  5.         Horse first = Horse.getLazyHorse("First", 20, 1);
  6.         Horse second = Horse.getHardWorkingHorse("Second", 70);
  7.         for(int i = 0; i < 90; i++) {
  8.             first.addOneWorkingHour();
  9.             second.addOneWorkingHour();
  10.         }
  11.         System.out.println(first + "\n" + second);
  12.         first.resetWorkingHours();
  13.         second.resetWorkingHours();
  14.         System.out.println(first + "\n" + second);
  15.     }
  16. }
  17.  
  18. class Horse {
  19.     private String name;
  20.     private short maxWorkingHoursPerWeek;
  21.     private short hoursWorkedThisWeek;
  22.     private boolean isLazy;
  23.     private int genLazy;
  24.    
  25.     private static final short MAX_WORKING_HOURS_FOR_LAZY_HORSE = 60;
  26.     private static final short MAX_WORKING_HOURS_FOR_HARD_WORKING_HORSE = 80;
  27.    
  28.    
  29.     public static Horse getLazyHorse(String name, int maxWorkingHoursPerWeek, int genLazy) {
  30.         if(genLazy < 0) {
  31.             throw new IllegalArgumentException("genLazy cannot be negative");
  32.         }
  33.         if(maxWorkingHoursPerWeek > (MAX_WORKING_HOURS_FOR_LAZY_HORSE / (genLazy + 1))) {
  34.             throw new IllegalArgumentException("maxWorkingHoursPerWeek for lazy horse must not exceed " + MAX_WORKING_HOURS_FOR_LAZY_HORSE + " / (genLazy + 1)");
  35.         }
  36.         return new Horse(name, (short)maxWorkingHoursPerWeek, genLazy);
  37.     }
  38.     public static Horse getHardWorkingHorse(String name, int maxWorkingHoursPerWeek) {
  39.         if(maxWorkingHoursPerWeek > MAX_WORKING_HOURS_FOR_HARD_WORKING_HORSE) {
  40.             throw new IllegalArgumentException("maxWorkingHoursPerWeek for hard working horse must not exceed " + MAX_WORKING_HOURS_FOR_HARD_WORKING_HORSE);
  41.         }
  42.         return new Horse(name, (short)maxWorkingHoursPerWeek);
  43.     }
  44.     //construct for lazy horse - with genLazy argument
  45.     private Horse(String name, short maxWorkingHoursPerWeek, int genLazy) {
  46.         this.isLazy = true;
  47.         this.name = name;
  48.         this.maxWorkingHoursPerWeek = maxWorkingHoursPerWeek;
  49.         this.genLazy = genLazy;
  50.     }
  51.     //constructor for hard working horse - no genLazy argument
  52.     private Horse(String name, short maxWorkingHoursPerWeek) {
  53.         this.isLazy = false;
  54.         this.name = name;
  55.         this.maxWorkingHoursPerWeek = maxWorkingHoursPerWeek;
  56.     }
  57.    
  58.     public void addOneWorkingHour() {
  59.         if(hoursWorkedThisWeek < maxWorkingHoursPerWeek) {
  60.             this.hoursWorkedThisWeek += 1;
  61.         } else {
  62.             System.err.println("maxWorkingHoursPerWeek reached (" + maxWorkingHoursPerWeek + "), horse " + name + " won't work this week anymore");
  63.         }
  64.     }
  65.    
  66.     public void resetWorkingHours() {
  67.         this.hoursWorkedThisWeek = 0;
  68.     }
  69.    
  70.     @Override
  71.     public String toString() {
  72.         return "Name: \"" + name + "\" lazy: " + (isLazy ? "yes genLazy: " + genLazy : "no") + " max hours / week = " + maxWorkingHoursPerWeek + " actual hours worked this week = " + hoursWorkedThisWeek;
  73.     }
  74. }
  75. /*
  76. Horses are more and more lazy as their parents teach them good lazyness.
  77.  
  78. Lazy horses have an extra field: genLaz - the generation of laziness.
  79.  
  80. 1 means that the lazy horse's parents were lazy most of their lives.
  81.  
  82. 2 means that the grandparents were too, and so on.
  83.  
  84. In fact, a lazy horse can work up to 60 hours/week, if genLaz = 0. If genLaz is 1, then it's half. If genLaz is 2, then it's third, and so on.
  85.  
  86. The genLaz attribute of the Horse class is only meaningful for lazy horses. We could split the Horse class into 2 childs (lazy with the field, and hard workers without the genLaz field), but it requires inheritance that is covered later. For now, we accept that genLaz is useless for hardworker horses.
  87.  
  88. Add a genLaz field that cannot be negative and adapt the possible values for working hours per week.
  89.  
  90. Attention, the genLaz notion does not exists for hard workers => raise an error if a program tries to read/write the genLaz value for a hard worker horse, it's a bug.
  91. */
Add Comment
Please, Sign In to add comment