updates to simplify example
This commit is contained in:
parent
66c92495fb
commit
499aec3490
@ -1,27 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,31 +1,9 @@
|
|||||||
package com.baeldung.jar;
|
package com.baeldung.jar;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class JarExample {
|
public class JarExample {
|
||||||
private static final String DIMENSION_FILE = "/dimensions.txt";
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String inputType = System.getProperty("input");
|
System.out.println("Hello Baeldung Reader!");
|
||||||
if (inputType != null && inputType.equalsIgnoreCase("file")) {
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,44 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user