Advertisement
Guest User

Untitled

a guest
Jul 6th, 2016
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.76 KB | None | 0 0
  1. import java.text.DecimalFormat;
  2. import java.util.Scanner;
  3.  
  4. /**
  5. * Created by iwano on 7/6/2016.
  6. */
  7. public class WildFarm {
  8. public static void main(String[] args) {
  9. Scanner scan = new Scanner(System.in);
  10. String input = scan.nextLine();
  11. while (!input.equals("End")) {
  12. String[] data = input.split("\\s+");
  13. String[] foodParams = scan.nextLine().split("\\s+");
  14. Food food = produceFood(foodParams);
  15. switch (data[0]) {
  16. case "Cat":
  17. Cat cat = new Cat(data[1], Double.parseDouble(data[2]), data[3], data[4]);
  18. cat.makeSound();
  19. try {
  20. cat.eat(food);
  21. } catch (IllegalArgumentException ex) {
  22. System.out.println(ex.getMessage());
  23. }
  24.  
  25. System.out.println(cat.toString());
  26. break;
  27. case "Tiger":
  28. Tiger tiger = new Tiger(data[1], Double.parseDouble(data[2]), data[3]);
  29. tiger.makeSound();
  30. try {
  31. tiger.eat(food);
  32. } catch (IllegalArgumentException ex) {
  33. System.out.println(ex.getMessage());
  34. }
  35.  
  36. System.out.println(tiger.toString());
  37. break;
  38. case "Zebra":
  39. Zebra zebra = new Zebra(data[1], Double.parseDouble(data[2]), data[3]);
  40. zebra.makeSound();
  41. try {
  42. zebra.eat(food);
  43. } catch (IllegalArgumentException ex) {
  44. System.out.println(ex.getMessage());
  45. }
  46.  
  47. System.out.println(zebra.toString());
  48. break;
  49. case "Mouse":
  50. Mouse mouse = new Mouse(data[1], Double.parseDouble(data[2]), data[3]);
  51. mouse.makeSound();
  52. try {
  53. mouse.eat(food);
  54. } catch (IllegalArgumentException ex) {
  55. System.out.println(ex.getMessage());
  56. }
  57.  
  58. System.out.println(mouse.toString());
  59. break;
  60. }
  61. input = scan.nextLine();
  62. }
  63. }
  64.  
  65. public static Food produceFood(String[] foodParams) {
  66. Food food;
  67.  
  68. if(foodParams[0].equals("Vegetable")) {
  69. food = new Vegetable(Integer.parseInt(foodParams[1]));
  70. } else {
  71. food = new Meat(Integer.parseInt(foodParams[1]));
  72. }
  73.  
  74. return food;
  75. }
  76. }
  77.  
  78.  
  79. abstract class Food {
  80. private int quantity;
  81.  
  82. protected Food(int quantity) {
  83. this.quantity = quantity;
  84. }
  85.  
  86. public int getQuantity() {
  87. return quantity;
  88. }
  89. }
  90.  
  91. class Vegetable extends Food {
  92.  
  93. protected Vegetable(int quantity) {
  94. super(quantity);
  95. }
  96. }
  97.  
  98. class Meat extends Food {
  99.  
  100. protected Meat(int quantity) {
  101. super(quantity);
  102. }
  103. }
  104.  
  105. abstract class Animal {
  106. private String name;
  107. private double weight;
  108. private int foodEaten;
  109.  
  110. public Animal(String name, double weight) {
  111. this.name = name;
  112. this.weight = weight;
  113. }
  114.  
  115. public void setFoodEaten(int foodEaten) {
  116. this.foodEaten = foodEaten;
  117. }
  118.  
  119. public int getFoodEaten() {
  120. return foodEaten;
  121. }
  122.  
  123. public String getName() {
  124. return name;
  125. }
  126.  
  127.  
  128. public double getWeight() {
  129. return weight;
  130. }
  131.  
  132. public abstract void makeSound();
  133.  
  134. public abstract void eat(Food food);
  135.  
  136.  
  137. }
  138.  
  139. abstract class Mammal extends Animal {
  140. private String livingRegion;
  141.  
  142. public Mammal(String name, double weight) {
  143. super(name, weight);
  144. }
  145.  
  146. public Mammal(String name, double weight, String livingRegion) {
  147. super(name, weight);
  148. this.livingRegion = livingRegion;
  149. }
  150.  
  151. public String getLivingRegion() {
  152. return livingRegion;
  153. }
  154. }
  155.  
  156. abstract class Felime extends Mammal {
  157.  
  158. public Felime(String name, double weight) {
  159. super(name, weight);
  160. }
  161.  
  162. public Felime(String name, double weight, String livingRegion) {
  163. super(name, weight, livingRegion);
  164. }
  165. }
  166.  
  167. class Cat extends Felime {
  168. private String breed;
  169.  
  170. public Cat(String name, double weight,String livingRegion, String breed) {
  171. super(name, weight, livingRegion);
  172. this.breed = breed;
  173. }
  174.  
  175. @Override
  176. public void makeSound() {
  177. System.out.println("Meowwww");
  178. }
  179.  
  180. @Override
  181. public void eat(Food food) {
  182. super.setFoodEaten(super.getFoodEaten() + food.getQuantity());
  183. }
  184.  
  185.  
  186. @Override
  187. public String toString() {
  188. DecimalFormat format = new DecimalFormat("0.#");
  189. return String.format("%s[%s, %s, %s, %s, %d]",
  190. this.getClass().getSimpleName(), super.getName(), this.breed, format.format(super.getWeight()),
  191. super.getLivingRegion(), super.getFoodEaten());
  192. }
  193. }
  194.  
  195. class Tiger extends Felime {
  196.  
  197. public Tiger(String name, double weight, String livingRegion) {
  198. super(name, weight, livingRegion);
  199. }
  200.  
  201. @Override
  202. public void makeSound() {
  203. System.out.println("ROAAR!!!");
  204. }
  205.  
  206. @Override
  207. public void eat(Food food) {
  208. if (!food.getClass().getSimpleName().equals("Meat")) {
  209. throw new IllegalArgumentException("Tigers are not eating that type of food!");
  210. }
  211. super.setFoodEaten(super.getFoodEaten() + food.getQuantity());
  212. }
  213.  
  214. @Override
  215. public String toString() {
  216. DecimalFormat format = new DecimalFormat("0.#");
  217. return String.format("%s[%s, %s, %s, %d]",
  218. this.getClass().getSimpleName(), super.getName(), format.format(super.getWeight()),
  219. super.getLivingRegion(), super.getFoodEaten());
  220. }
  221. }
  222.  
  223. class Mouse extends Mammal {
  224.  
  225. public Mouse(String name, double weight, String livingRegion) {
  226. super(name, weight, livingRegion);
  227. }
  228.  
  229. @Override
  230. public void makeSound() {
  231. System.out.println("SQUEEEAAAK!");
  232. }
  233.  
  234. @Override
  235. public void eat(Food food) {
  236. if (food.getClass().getSimpleName().equals("Meat")) {
  237. throw new IllegalArgumentException("Mouses are not eating that type of food!");
  238. }
  239. System.out.println("A cheese was just eaten!");
  240. super.setFoodEaten(super.getFoodEaten() + food.getQuantity());
  241. }
  242.  
  243. @Override
  244. public String toString() {
  245. DecimalFormat format = new DecimalFormat("0.#");
  246. return String.format("%s[%s, %s, %s, %d]",
  247. this.getClass().getSimpleName(), super.getName(), format.format(super.getWeight()),
  248. super.getLivingRegion(), super.getFoodEaten());
  249. }
  250. }
  251.  
  252. class Zebra extends Mammal {
  253.  
  254. public Zebra(String name, double weight, String livingRegion) {
  255. super(name, weight, livingRegion);
  256. }
  257.  
  258. @Override
  259. public void makeSound() {
  260. System.out.println("Zs");
  261. }
  262.  
  263. @Override
  264. public void eat(Food food) {
  265. if (food.getClass().getSimpleName().equals("Meat")) {
  266. throw new IllegalArgumentException("Zebras are not eating that type of food!");
  267. }
  268. super.setFoodEaten(super.getFoodEaten() + food.getQuantity());
  269. }
  270.  
  271. @Override
  272. public String toString() {
  273. DecimalFormat format = new DecimalFormat("0.#");
  274. return String.format("%s[%s, %s, %s, %d]",
  275. this.getClass().getSimpleName(), super.getName(), format.format(super.getWeight()),
  276. super.getLivingRegion(), super.getFoodEaten());
  277. }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement