Guest User

Untitled

a guest
Jan 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 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 int maxWorkingHoursPerWeek;
  21.     private int hoursWorkedThisWeek;
  22.     private boolean isLazy;
  23.     private int genLazy;
  24.    
  25.     private static final int MAX_WORKING_HOURS_FOR_LAZY_HORSE = 60;
  26.     private static final int MAX_WORKING_HOURS_FOR_HARD_WORKING_HORSE = 80;
  27.    
  28.     public static Horse getLazyHorse(String name, int maxWorkingHoursPerWeek, int genLazy) {
  29.         if(genLazy < 0) {
  30.             throw new IllegalArgumentException("genLazy cannot be negative");
  31.         }
  32.         if(maxWorkingHoursPerWeek > (MAX_WORKING_HOURS_FOR_LAZY_HORSE / (genLazy + 1))) {
  33.             throw new IllegalArgumentException("maxWorkingHoursPerWeek for lazy horse must not exceed " + MAX_WORKING_HOURS_FOR_LAZY_HORSE + " / (genLazy + 1)");
  34.         }
  35.         return new Horse(true, name, maxWorkingHoursPerWeek, genLazy);
  36.     }
  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(false, name, maxWorkingHoursPerWeek, 0);
  43.     }
  44.    
  45.     private Horse(boolean isLazy, String name, int maxWorkingHoursPerWeek, int genLazy) {
  46.         this.isLazy = true;
  47.         this.name = name;
  48.         this.maxWorkingHoursPerWeek = maxWorkingHoursPerWeek;
  49.         this.genLazy = genLazy;
  50.     }
  51.    
  52.     public void addOneWorkingHour() {
  53.         if(hoursWorkedThisWeek < maxWorkingHoursPerWeek) {
  54.             this.hoursWorkedThisWeek += 1;
  55.         } else {
  56.             System.err.println("maxWorkingHoursPerWeek reached (" + maxWorkingHoursPerWeek + "), horse " + name + " won't work this week anymore");
  57.         }
  58.     }
  59.    
  60.     public void resetWorkingHours() {
  61.         this.hoursWorkedThisWeek = 0;
  62.     }
  63.    
  64.     @Override
  65.     public String toString() {
  66.         return "Name: \"" + name + "\" lazy: " + (isLazy ? "yes genLazy: " + genLazy : "no") + " max hours / week = " + maxWorkingHoursPerWeek + " actual hours worked this week = " + hoursWorkedThisWeek;
  67.     }
  68. }
  69. /*
  70. Horses are more and more lazy as their parents teach them good lazyness.
  71.  
  72. Lazy horses have an extra field: genLaz - the generation of laziness.
  73.  
  74. 1 means that the lazy horse's parents were lazy most of their lives.
  75.  
  76. 2 means that the grandparents were too, and so on.
  77.  
  78. 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.
  79.  
  80. 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.
  81.  
  82. Add a genLaz field that cannot be negative and adapt the possible values for working hours per week.
  83.  
  84. 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.
  85. */
Add Comment
Please, Sign In to add comment