From 720c32ab78b18e3fc092700e674c4e84724ef33b Mon Sep 17 00:00:00 2001 From: amdegregorio Date: Sat, 26 Jan 2019 09:00:26 -0500 Subject: [PATCH] BAEL-2542 Creating a Jar - example code --- .../java/com/baeldung/jar/Dimensioner.java | 27 ++++++++++++ .../java/com/baeldung/jar/JarExample.java | 31 +++++++++++++ .../main/java/com/baeldung/jar/Rectangle.java | 44 +++++++++++++++++++ .../resources/META-INF/example_manifest.txt | 1 + .../src/main/resources/files/dimensions.txt | 3 ++ 5 files changed, 106 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/jar/Dimensioner.java create mode 100644 core-java/src/main/java/com/baeldung/jar/JarExample.java create mode 100644 core-java/src/main/java/com/baeldung/jar/Rectangle.java create mode 100644 core-java/src/main/resources/META-INF/example_manifest.txt create mode 100644 core-java/src/main/resources/files/dimensions.txt diff --git a/core-java/src/main/java/com/baeldung/jar/Dimensioner.java b/core-java/src/main/java/com/baeldung/jar/Dimensioner.java new file mode 100644 index 0000000000..bc5cea4010 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/jar/Dimensioner.java @@ -0,0 +1,27 @@ +package com.baeldung.jar; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; + +public class Dimensioner { + + public List loadFromFile(String dimensionFile) throws FileNotFoundException, IOException { + List rectangles = new ArrayList(); + try (BufferedReader br = new BufferedReader(new InputStreamReader(Dimensioner.class.getResourceAsStream(dimensionFile)))) { + String line; + while ((line = br.readLine()) != null) { + String[] dimensions = line.split(","); + if (dimensions.length == 2) { + Rectangle rectangle = new Rectangle(Integer.valueOf(dimensions[0]), Integer.valueOf(dimensions[1])); + rectangles.add(rectangle); + } + } + } + return rectangles; + } + +} diff --git a/core-java/src/main/java/com/baeldung/jar/JarExample.java b/core-java/src/main/java/com/baeldung/jar/JarExample.java new file mode 100644 index 0000000000..262df3a5ec --- /dev/null +++ b/core-java/src/main/java/com/baeldung/jar/JarExample.java @@ -0,0 +1,31 @@ +package com.baeldung.jar; + +import java.io.IOException; +import java.util.List; + +public class JarExample { + private static final String DIMENSION_FILE = "/dimensions.txt"; + + public static void main(String[] args) { + String environment = System.getProperty("environment"); + if (environment != null && environment.equalsIgnoreCase("prod")) { + Dimensioner dimensioner = new Dimensioner(); + try { + List rectangles = dimensioner.loadFromFile(DIMENSION_FILE); + rectangles.forEach(rectangle -> { + rectangle.printArea(); + rectangle.printPerimeter(); + }); + } catch (IOException e) { + System.err.println("Exception loading dimensions"); + } + } else if (args.length > 0) { + int length = Integer.valueOf(args[0]); + int width = (args.length > 1) ? Integer.valueOf(args[1]) : Integer.valueOf(args[0]); + Rectangle rectangle = new Rectangle(length, width); + rectangle.printArea(); + rectangle.printPerimeter(); + } + } + +} diff --git a/core-java/src/main/java/com/baeldung/jar/Rectangle.java b/core-java/src/main/java/com/baeldung/jar/Rectangle.java new file mode 100644 index 0000000000..f2a174c819 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/jar/Rectangle.java @@ -0,0 +1,44 @@ +package com.baeldung.jar; + +public class Rectangle { + private int length; + private int width; + + public Rectangle(int length, int width) { + this.length = length; + this.width = width; + } + + public int area() { + return length * width; + } + + public int perimeter() { + return (length * 2) + (width * 2); + } + + public void printArea() { + System.out.println("Area: " + area()); + } + + public void printPerimeter() { + System.out.println("Perimeter: " + perimeter()); + } + + public int getLength() { + return length; + } + + public void setLength(int length) { + this.length = length; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + +} diff --git a/core-java/src/main/resources/META-INF/example_manifest.txt b/core-java/src/main/resources/META-INF/example_manifest.txt new file mode 100644 index 0000000000..90e83e9b42 --- /dev/null +++ b/core-java/src/main/resources/META-INF/example_manifest.txt @@ -0,0 +1 @@ +Main-Class: com.baeldung.jar.JarExample diff --git a/core-java/src/main/resources/files/dimensions.txt b/core-java/src/main/resources/files/dimensions.txt new file mode 100644 index 0000000000..0397c2a610 --- /dev/null +++ b/core-java/src/main/resources/files/dimensions.txt @@ -0,0 +1,3 @@ +12,16 +24,6 +7,19 \ No newline at end of file