class Point {
private readonly x: number;
private readonly y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
// Getters
getX(): number {
return this.x;
}
getY(): number {
return this.y;
}
// toString for display
toString(): string {
return `(${this.x}, ${this.y})`;
}
// Custom key for Map
key(): string {
return `${this.x},${this.y}`;
}
// Static method to parse key back to Point
static fromKey(key: string): Point {
const [x, y] = key.split(',').map(Number);
return new Point(x, y);
}
}
// Simulate Map<Point, string> using string keys
const map: Map<string, string> = new Map();
map.set(new Point(2, 7).key(), "A");
map.set(new Point(3, 6).key(), "B");
map.set(new Point(0, 0).key(), "C");
// Print x and y separately
for (const [key, value] of map.entries()) {
const point = Point.fromKey(key);
console.log(`x: ${point.getX()}, y: ${point.getY()} => ${value}`);
}
/*
run:
"x: 2, y: 7 => A"
"x: 3, y: 6 => B"
"x: 0, y: 0 => C"
*/