Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/main/java/calculator/DotV1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package calculator;

import java.util.Objects;

public class DotV1 {
private final int x;
private final int y;
private static final String INVALID_INPUT_FORMAT = "Invalid input format";
private static final String REGEX_FOR_STRING_INPUT = "[()]";
private static final int MAX_LOCATION_FOR_DOT = 24;
public DotV1(int x, int y) {
validateLocations(x, y);
this.x = x;
this.y = y;
}

public DotV1(String input) {
String[] parts = input.replaceAll(REGEX_FOR_STRING_INPUT, "").split(",");
if (parts.length != 2) {
throw new IllegalArgumentException(INVALID_INPUT_FORMAT);
}
int x = Integer.parseInt(parts[0].trim());
int y = Integer.parseInt(parts[1].trim());
validateLocations(x, y);
this.x = x;
this.y = y;
}

private void validateLocations(int x, int y) {
if (x > MAX_LOCATION_FOR_DOT || y > MAX_LOCATION_FOR_DOT) throw new IllegalArgumentException();
}

public double getDistanceBetween(DotV1 secondDotV1) {
return Math.sqrt(Math.pow((secondDotV1.x - this.x), 2) + Math.pow((secondDotV1.y - this.y), 2));
}
public boolean isXParallel(DotV1 dotV1) {
return this.y == dotV1.y;
}

public boolean isYParallel(DotV1 dotV1) {
return this.x == dotV1.x;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DotV1 dotV1 = (DotV1) o;
return x == dotV1.x && y == dotV1.y;
}

@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
141 changes: 141 additions & 0 deletions src/main/java/calculator/Dots.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package calculator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Dots {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dots 클래스가 너무 많은 일을 하고있는 것은 아닐까? 상속을 활용할 순 없을까?

private final List<DotV1> dotV1s;

/**
* CONSTANTS
*/
private static final String SPLIT_EXPRESSION = "-";
private static final String IS_NOT_LINE = "선이 아니다.";
private static final String IS_NOT_SQUARE = "사각형이 아니다.";

public Dots(String input) {
validateInput(input);
this.dotV1s = Arrays.stream(input.split("-")).map(DotV1::new).collect(Collectors.toList());
}

private Dots(DotV1 first, DotV1 second){
this.dotV1s = new ArrayList<>();
dotV1s.add(first);
dotV1s.add(second);
}

public static Dots asLine(DotV1 first, DotV1 second){
return new Dots(first, second);
}

/**
* validations & common methods
*/
private void validateInput(String input) {
if (!input.contains(SPLIT_EXPRESSION)){
throw new IllegalArgumentException();
}
}

public int size() {
return this.dotV1s.size();
}

/**
* line methods
*/
public boolean isLine() {
return this.dotV1s.size() == 2;
}

public boolean isXParallelLine(){
if (!isLine()) {
throw new IllegalArgumentException(IS_NOT_LINE);
}
return dotV1s.get(0).isXParallel(dotV1s.get(1));
}

public boolean isYParallelLine(){
if (!isLine()) {
throw new IllegalArgumentException(IS_NOT_LINE);
}
return dotV1s.get(0).isYParallel(dotV1s.get(1));
}

public double getDistanceBetween(){
if (!this.isLine()) {
throw new IllegalArgumentException(IS_NOT_LINE);
}
return this.dotV1s.get(0).getDistanceBetween(dotV1s.get(1));
}

/**
* square methods
*/
public boolean isSquare() {
if (this.dotV1s.size() != 4) {
return false;
}
return isAllParallel();
}

public boolean isAllParallel() {
return IntStream.range(0, 4).allMatch(i -> {
DotV1 dotV1 = dotV1s.get(i);
DotV1 dotV12 = dotV1s.get((i + 1) % 4); // 다음 점
return dotV1.isXParallel(dotV12) || dotV1.isYParallel(dotV12);
});
}

public int getSquareArea(){
if (!isSquare()) {
throw new IllegalArgumentException(IS_NOT_SQUARE);
}

List<Double> collect = IntStream.range(0, 4)
.mapToObj(i -> dotV1s.get(i).getDistanceBetween(dotV1s.get((i + 1) % 4)))
.distinct().collect(Collectors.toList());

if (collect.size() == 1) return (int) Math.pow(collect.get(0), 2);
return (int) (collect.get(0) * collect.get(1));
}

/**
* triangle
*/
public boolean isTriangle() {
return this.dotV1s.size() == 3;
}

public double getLinesLength(){
double lineLength = 0;
for (int i = 0; i < this.dotV1s.size(); i++) {
DotV1 first = dotV1s.get(i);
DotV1 second = dotV1s.get((i + 1) % dotV1s.size());
LineV1 lineV1 = new LineV1(first, second);
lineLength += lineV1.getDotsDistance();
}
return lineLength;
}

//헤론의 공식
//s = (a + b + c) / 2
//Area = √(s * (s - a) * (s - b) * (s - c))
public double getTriangleArea(){
List<Double> lineLengths = new ArrayList<>();
for (int i = 0; i < this.dotV1s.size(); i++) {
DotV1 first = dotV1s.get(i);
DotV1 second = dotV1s.get((i + 1) % dotV1s.size());
LineV1 lineV1 = new LineV1(first, second);
lineLengths.add(lineV1.getDotsDistance());
}
double area = getLinesLength() / 2;
for (Double lineLength : lineLengths) {
area *= (getLinesLength() / 2) - lineLength;
}
return Math.sqrt(area);
}
}
35 changes: 0 additions & 35 deletions src/main/java/calculator/Line.java

This file was deleted.

25 changes: 25 additions & 0 deletions src/main/java/calculator/LineV1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package calculator;

public class LineV1 {
private final Dots dots;

public LineV1(Dots dots) {
this.dots = dots;
}

public LineV1(DotV1 first, DotV1 second){
this.dots = Dots.asLine(first, second);
}

public double getDotsDistance(){
return this.dots.getDistanceBetween();
}

public boolean isXParallel() {
return this.dots.isXParallelLine();
}

public boolean isYParallel(){
return this.dots.isYParallelLine();
}
}
16 changes: 16 additions & 0 deletions src/main/java/calculator/SquareV1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package calculator;

public class SquareV1 {
private final Dots dots;

public SquareV1(Dots dots) {
if (!dots.isSquare()){
throw new IllegalArgumentException();
}
this.dots = dots;
}

public int getArea() {
return this.dots.getSquareArea();
}
}
5 changes: 5 additions & 0 deletions src/main/java/calculator/TriangleV1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package calculator;

public class TriangleV1 {

}
21 changes: 21 additions & 0 deletions src/main/java/figure/AbstractFigure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package figure;

import java.util.List;

public abstract class AbstractFigure implements Figure{
private final List<Dot> dotList;

protected AbstractFigure(List<Dot> dotList) {
if (dotList.size() != this.size()){
throw new IllegalArgumentException();
}
this.dotList = dotList;
}
public Dot getDot(int index){
return this.dotList.get(index);
}
@Override
public List<Dot> getDots(){
return this.dotList;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package calculator;
package figure;

import java.util.Objects;

Expand Down Expand Up @@ -33,6 +33,13 @@ private void validateLocations(int x, int y) {
public double getDistanceBetween(Dot secondDot) {
return Math.sqrt(Math.pow((secondDot.x - this.x), 2) + Math.pow((secondDot.y - this.y), 2));
}
public boolean isXParallel(Dot dot) {
return this.y == dot.y;
}

public boolean isYParallel(Dot dot) {
return this.x == dot.x;
}

@Override
public boolean equals(Object o) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/figure/Figure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package figure;

import java.util.List;

public interface Figure {
String getName();
int size();
double getArea();
List<Dot> getDots();
}
22 changes: 22 additions & 0 deletions src/main/java/figure/FigureEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package figure;

public enum FigureEnum {
LINE("Line", 2),
TRIANGLE("Triangle", 3),
SQUARE("Square", 4);

private final String name;
private final int size;
FigureEnum(String name, int size) {
this.name = name;
this.size = size;
}

public String getName() {
return name;
}

public int getSize(){
return this.size;
}
}
21 changes: 21 additions & 0 deletions src/main/java/figure/FigureFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package figure;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FigureFactory {
private final List<Figure> figures;
private static final Map<Integer, Figure> figureMap = new HashMap<>();

public FigureFactory(List<Figure> figures) {
this.figures = figures;
for (Figure figure : figures) {
figureMap.put(figure.size(), figure);
}
}

static Figure getInstance(List<Dot> points) {
return figureMap.get(points.size());
}
}
Loading