programmieren-projekt/src/Point.java

46 lines
930 B
Java

public class Point {
private int x;
private int y;
public Point (int x, int y) {
this.setX(x);
this.setY(y);
}
public Point (String str) {
if (Point.isValidSyntax(str)) {
this.setX(str.charAt(0));
this.setY(Integer.parseInt(str.substring(1)));
} else {
throw new IllegalArgumentException("String ist keine gültige Koordinate");
}
}
@Override
public String toString() {
return (char) ('A' + this.x) + String.valueOf(this.y);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setX(char c) {
this.x = c - 'A';
}
public static boolean isValidSyntax(String str) {
return str.matches("^[A-Z]\\d+$");
}
}