From 64a24a0b9a829a5a9a6498bf7bd6845485f00006 Mon Sep 17 00:00:00 2001 From: vishwesh Date: Sat, 7 Oct 2023 17:25:51 -0700 Subject: [PATCH] Implement second feature. --- src/main/java/GraphData.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/main/java/GraphData.java b/src/main/java/GraphData.java index 97a1c21..c415b52 100644 --- a/src/main/java/GraphData.java +++ b/src/main/java/GraphData.java @@ -47,11 +47,31 @@ public void outputGraph(String filepath) throws IOException { String opt = toString(); Files.writeString(Paths.get(filepath), opt, StandardCharsets.ISO_8859_1); } + + public void addNode(String label) { + boolean existing = graphObject.vertexSet().stream().anyMatch(v -> Objects.equals(v, label)); + + if (existing) { + System.out.println("Node with label "+label+" already exists!"); + } + else { + graphObject.addVertex(label); + } + } + + public void addNodes(String[] labels) { + for(String label: labels) { + addNode(label); + } + } public static void main(String[] args) throws IOException { GraphData graphApi = new GraphData(); graphApi.parseGraph("src/main/example.dot"); System.out.println(graphApi.toString()); graphApi.outputGraph("src/main/output.txt"); + graphApi.addNodes(new String[]{"Z", "X"}); + System.out.println(graphApi.toString()); +