Advertisement
SashkoKlincharov

[Java][НП] - Движечки објекти

Nov 14th, 2020 (edited)
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.21 KB | None | 0 0
  1. import java.io.ObjectStreamException;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. class ObjectCanNotBeMovedException extends Exception {
  7.  
  8. public ObjectCanNotBeMovedException(int pointX, int y) {
  9. super(String.format("Point (%d,%d) is out of bounds", pointX, y));
  10.  
  11. }
  12. }
  13.  
  14. class MovableObjectNotFittableException extends Exception {
  15.  
  16. public MovableObjectNotFittableException(int x, int y, int r) {
  17. super(String.format("Movable circle with center (%d,%d) and radius %d can not be fitted into the collection", x, y, r));
  18. }
  19. }
  20.  
  21. interface Movable {
  22. void moveUp() throws ObjectCanNotBeMovedException;
  23.  
  24. void moveDown() throws ObjectCanNotBeMovedException;
  25.  
  26. void moveLeft() throws ObjectCanNotBeMovedException;
  27.  
  28. void moveRight() throws ObjectCanNotBeMovedException;
  29.  
  30. int getCurrentXPosition();
  31.  
  32. int getCurrentYPosition();
  33.  
  34. String getTypeInstance();
  35.  
  36. Movable moveInDirection(String direction) throws ObjectCanNotBeMovedException;
  37.  
  38. int getRadius();
  39. }
  40.  
  41. class MovablePoint implements Movable {
  42.  
  43. private int pointX;
  44. private int pointY;
  45. private int xSpeed;
  46. private int ySpeed;
  47.  
  48. public MovablePoint(int pointX, int pointY, int xSpeed, int ySpeed) {
  49. this.pointX = pointX;
  50. this.pointY = pointY;
  51. this.xSpeed = xSpeed;
  52. this.ySpeed = ySpeed;
  53. }
  54.  
  55. @Override
  56. public String toString() {
  57. return String.format("Movable point with coordinates (%d,%d)", this.pointX, this.pointY);
  58. }
  59.  
  60.  
  61. @Override
  62. public void moveUp() throws ObjectCanNotBeMovedException {
  63. if (pointY + ySpeed > MovablesCollection.x_MAX)
  64. throw new ObjectCanNotBeMovedException(pointX, pointY + ySpeed);
  65. pointY = pointY + ySpeed;
  66. }
  67.  
  68. @Override
  69. public void moveDown() throws ObjectCanNotBeMovedException {
  70. if (pointY - ySpeed < 0)
  71. throw new ObjectCanNotBeMovedException(pointX, pointY - ySpeed);
  72. pointY = pointY - ySpeed;
  73. }
  74.  
  75. @Override
  76. public void moveLeft() throws ObjectCanNotBeMovedException {
  77. if (pointX - xSpeed < 0)
  78. throw new ObjectCanNotBeMovedException(pointX - xSpeed, pointY);
  79. pointX = pointX - xSpeed;
  80. }
  81.  
  82. @Override
  83. public void moveRight() throws ObjectCanNotBeMovedException {
  84. if (pointX + xSpeed > MovablesCollection.x_MAX)
  85. throw new ObjectCanNotBeMovedException(pointX + xSpeed, pointY);
  86. pointX = pointX + xSpeed;
  87. }
  88.  
  89. @Override
  90. public int getCurrentXPosition() {
  91. return pointX;
  92. }
  93.  
  94. @Override
  95. public int getCurrentYPosition() {
  96. return pointY;
  97. }
  98.  
  99. @Override
  100. public String getTypeInstance() {
  101. return "MovablePoint";
  102. }
  103.  
  104. @Override
  105. public Movable moveInDirection(String direction) throws ObjectCanNotBeMovedException {
  106. // System.out.println(direction);
  107. if (direction.equals("UP")) {
  108. moveUp();
  109. } else if (direction.equals("DOWN")) {
  110. moveDown();
  111. } else if (direction.equals("LEFT")) {
  112. moveLeft();
  113. } else if (direction.equals("RIGHT")) {
  114. moveRight();
  115. }
  116. return this;
  117. }
  118.  
  119. @Override
  120. public int getRadius() {
  121. return 0;
  122. }
  123.  
  124.  
  125. }
  126.  
  127. class MovableCircle implements Movable {
  128.  
  129. private int radius;
  130. private MovablePoint center;
  131.  
  132. public MovableCircle(int radius, MovablePoint center) {
  133. this.radius = radius;
  134. this.center = center;
  135. }
  136.  
  137. @Override
  138. public String toString() {
  139. return String.format("Movable circle with center coordinates (%d,%d) and radius %d\n", this.center.getCurrentXPosition(), this.center.getCurrentYPosition(), radius);
  140. }
  141.  
  142. @Override
  143. public void moveUp() throws ObjectCanNotBeMovedException {
  144. center.moveUp();
  145. }
  146.  
  147. @Override
  148. public void moveDown() throws ObjectCanNotBeMovedException {
  149. center.moveDown();
  150. }
  151.  
  152. @Override
  153. public void moveLeft() throws ObjectCanNotBeMovedException {
  154. center.moveLeft();
  155. }
  156.  
  157. @Override
  158. public void moveRight() throws ObjectCanNotBeMovedException {
  159. center.moveRight();
  160. }
  161.  
  162. @Override
  163. public int getCurrentXPosition() {
  164. return center.getCurrentXPosition();
  165. }
  166.  
  167. @Override
  168. public int getCurrentYPosition() {
  169. return center.getCurrentYPosition();
  170. }
  171.  
  172. public int getRadius() {
  173. return radius;
  174. }
  175.  
  176. @Override
  177. public String getTypeInstance() {
  178. return "MovableCircle";
  179. }
  180.  
  181. @Override
  182. public Movable moveInDirection(String direction) throws ObjectCanNotBeMovedException {
  183. if (direction.equals("UP")) {
  184. moveUp();
  185. } else if (direction.equals("DOWN")) {
  186. moveDown();
  187. } else if (direction.equals("LEFT")) {
  188. moveLeft();
  189. } else if (direction.equals("RIGHT")) {
  190. moveRight();
  191. }
  192. return this;
  193. }
  194. }
  195.  
  196. class MovablesCollection {
  197.  
  198. private List<Movable> movables;
  199. public static int x_MAX = 0;
  200. public static int y_MAX = 0;
  201.  
  202. public MovablesCollection(int x_MAX, int y_MAX) {
  203. this.x_MAX = x_MAX;
  204. this.y_MAX = y_MAX;
  205. this.movables = new ArrayList<>();
  206. }
  207.  
  208. public static void setX_MAX(int x_MAX) {
  209. MovablesCollection.x_MAX = x_MAX;
  210. }
  211.  
  212. public static void setY_MAX(int y_MAX) {
  213. MovablesCollection.y_MAX = y_MAX;
  214. }
  215.  
  216. public void addMovableObject(Movable m) throws MovableObjectNotFittableException {
  217. if(m.getTypeInstance().equals("MovablePoint")){
  218. if(m.getCurrentXPosition()>=0&&m.getCurrentXPosition() <= x_MAX
  219. && m.getCurrentYPosition()>=0 && m.getCurrentYPosition() <= y_MAX){
  220. movables.add(m);
  221. }else throw new MovableObjectNotFittableException(m.getCurrentXPosition(),m.getCurrentYPosition(),0);
  222. }else if(m.getTypeInstance().equals("MovableCircle")) {
  223. if(m.getCurrentXPosition() - m.getRadius() >=0 && m.getCurrentXPosition() + m.getRadius() <= x_MAX
  224. && m.getCurrentYPosition() - m.getRadius() >=0 && m.getCurrentYPosition() + m.getRadius() <= y_MAX){
  225. movables.add(m);
  226. }else throw new MovableObjectNotFittableException(m.getCurrentXPosition(),m.getCurrentYPosition(),m.getRadius());
  227. }
  228. }
  229.  
  230. public void moveObjectsFromTypeWithDirection(TYPE type, DIRECTION direction) throws ObjectCanNotBeMovedException {
  231. if (type.equals(TYPE.CIRCLE)) {
  232. for (int i = 0; i < movables.size(); i++) {
  233. if (movables.get(i).getTypeInstance().equals("MovableCircle")) {
  234. try {
  235. movables.set(i, movables.get(i).moveInDirection(direction.name()));
  236. } catch (ObjectCanNotBeMovedException e) {
  237. System.out.println(e.getMessage());
  238. }
  239. }
  240. }
  241. } else if(type.equals(TYPE.POINT)) {
  242. for (int i = 0; i < movables.size(); i++) {
  243. if (movables.get(i).getTypeInstance().equals("MovablePoint")) {
  244. try {
  245. movables.set(i, movables.get(i).moveInDirection(direction.name()));
  246. } catch (ObjectCanNotBeMovedException e) {
  247. System.out.println(e.getMessage());
  248. }
  249. }
  250. }
  251. }
  252.  
  253. }
  254.  
  255. @Override
  256. public String toString() {
  257. StringBuilder sb = new StringBuilder();
  258. sb.append(String.format("Collection of movable objects with size %d:\n", movables.size()));
  259. for (int i = 0; i < movables.size(); i++) {
  260. if(movables.get(i).getTypeInstance().equals("MovablePoint")) {
  261. sb.append(String.format("Movable point with coordinates (%d,%d)\n", movables.get(i).getCurrentXPosition(), movables.get(i).getCurrentYPosition()));
  262. }else {
  263. sb.append(String.format("Movable circle with center coordinates (%d,%d) and radius %d\n", movables.get(i).getCurrentXPosition(), movables.get(i).getCurrentYPosition(),movables.get(i).getRadius()));
  264. }
  265. }
  266. return sb.toString();
  267. }
  268. }
  269.  
  270.  
  271. enum TYPE {
  272. POINT,
  273. CIRCLE
  274. }
  275.  
  276. enum DIRECTION {
  277. UP,
  278. DOWN,
  279. LEFT,
  280. RIGHT
  281. }
  282.  
  283. public class CirclesTest {
  284.  
  285. public static void main(String[] args) {
  286.  
  287. System.out.println("===COLLECTION CONSTRUCTOR AND ADD METHOD TEST===");
  288. MovablesCollection collection = new MovablesCollection(100, 100);
  289. Scanner sc = new Scanner(System.in);
  290. int samples = Integer.parseInt(sc.nextLine());
  291. for (int i = 0; i < samples; i++) {
  292. String inputLine = sc.nextLine();
  293. String[] parts = inputLine.split(" ");
  294.  
  295. int x = Integer.parseInt(parts[1]);
  296. int y = Integer.parseInt(parts[2]);
  297. int xSpeed = Integer.parseInt(parts[3]);
  298. int ySpeed = Integer.parseInt(parts[4]);
  299.  
  300. if (Integer.parseInt(parts[0]) == 0) { //point
  301. try {
  302. collection.addMovableObject(new MovablePoint(x, y, xSpeed, ySpeed));
  303. } catch (MovableObjectNotFittableException e) {
  304. System.out.println(e.getMessage());
  305. }
  306. } else { //circle
  307. int radius = Integer.parseInt(parts[5]);
  308. try {
  309. collection.addMovableObject(new MovableCircle(radius, new MovablePoint(x, y, xSpeed, ySpeed)));
  310. } catch (MovableObjectNotFittableException e) {
  311. System.out.println(e.getMessage());
  312. }
  313. }
  314.  
  315. }
  316. System.out.println(collection.toString());
  317.  
  318. System.out.println("MOVE POINTS TO THE LEFT");
  319.  
  320. try {
  321. collection.moveObjectsFromTypeWithDirection(TYPE.POINT, DIRECTION.LEFT);
  322. } catch (ObjectCanNotBeMovedException e) {
  323. e.printStackTrace();
  324. }
  325.  
  326. System.out.println(collection.toString());
  327.  
  328. System.out.println("MOVE CIRCLES DOWN");
  329. try {
  330. collection.moveObjectsFromTypeWithDirection(TYPE.CIRCLE, DIRECTION.DOWN);
  331. } catch (ObjectCanNotBeMovedException e) {
  332. System.out.println(e.getMessage());
  333. }
  334. System.out.println(collection.toString());
  335.  
  336. System.out.println("CHANGE X_MAX AND Y_MAX");
  337. MovablesCollection.setX_MAX(90);
  338. MovablesCollection.setY_MAX(90);
  339.  
  340. System.out.println("MOVE POINTS TO THE RIGHT");
  341. try {
  342. collection.moveObjectsFromTypeWithDirection(TYPE.POINT, DIRECTION.RIGHT);
  343. } catch (ObjectCanNotBeMovedException e) {
  344. System.out.println(e.getMessage());
  345. }
  346. System.out.println(collection.toString());
  347.  
  348. System.out.println("MOVE CIRCLES UP");
  349. try {
  350. collection.moveObjectsFromTypeWithDirection(TYPE.CIRCLE, DIRECTION.UP);
  351. } catch (ObjectCanNotBeMovedException e) {
  352. System.out.println(e.getMessage());
  353. }
  354. System.out.println(collection.toString());
  355.  
  356. }
  357.  
  358.  
  359. }
  360.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement