Skip to content
Merged
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
18 changes: 14 additions & 4 deletions src/main/java/coordinate/CoordinateLine.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package coordinate;

import java.util.ArrayList;
import java.util.*;

public class CoordinateLine {
private ArrayList<Integer> list = new ArrayList<>();


private List<Point> points = new ArrayList<>();

public CoordinateLine(List<Integer> numbers) {
for (int i = 0; i < numbers.size(); i++) {
if (i % 2 == 0)
points.add(new Point(numbers.get(i), numbers.get(i + 1)));
}
}

public double findLineLength() {
return points.get(0).calculatorLength(points.get(1));
}

public List<Point> getPoints() {
return points;
}
}
20 changes: 17 additions & 3 deletions src/main/java/coordinate/CoordinateMain.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
package coordinate;

import java.util.*;

public class CoordinateMain {
public static void main(String[] args) {
PrintView.drawVertical();
PrintView.drawRow();
PrintView.rowNumber();
try {
run();
} catch (Exception e) {
run();
}
}

public static void run() throws IllegalArgumentException {
List<Integer> numbers = InputView.extractNum(InputView.inputPoint());

CoordinateLine line = new CoordinateLine(numbers);

PrintView.drawVertical(line.getPoints());
PrintView.drawRow();
PrintView.rowNumber();
PrintView.printLength(line.findLineLength());
}
}
25 changes: 25 additions & 0 deletions src/main/java/coordinate/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package coordinate;

import java.util.*;

public class InputView {

public static String inputPoint() {
Scanner scan = new Scanner(System.in);
System.out.println("좌표를 입력하세요.");
return scan.next();
}

public static List<Integer> extractNum(String beforeToken) {
StringTokenizer stz = new StringTokenizer(beforeToken, "\\(|\\)|,|-");
List<Integer> numbers = new ArrayList<>();
while (stz.hasMoreElements()) {
numbers.add(stringToInt(stz.nextToken()));
}
return numbers;
}

public static int stringToInt(String stringObject) {
return Integer.parseInt(stringObject);
}
}
25 changes: 25 additions & 0 deletions src/main/java/coordinate/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package coordinate;

public class Point {
private int x;
private int y;

public Point(int x, int y)throws IllegalArgumentException {
if (x < 0 || x > 24 || y < 0 || y > 24)
throw new IllegalArgumentException();
this.x = x;
this.y = y;
}

public double calculatorLength(Point point) {
return Math.sqrt(Math.pow(this.x - point.x, 2) + Math.pow(this.y - point.y, 2));
}

public Boolean isX(int x) {
return this.x * 3 == x;
}

public Boolean isY(int y) {
return this.y == y;
}
}
65 changes: 41 additions & 24 deletions src/main/java/coordinate/PrintView.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,65 @@
package coordinate;

import java.util.*;

public class PrintView {
private static final int AXIS_MAX = 24;
private static final String VERTICAL = "│";
private static final String ZERO_POINT = "┼";
private static final String ROW = "───";
private static final String BLANK = " "; //1칸
private static final String BLANK_TWO = " ";
private static final String BLANK_FOUR = " ";
private static final int AXIS_MAX = 24;
private static final String THREE_STRING = "%3s";
private static final String SIX_STRING = "%6s";
private static final String BLANK = " ";

public static void drawVertical(List<Point> points) {
for (int y = AXIS_MAX; y > 0; y--) {
if (y % 2 == 0)
System.out.printf(THREE_STRING, String.valueOf(y) + VERTICAL);
if (y % 2 == 1)
System.out.printf(THREE_STRING, VERTICAL);
drawDotSpace(y, points);
System.out.println();

public static void drawVertical() {
for (int i = AXIS_MAX; i > 0; i--) {
if (i % 2 == 1) {
System.out.print(BLANK_TWO);
System.out.println(VERTICAL);
continue;
}
if (i < 10)
}
}

private static void drawDotSpace(int y, List<Point> points) {
for (int x = 1; x <= AXIS_MAX * ROW.length(); x++) {
if(drawDot(y, points, x))
System.out.print(BLANK);
System.out.print(i);
System.out.println(VERTICAL);
}
}

private static Boolean drawDot(int y, List<Point> points, int x) {
String dot = "●";
for (Point point : points) {
if(point.isY(y) && point.isX(x)) {
System.out.print(dot);
return false;
}
}
return true;
}

public static void drawRow() {
System.out.print(BLANK_TWO);
System.out.print(ZERO_POINT);
for (int i = 0; i < AXIS_MAX-1; i++) {
System.out.printf(THREE_STRING, ZERO_POINT);
for (int i = 0; i < AXIS_MAX; i++) {
System.out.print(ROW);
}
System.out.println();
}

public static void rowNumber() {
System.out.print(BLANK_TWO);
System.out.print("0");
System.out.printf(THREE_STRING, String.valueOf(0));
for (int i = 1; i <= AXIS_MAX; i++) {
if (i % 2 == 0) {
System.out.print(BLANK_FOUR);
System.out.print(i);
}
if (i % 2 == 0)
System.out.printf(SIX_STRING, String.valueOf(i));
}
System.out.println();
}


public static void printLength(double lineLength) {
System.out.print("두 점 사이 거리는 ");
System.out.println(lineLength);
}
}
15 changes: 15 additions & 0 deletions src/test/java/CoordinateTest/CoordinateLineTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package coordinateTest;

import org.junit.Test;

public class CoordinateLineTest {
@Test
public void isIntegerTest() {
int a = 4;
double b = Math.sqrt(16);
if (a == (int) b)
System.out.println("트루");
System.out.println("거짓");
System.out.println((int) Math.sqrt(16));
}
}
96 changes: 96 additions & 0 deletions src/test/java/CoordinateTest/InputViewTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package coordinateTest;

import org.junit.Test;

import java.util.*;
import java.util.regex.*;

public class InputViewTest {
@Test
public void extractValue() {
String a = "<10,10)-<14,15)"; //pattern matcher 클래스를 통한 숫자 추출
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(a);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
}

@Test
public void stringSplit() {
String spaceString = "ABC, 1, DEFg, HIJK"; //공백 제거
String[] arr = spaceString.split(",|\\s+");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i].length());
}
System.out.println(arr.length);
}

@Test
public void extractNumberBySplit() {
// String b = "";
String a = "(10,10)-(14,15)"; //split 으로 숫자 추출 하려고 했 지만 비어있는 값을 반환해서 StringTokenizer 로 쪼갬
List<String> nums = new ArrayList<>();
StringTokenizer stz = new StringTokenizer(a, "\\(|,|\\)");

while (stz.hasMoreElements()) {
nums.add(stz.nextToken());
}
System.out.println(nums);

// String[] num = a.split("\\(|,|\\)|-");
// System.out.println(Arrays.toString(num));
// System.out.println(num.length);
}

@Test
public void splitPractice() { // split 으로 2개의 문자를 기준으로 자르기
String a = "AA.BB-CC-DD.zip";
String[] tokens = a.split("\\.|-");
System.out.println(Arrays.toString(tokens));
}

@Test
public void splitTest() { //split 으로 자르는 횟수 정하기
String a = "a,b,c,d";
System.out.println(Arrays.toString(a.split(",", 2)));
}

@Test
public void forEachTest() {
String[] a = {"aa", "bb", "cc", "dd"};
for (String b : a) {
System.out.println(b);
}
}

@Test
public void splitDashTest() {
String[] numberWithDash = {"10", "10", "-", "14", "15"};
System.out.println(Arrays.toString(Arrays.toString(numberWithDash).split("-")));
System.out.println(Arrays.toString(numberWithDash).split("-").length);
}

@Test
public void diasassembleAndInputArray() {
String a = "(10,10)-(20,20)";
StringTokenizer stz = new StringTokenizer(a, "\\(|\\)|,|-");
int[][] arr = new int[stz.countTokens() / 2][2];

for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = Integer.parseInt(stz.nextToken());
}
}

for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.println(arr[i][j]);
}
}
}


}


Loading