fork download
  1. class Hair
  2. {
  3. public static void main(String[] args)
  4. {
  5. Hair hair = new Hair();
  6. hair.invokeMethod(100);
  7. hair.invokeMethod(200);
  8. hair.setMethod(Hair::datsumou);
  9. hair.invokeMethod(500);
  10. hair.invokeMethod(800);
  11. hair.setMethod(Hair::shokumou);
  12. hair.invokeMethod(1);
  13. }
  14.  
  15. private int count = 1000;
  16. private Method method = Hair::shokumou;
  17.  
  18. public void invokeMethod(int num)
  19. {
  20. method.invoke(this, num);
  21. }
  22.  
  23. public void setMethod(Method method)
  24. {
  25. this.method = method;
  26. }
  27.  
  28. private static void shokumou(Hair hair, int num)
  29. {
  30. if (num <= 0) throw new IllegalArgumentException();
  31. hair.count += num;
  32. System.out.printf("%d本植毛しました、現在の髪の毛%d本%n", num, hair.count);
  33. }
  34.  
  35. private static void datsumou(Hair hair, int num)
  36. {
  37. if (num <= 0 || num > hair.count) throw new IllegalArgumentException();
  38. hair.count -= num;
  39. System.out.printf("%d本脱毛しました、現在の髪の毛%d本%n", num, hair.count);
  40. if (hair.count == 0)
  41. {
  42. System.out.println("つるっぱげです。");
  43. }
  44. }
  45. }
  46.  
  47. interface Method
  48. {
  49. void invoke(Hair hair, int num);
  50. }
  51.  
Success #stdin #stdout 0.14s 4386816KB
stdin
Standard input is empty
stdout
100本植毛しました、現在の髪の毛1100本
200本植毛しました、現在の髪の毛1300本
500本脱毛しました、現在の髪の毛800本
800本脱毛しました、現在の髪の毛0本
つるっぱげです。
1本植毛しました、現在の髪の毛1本