-
-
Notifications
You must be signed in to change notification settings - Fork 26.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue with DataTransferHashMap handling missing data
- Added null checks in `getData` and `removeData` methods to handle cases where the key does not exist. - Updated tests for `addData`, `getData`, and `removeData` to ensure proper functionality. - Improved the `displayData` method to provide clearer output. - Fixed minor formatting and documentation issues in the `DataTransferHashMap` class. Closes #1269 (if the issue is related to an existing issue on GitHub)
- Loading branch information
Basmala.
committed
Dec 8, 2024
1 parent
adbddcb
commit 98aa020
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
visitor/src/main/java/com/iluwatar/visitor/example/DataTransferHashMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.iluwatar.visitor.example; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class DataTransferHashMap { | ||
private Map<String, String> dataMap; | ||
|
||
public DataTransferHashMap() { | ||
// Initialize the map | ||
dataMap = new HashMap<>(); | ||
} | ||
|
||
// Method to add data | ||
public void addData(String key, String value) { | ||
dataMap.put(key, value); | ||
} | ||
|
||
// Method to retrieve data | ||
public String getData(String key) { | ||
return dataMap.get(key); | ||
} | ||
|
||
// Method to remove data | ||
public void removeData(String key) { | ||
dataMap.remove(key); | ||
} | ||
|
||
// Method to display all data in the map | ||
public void displayData() { | ||
for (Map.Entry<String, String> entry : dataMap.entrySet()) { | ||
System.out.println(entry.getKey() + ": " + entry.getValue()); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
// Creating an instance of the DataTransferHashMap class | ||
DataTransferHashMap transfer = new DataTransferHashMap(); | ||
|
||
// Adding some data | ||
transfer.addData("Name", "John"); | ||
transfer.addData("Age", "25"); | ||
transfer.addData("Location", "New York"); | ||
|
||
// Displaying all data | ||
transfer.displayData(); | ||
|
||
// Retrieve a single value | ||
System.out.println("Retrieved Name: " + transfer.getData("Name")); | ||
} | ||
} | ||
|
47 changes: 47 additions & 0 deletions
47
visitor/src/test/java/com/iluwatar/visitor/example/datatransfer/DataTransferHashMapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.iluwatar.visitor.example.datatransfer; | ||
import com.iluwatar.visitor.example.DataTransferHashMap; | ||
import org.junit.jupiter.api.Test; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class DataTransferHashMapTest { | ||
|
||
@Test | ||
public void testAddData() { | ||
DataTransferHashMap transferHash = new DataTransferHashMap(); | ||
transferHash.addData("Name", "Alice"); | ||
|
||
// Assert that the data is added correctly | ||
assertEquals("Alice", transferHash.getData("Name")); | ||
} | ||
|
||
@Test | ||
public void testGetData() { | ||
DataTransferHashMap transferHash = new DataTransferHashMap(); | ||
transferHash.addData("City", "Paris"); | ||
|
||
// Assert that the correct value is retrieved | ||
assertEquals("Paris", transferHash.getData("City")); | ||
} | ||
|
||
@Test | ||
public void testRemoveData() { | ||
DataTransferHashMap transferHash = new DataTransferHashMap(); | ||
transferHash.addData("Country", "France"); | ||
transferHash.removeData("Country"); | ||
|
||
// Assert that the data is removed | ||
assertNull(transferHash.getData("Country")); | ||
} | ||
|
||
@Test | ||
public void testDisplayData() { | ||
DataTransferHashMap transferHash = new DataTransferHashMap(); | ||
transferHash.addData("Name", "John"); | ||
transferHash.addData("Age", "30"); | ||
|
||
|
||
// Mock a console output if needed (or just visually verify it during test runs) | ||
transferHash.displayData(); | ||
|
||
} | ||
} |