Skip to content

Instantly share code, notes, and snippets.

@ehdrhelr
Created January 15, 2021 08:58
Show Gist options
  • Save ehdrhelr/bac44cf4bb739b42ffe1c593e8eaa974 to your computer and use it in GitHub Desktop.
Save ehdrhelr/bac44cf4bb739b42ffe1c593e8eaa974 to your computer and use it in GitHub Desktop.
CS04
package main.day4.rectangle;
import main.day4.rectangle.controller.MainController;
public class Application {
public static void main(String[] args) {
MainController main = new MainController();
main.start();
}
}
package main.day4.rectangle.service;
import main.day4.rectangle.domain.*;
import main.day4.triangle.view.Printer;
public class Calculator {
public static void calLength(int i, int j) {
Position a = PositionRepository.positions().get(i);
Position b = PositionRepository.positions().get(j);
Length length = new Length(a, b);
LengthRepository.add(length);
}
public static void calThreeLengths() {
calLength(0, 1);
calLength(1, 2);
calLength(2, 0);
}
public static Triangle getTriangle() {
calThreeLengths();
Length a = LengthRepository.lengths().get(0);
Length b = LengthRepository.lengths().get(1);
Length c = LengthRepository.lengths().get(2);
return new Triangle(a, b, c);
}
public static void getResult() {
int posCnt = PositionRepository.positions().size(); // Position Count
if (posCnt == 2) { // 점이 2개 일때 (직선)
calLength(0, 1);
double result = LengthRepository.lengths().get(0).calVal();
Printer.printLength(result);
return;
}
if (posCnt == 3) { // 점이 3개 일때 (삼각형)
Triangle triangle = getTriangle();
Printer.printArea(triangle.calArea());
return;
}
}
}
package main.day4.rectangle.service;
import main.day4.rectangle.domain.Position;
import main.day4.rectangle.domain.PositionRepository;
public class Extractor {
public static void savePos(String input) {
String[] pos = input.split("-");
for(int i = 0; i < pos.length; i++) {
Position eachPos = extractPos(pos[i]);
PositionRepository.add(eachPos);
}
}
public static Position extractPos(String pos) {
pos = pos.replace("(", "");
pos = pos.replace(")", "");
String[] xy = pos.split(",");
return new Position(Integer.parseInt(xy[0]), Integer.parseInt(xy[1]));
}
}
package main.day4.rectangle.view;
public class InputView {
public static void askPos() {
System.out.println("> 좌표를 입력해주세요.");
}
}
package main.day4.rectangle.domain;
public class Length {
private Position a;
private Position b;
Length() {
}
public Length(Position a, Position b) {
this.a = a;
this.b = b;
}
public double calVal() {
return Math.sqrt(
Math.pow(
(a.getX() - b.getX()), 2) + Math.pow((a.getY() - b.getY())
, 2)
);
}
}
package main.day4.rectangle.domain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class LengthRepository {
private static final List<Length> lengths = new ArrayList<>();
public static List<Length> lengths() {
return Collections.unmodifiableList(lengths);
}
public static boolean add(Length length) {
return lengths.add(length);
}
}
package main.day4.rectangle.controller;
import main.day4.rectangle.service.Calculator;
import main.day4.rectangle.service.Extractor;
import main.day4.rectangle.view.InputView;
import java.util.Scanner;
public class MainController {
public void start() {
Scanner sc = new Scanner(System.in);
InputView.askPos();
String input = sc.nextLine();
Extractor.savePos(input);
Calculator.getResult();
}
}
package main.day4.rectangle.domain;
public class Position {
private int x;
private int y;
Position() {
}
public Position(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
package main.day4.rectangle.domain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PositionRepository {
private static final List<Position> positions = new ArrayList<>();
public static List<Position> positions() {
return Collections.unmodifiableList(positions);
}
public static boolean add(Position pos) {
return positions.add(pos);
}
}
package main.day4.rectangle.view;
public class ResultView {
public static void printLength(double result) {
System.out.println("두 점 사이의 거리는 " + result);
}
public static void printArea(double result) {
System.out.println("삼각형의 넓이는 " + result);
}
}
package main.day4.rectangle.domain;
public class Triangle {
private Position aPos;
private Position bPos;
private Position cPos;
private Length aLeng;
private Length bLeng;
private Length cLeng;
Triangle() {
}
public Triangle(Length aLeng, Length bLeng, Length cLeng) {
this.aLeng = aLeng;
this.bLeng = bLeng;
this.cLeng = cLeng;
}
public double calArea() {
return Math.sqrt(
4 * Math.pow(aLeng.calVal(), 2) * Math.pow(bLeng.calVal(), 2)
- Math.pow(
(Math.pow(aLeng.calVal(), 2) + Math.pow(bLeng.calVal(), 2) - Math.pow(cLeng.calVal(), 2)
), 2
)
) / 4;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment