BAEL-2542 Creating a Jar - example code
This commit is contained in:
parent
667ecbb556
commit
720c32ab78
|
@ -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<Rectangle> loadFromFile(String dimensionFile) throws FileNotFoundException, IOException {
|
||||
List<Rectangle> rectangles = new ArrayList<Rectangle>();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<Rectangle> 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
Main-Class: com.baeldung.jar.JarExample
|
|
@ -0,0 +1,3 @@
|
|||
12,16
|
||||
24,6
|
||||
7,19
|
Loading…
Reference in New Issue