Skip to content

Commit 311158e

Browse files
committed
Fix
1 parent a2ba20f commit 311158e

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

src/main/java/com/thealgorithms/others/SkylineProblem.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,27 @@ public class SkylineProblem {
3434
/**
3535
* Array of buildings to process. Must be initialized before use.
3636
*/
37-
public Building[] building;
37+
private Building[] building;
3838

3939
/**
4040
* Number of buildings added so far.
4141
*/
4242
public int count;
4343

44+
/**
45+
* Sets the building array to the specified size.
46+
*
47+
* @param size The size of the building array.
48+
* @throws IllegalArgumentException if size is negative
49+
*/
50+
public void setBuilding(int size) {
51+
if (size < 0) {
52+
throw new IllegalArgumentException("Size must be non-negative");
53+
}
54+
this.building = new Building[size];
55+
this.count = 0;
56+
}
57+
4458
/**
4559
* Adds a building with the given left, height, and right values to the
4660
* buildings list.

src/test/java/com/thealgorithms/others/SkylineProblemTest.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class SkylineProblemTest {
1313
@Test
1414
void testSingleBuildingSkyline() {
1515
SkylineProblem skylineProblem = new SkylineProblem();
16-
skylineProblem.building = new SkylineProblem.Building[1];
16+
skylineProblem.setBuilding(1);
1717
skylineProblem.add(2, 10, 9);
1818

1919
List<SkylineProblem.Skyline> result = skylineProblem.findSkyline(0, 0);
@@ -24,7 +24,7 @@ void testSingleBuildingSkyline() {
2424
@Test
2525
void testTwoBuildingsSkyline() {
2626
SkylineProblem skylineProblem = new SkylineProblem();
27-
skylineProblem.building = new SkylineProblem.Building[2];
27+
skylineProblem.setBuilding(2);
2828
skylineProblem.add(1, 11, 5);
2929
skylineProblem.add(2, 6, 7);
3030

@@ -48,7 +48,7 @@ void testMergeSkyline() {
4848
@Test
4949
void testMultipleBuildingsSkyline() {
5050
SkylineProblem skylineProblem = new SkylineProblem();
51-
skylineProblem.building = new SkylineProblem.Building[3];
51+
skylineProblem.setBuilding(3);
5252
skylineProblem.add(1, 10, 5);
5353
skylineProblem.add(2, 15, 7);
5454
skylineProblem.add(3, 12, 9);
@@ -65,15 +65,15 @@ void testAddBuildingInvalidCases() {
6565
Exception ex = assertThrows(IllegalStateException.class, () -> skylineProblem.add(1, 2, 3));
6666
assertTrue(ex.getMessage().contains("not initialized"));
6767

68-
skylineProblem.building = new SkylineProblem.Building[1];
68+
skylineProblem.setBuilding(1);
6969
skylineProblem.add(1, 2, 3);
7070
// Array full
7171
Exception ex2 = assertThrows(IllegalStateException.class, () -> skylineProblem.add(4, 5, 6));
7272
assertTrue(ex2.getMessage().contains("full"));
7373

7474
// Invalid left >= right
7575
SkylineProblem skylineProblem2 = new SkylineProblem();
76-
skylineProblem2.building = new SkylineProblem.Building[1];
76+
skylineProblem2.setBuilding(1);
7777
Exception ex3 = assertThrows(IllegalArgumentException.class, () -> skylineProblem2.add(5, 2, 2));
7878
assertTrue(ex3.getMessage().contains("Left coordinate"));
7979

@@ -89,7 +89,7 @@ void testFindSkylineInvalidCases() {
8989
Exception ex = assertThrows(IllegalArgumentException.class, () -> skylineProblem.findSkyline(0, 0));
9090
assertTrue(ex.getMessage().contains("not initialized"));
9191

92-
skylineProblem.building = new SkylineProblem.Building[2];
92+
skylineProblem.setBuilding(2);
9393
skylineProblem.count = 1;
9494
Exception ex2 = assertThrows(IllegalArgumentException.class, () -> skylineProblem.findSkyline(0, 1));
9595
assertTrue(ex2.getMessage().contains("Invalid start or end index"));
@@ -98,18 +98,8 @@ void testFindSkylineInvalidCases() {
9898
@Test
9999
void testMergeSkylineNullCases() {
100100
SkylineProblem skylineProblem = new SkylineProblem();
101-
Exception ex1 = assertThrows(NullPointerException.class, new org.junit.jupiter.api.function.Executable() {
102-
@Override
103-
public void execute() {
104-
skylineProblem.mergeSkyline(null, List.of());
105-
}
106-
});
107-
Exception ex2 = assertThrows(NullPointerException.class, new org.junit.jupiter.api.function.Executable() {
108-
@Override
109-
public void execute() {
110-
skylineProblem.mergeSkyline(List.of(), null);
111-
}
112-
});
101+
Exception ex1 = assertThrows(NullPointerException.class, () -> skylineProblem.mergeSkyline(null, List.of()));
102+
Exception ex2 = assertThrows(NullPointerException.class, () -> skylineProblem.mergeSkyline(List.of(), null));
113103
assertTrue(ex1.getMessage().contains("sky1"));
114104
assertTrue(ex2.getMessage().contains("sky2"));
115105
}

0 commit comments

Comments
 (0)