fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. abstract class Hen
  5. {
  6. String descr()
  7. {
  8. return "Chicken";
  9. }
  10. abstract int eggsPerMonth();
  11. }
  12.  
  13. class RusHen extends Hen
  14. {
  15. int eggsPerMonth()
  16. {
  17. return 5;
  18. }
  19. private String country="Russia";
  20. String descr()
  21. {
  22. String res=super.descr();
  23. res+=(" Country - "+country+" Eggs per month- "+eggsPerMonth());
  24. return res;
  25. }
  26. }
  27.  
  28. class UkrHen extends Hen
  29. {
  30. int eggsPerMonth()
  31. {
  32. return 8;
  33. }
  34. private String country="Ukraine";
  35. String descr()
  36. {
  37. String res=super.descr();
  38. res+=(" Country - "+country+" Eggs per month- "+eggsPerMonth());
  39. return res;
  40. }
  41. }
  42.  
  43. class MldHen extends Hen
  44. {
  45. int eggsPerMonth()
  46. {
  47. return 4;
  48. }
  49. private String country="Moldova";
  50. String descr()
  51. {
  52. String res=super.descr();
  53. res+=(" Country - "+country+" Eggs per month- "+eggsPerMonth());
  54. return res;
  55. }
  56. }
  57.  
  58. class BelHen extends Hen
  59. {
  60. int eggsPerMonth()
  61. {
  62. return 3;
  63. }
  64. private String country="Belarus";
  65. String descr()
  66. {
  67. String res=super.descr();
  68. res+=(" Country - "+country+" Eggs per month- "+eggsPerMonth());
  69. return res;
  70. }
  71. }
  72. enum countries
  73. {RUSSIA, UKRAINE, MOLDOVA, BELARUS}
  74.  
  75. class HenFactory
  76. {
  77. static Hen getHen(countries country)
  78. {
  79. switch (country)
  80. {
  81. case RUSSIA: return new RusHen();
  82. case UKRAINE: return new UkrHen();
  83. case MOLDOVA: return new MldHen();
  84. case BELARUS: return new BelHen();
  85. }
  86. return null;
  87. }
  88. }
  89.  
  90. /* Name of the class has to be "Main" only if the class is public. */
  91. class Ideone
  92. {
  93. public static void main (String[] args) throws java.lang.Exception
  94. {
  95. for (countries country:countries.values())
  96. System.out.println(HenFactory.getHen(country).descr());
  97. }
  98. }
  99.  
Success #stdin #stdout 0.16s 37260KB
stdin
Standard input is empty
stdout
Chicken Country - Russia Eggs per month- 5
Chicken Country - Ukraine Eggs per month- 8
Chicken Country - Moldova Eggs per month- 4
Chicken Country - Belarus Eggs per month- 3