Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.45 KB | None | 0 0
  1. package com.kulman;
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. public class GeometricShape implements GeometricShapeInterface {
  6.     private List<BetterPoint> points;
  7.     private int dimensions;
  8.     private int counter;
  9.  
  10.     private class BetterPoint {
  11.         private Point point;
  12.         private int count;
  13.  
  14.         private BetterPoint(Point point, int count) {
  15.             this.point = point;
  16.             this.count = count;
  17.         }
  18.  
  19.         private Point getPoint() {
  20.             return point;
  21.         }
  22.  
  23.         private int getCount() {
  24.             return count;
  25.         }
  26.     }
  27.  
  28.     public GeometricShape() {
  29.         this.points = new LinkedList<>();
  30.         this.dimensions = 0;
  31.         this.counter = 0;
  32.     }
  33.  
  34.     /**
  35.      * Metoda zwraca liczbę wymiarów point
  36.      *
  37.      * @param point sprawdzany punkt
  38.      * @return liczba wymiarów punktu
  39.      */
  40.     private int checkDimensions(Point point) {
  41.         int i = 0;
  42.         String number = "";
  43.         while (true) {
  44.             try {
  45.                 point.getPosition(i);
  46.             } catch (ArrayIndexOutOfBoundsException e) {
  47.                 for (int j = 42; j < e.toString().length(); j++) {
  48.                     number += Character.getNumericValue(e.toString().charAt(j));
  49.                 }
  50.                 return Integer.parseInt(number);
  51.             }
  52.             i++;
  53.         }
  54.     }
  55.  
  56.     /**
  57.      * Metoda dodaje punkt. Punkt dodawany na koniec kolekcji.
  58.      *
  59.      * @param point dodawany punkt
  60.      * @throws WrongNumberOfDimensionsException wyjątek zgłaszany, gdy nowododawany
  61.      *                                          punkt posiada inną liczbę wymiarów
  62.      *                                          niż te, które już wcześniej przed
  63.      *                                          nim dodano. O poprawnej liczbie
  64.      *                                          wymiarów decyduje <b>pierwszy</b>
  65.      *                                          punkt dodany do kształtu.
  66.      */
  67.     @Override
  68.     public void add(Point point) throws WrongNumberOfDimensionsException {
  69.         if(dimensions == 0) {
  70.             points.add(new BetterPoint(point, counter));
  71.             counter++;
  72.             dimensions = checkDimensions(point);
  73.         } else {
  74.             int pointDimension = checkDimensions(point);
  75.             if(pointDimension != dimensions) throw new WrongNumberOfDimensionsException(dimensions, pointDimension);
  76.             points.add(new BetterPoint(point, counter));
  77.             counter++;
  78.         }
  79.     }
  80.  
  81.     /**
  82.      * Metoda usuwa punkt point, o ile taki istnieje. Jeśli w kolekcji punktów jest
  83.      * więcej takich samych jak point, usuwany jest tylko pierwszy z nich.
  84.      *
  85.      * @param point punkt do usunięcia
  86.      * @throws WrongArgumentException wyjątek zgłaszany gdy zlecane jest usunięcie
  87.      *                                punktu, który nie należy do figury.
  88.      */
  89.     @Override
  90.     public void remove(Point point) throws WrongArgumentException {
  91.         for(int i = 0; i < points.size(); i++) {
  92.             if(points.get(i).getPoint().equals(point)) {
  93.                 points.remove(i);
  94.                 return;
  95.             }
  96.         }
  97.         throw new WrongArgumentException(point);
  98.     }
  99.  
  100.     /**
  101.      * Metoda dodaje punkt przed punktem beforePoint.
  102.      *
  103.      * @param point       dodawany punkt
  104.      * @param beforePoint punkt, bezpośrednio przed którym nowy należy dodać
  105.      * @throws WrongArgumentException           wyjątek zgłaszany, gdy jako
  106.      *                                          beforePoint przekazany został punkt,
  107.      *                                          który nie został wcześniej dodany do
  108.      *                                          figury.
  109.      * @throws WrongNumberOfDimensionsException wyjątek zgłaszany, gdy liczba
  110.      *                                          wymiarów punktu point lub
  111.      *                                          beforePoint nie jest zgodna z liczbą
  112.      *                                          wymiarów punktów dodanych wcześniej
  113.      *                                          do kształtu.
  114.      */
  115.     @Override
  116.     public void addBefore(Point point, Point beforePoint)  throws WrongArgumentException, WrongNumberOfDimensionsException {
  117.         int pointDimension = checkDimensions(point);
  118.         if(pointDimension != dimensions) throw new WrongNumberOfDimensionsException(dimensions, pointDimension);
  119.         pointDimension = checkDimensions(beforePoint);
  120.         if(pointDimension != dimensions) throw new WrongNumberOfDimensionsException(dimensions, pointDimension);
  121.  
  122.         for(int i = 0; i < points.size(); i++) {
  123.             if(points.get(i).getPoint().equals(beforePoint)) {
  124.                 points.add(i, new BetterPoint(point, counter));
  125.                 counter++;
  126.                 return;
  127.             }
  128.         }
  129.         throw new WrongArgumentException(beforePoint);
  130.     }
  131.  
  132.     /**
  133.      * Metoda dodaje punkt za punktem afterPoint.
  134.      *
  135.      * @param point      dodawany punkt
  136.      * @param afterPoint punkt, bezpośrednio za którym nowy należy dodać
  137.      * @throws WrongArgumentException           wyjątek zgłaszany, gdy jako
  138.      *                                          afterPoint przekazany został punkt,
  139.      *                                          który nie został wcześniej dodany do
  140.      *                                          figury.
  141.      * @throws WrongNumberOfDimensionsException wyjątek zgłaszany, gdy liczba
  142.      *                                          wymiarów punktu point lub afterPoint
  143.      *                                          nie jest zgodna z liczbą wymiarów
  144.      *                                          punktów dodanych wcześniej do
  145.      *                                          kształtu.
  146.      */
  147.     @Override
  148.     public void addAfter(Point point, Point afterPoint) throws WrongNumberOfDimensionsException, WrongArgumentException {
  149.         int pointDimension = checkDimensions(point);
  150.         if(pointDimension != dimensions) throw new WrongNumberOfDimensionsException(dimensions, pointDimension);
  151.         pointDimension = checkDimensions(afterPoint);
  152.         if(pointDimension != dimensions) throw new WrongNumberOfDimensionsException(dimensions, pointDimension);
  153.  
  154.         for(int i = points.size()-1; i >= 0; i--) {
  155.             if(points.get(i).getPoint().equals(afterPoint)) {
  156.                 if(i == points.size()-1) {
  157.                     points.add(new BetterPoint(point, counter));
  158.                     counter++;
  159.                     return;
  160.                 }
  161.                 points.add(i+1, new BetterPoint(point, counter));
  162.                 counter++;
  163.                 return;
  164.             }
  165.         }
  166.         throw new WrongArgumentException(afterPoint);
  167.     }
  168.  
  169.     /**
  170.      * Metoda usuwa punkt przed punktem beforePoint.
  171.      *
  172.      * @param beforePoint punkt istniejący bezpośrednio przed beforePoint należy
  173.      *                    usunąć.
  174.      * @return Gdy punkt odniesienia istniał oraz istniał punkt do usunięcia,
  175.      *         zwracana jest referencja do usuniętego punktu.
  176.      * @throws NoSuchPointException             wyjątek zgłaszany, gdy punkt
  177.      *                                          beforePoint jest pierwszym z punktów
  178.      *                                          figury i innego punktu przed nim nie
  179.      *                                          ma.
  180.      * @throws WrongArgumentException           wyjątek zgłaszany, gdy punkt
  181.      *                                          beforePoint nie został wcześniej
  182.      *                                          dodany do figury.
  183.      * @throws WrongNumberOfDimensionsException wyjątek zgłaszany, gdy liczba
  184.      *                                          wymiarów punktu beforePoint nie jest
  185.      *                                          zgodna z liczbą wymiarów punktów
  186.      *                                          dodanych wcześniej do kształtu.
  187.      */
  188.     @Override
  189.     public Point removeBefore(Point beforePoint) throws NoSuchPointException, WrongNumberOfDimensionsException, WrongArgumentException {
  190.         int pointDimension = checkDimensions(beforePoint);
  191.         if(pointDimension != dimensions) throw new WrongNumberOfDimensionsException(dimensions, pointDimension);
  192.  
  193.         for(int i = 0; i < points.size(); i++) {
  194.             if (points.get(i).getPoint().equals(beforePoint)) {
  195.                 if (i == 0)
  196.                     throw new NoSuchPointException(beforePoint);
  197.  
  198.                 Point point = points.get(i-1).getPoint();
  199.                 points.remove(i-1);
  200.                 return point;
  201.             }
  202.         }
  203.  
  204.         throw new WrongArgumentException(beforePoint);
  205.     }
  206.  
  207.     /**
  208.      * Metoda usuwa punkt za punktem afterPoint.
  209.      *
  210.      * @param afterPoint punkt istniejący bezpośrednio po afterPoint należy usunąć.
  211.      * @return Gdy punkt odniesienia istniał oraz istniał punkt do usunięcia,
  212.      *         zwracana jest referencja do usuniętego punktu.
  213.      * @throws NoSuchPointException             wyjątek zgłaszany, gdy punkt
  214.      *                                          afterPoint jest ostatnim z punktów
  215.      *                                          figury i innego punktu za nim nie
  216.      *                                          ma.
  217.      * @throws WrongArgumentException           wyjątek zgłaszany, gdy punkt
  218.      *                                          afterPoint nie został wcześniej
  219.      *                                          dodany do figury.
  220.      * @throws WrongNumberOfDimensionsException wyjątek zgłaszany, gdy liczba
  221.      *                                          wymiarów punktu afterPoint nie jest
  222.      *                                          zgodna z liczbą wymiarów punktów
  223.      *                                          dodanych wcześniej do kształtu.
  224.      */
  225.     @Override
  226.     public Point removeAfter(Point afterPoint) throws NoSuchPointException, WrongNumberOfDimensionsException, WrongArgumentException {
  227.         int pointDimension = checkDimensions(afterPoint);
  228.         if(dimensions != pointDimension) throw new WrongNumberOfDimensionsException(dimensions, pointDimension);
  229.  
  230.         for(int i = points.size()-1; i >= 0; i--) {
  231.             if(points.get(i).getPoint().equals(afterPoint)) {
  232.                 if(i == points.size()-1) {
  233.                     throw new NoSuchPointException(afterPoint);
  234.                 }
  235.                 Point point = points.get(i+1).getPoint();
  236.                 points.remove(i+1);
  237.                 return point;
  238.             }
  239.         }
  240.         throw new WrongArgumentException(afterPoint);
  241.     }
  242.  
  243.     /**
  244.      * Metoda zwraca aktualną listę wszystkich punktów.
  245.      *
  246.      * @return lista punktów
  247.      */
  248.     @Override
  249.     public List<Point> get() {
  250.         List<Point> list = new ArrayList<>(points.size());
  251.         for(BetterPoint betterPoint : points) {
  252.             list.add(betterPoint.getPoint());
  253.         }
  254.         return list;
  255.     }
  256.  
  257.     /**
  258.      * Metoda zwraca zbiór punktów czyli kolekcję punktów bez powtórzeń. Kolejność
  259.      * punktów w tej kolekcji nie ma znaczenia. Powtórzenie punktu ma miejsce wtedy,
  260.      * gdy P1.equals(P2)=true.
  261.      *
  262.      * @return kolekcja punktów bez powtórzeń.
  263.      */
  264.     @Override
  265.     public Set<Point> getSetOfPoints() {
  266.         return new HashSet<>(this.get());
  267.     }
  268.  
  269.     /**
  270.      * Metoda zwraca obiekt typu Optional zawierający (o ile istnieje) punkt,
  271.      * którego współrzędne przekazywane są na liście positions. Jeśli istnieje wiele
  272.      * punktów o podanych wspołrzędnych zwracany jest punkt, który został dodany
  273.      * jako ostatni. Metoda nigdy nie może zakończyć się zwróceniem null, jeśli
  274.      * punktu o podanych współrzędnych nie ma, należy zwrócić pusty obiekt Optional.
  275.      *
  276.      * @param positions lista współrzędnych
  277.      * @return obiekt Optional zawierający (o ile istnieje) punkt, o przekazanych za
  278.      *         pomocą positions współrzędnych, w przeciwnym wypadku pusty obiekt
  279.      *         Optional.
  280.      * @throws WrongNumberOfDimensionsException wyjątek zgłaszany, gdy rozmiar listy
  281.      *                                          jest niezgodny z liczbą wymiarów
  282.      *                                          punktów należących do kształtu.
  283.      */
  284.     @Override
  285.     public Optional<Point> getByPosition(List<Integer> positions) throws WrongNumberOfDimensionsException {
  286.         if(dimensions != positions.size()) throw new WrongNumberOfDimensionsException(dimensions, positions.size()+1);
  287.  
  288.         List<BetterPoint> list = new ArrayList<>();
  289.  
  290.         for(BetterPoint betterPoint : points) {
  291.             int j = 0;
  292.             Point point = betterPoint.getPoint();
  293.             for(int i = 0; i < dimensions; i++) {
  294.                 if(point.getPosition(i) == positions.get(i)) {
  295.                     j++;
  296.                 }
  297.             }
  298.  
  299.             if(j == dimensions) {
  300.                 list.add(betterPoint);
  301.             }
  302.         }
  303.  
  304.         if(list.size() == 0) {
  305.             return Optional.empty();
  306.         }
  307.  
  308.         Point point = new Point(dimensions);
  309.         int count;
  310.         int bestCount = 0;
  311.         for(BetterPoint betterPoint : list) {
  312.             count = betterPoint.getCount();
  313.             if(count >= bestCount) {
  314.                 bestCount = count;
  315.                 point = betterPoint.getPoint();
  316.             }
  317.         }
  318.         return Optional.of(point);
  319.     }
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement