File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .jar ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .FileNotFoundException ;
5+ import java .io .IOException ;
6+ import java .io .InputStreamReader ;
7+ import java .util .ArrayList ;
8+ import java .util .List ;
9+
10+ public class Dimensioner {
11+
12+ public List <Rectangle > loadFromFile (String dimensionFile ) throws FileNotFoundException , IOException {
13+ List <Rectangle > rectangles = new ArrayList <Rectangle >();
14+ try (BufferedReader br = new BufferedReader (new InputStreamReader (Dimensioner .class .getResourceAsStream (dimensionFile )))) {
15+ String line ;
16+ while ((line = br .readLine ()) != null ) {
17+ String [] dimensions = line .split ("," );
18+ if (dimensions .length == 2 ) {
19+ Rectangle rectangle = new Rectangle (Integer .valueOf (dimensions [0 ]), Integer .valueOf (dimensions [1 ]));
20+ rectangles .add (rectangle );
21+ }
22+ }
23+ }
24+ return rectangles ;
25+ }
26+
27+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .jar ;
2+
3+ import java .io .IOException ;
4+ import java .util .List ;
5+
6+ public class JarExample {
7+ private static final String DIMENSION_FILE = "/dimensions.txt" ;
8+
9+ public static void main (String [] args ) {
10+ String environment = System .getProperty ("environment" );
11+ if (environment != null && environment .equalsIgnoreCase ("prod" )) {
12+ Dimensioner dimensioner = new Dimensioner ();
13+ try {
14+ List <Rectangle > rectangles = dimensioner .loadFromFile (DIMENSION_FILE );
15+ rectangles .forEach (rectangle -> {
16+ rectangle .printArea ();
17+ rectangle .printPerimeter ();
18+ });
19+ } catch (IOException e ) {
20+ System .err .println ("Exception loading dimensions" );
21+ }
22+ } else if (args .length > 0 ) {
23+ int length = Integer .valueOf (args [0 ]);
24+ int width = (args .length > 1 ) ? Integer .valueOf (args [1 ]) : Integer .valueOf (args [0 ]);
25+ Rectangle rectangle = new Rectangle (length , width );
26+ rectangle .printArea ();
27+ rectangle .printPerimeter ();
28+ }
29+ }
30+
31+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .jar ;
2+
3+ public class Rectangle {
4+ private int length ;
5+ private int width ;
6+
7+ public Rectangle (int length , int width ) {
8+ this .length = length ;
9+ this .width = width ;
10+ }
11+
12+ public int area () {
13+ return length * width ;
14+ }
15+
16+ public int perimeter () {
17+ return (length * 2 ) + (width * 2 );
18+ }
19+
20+ public void printArea () {
21+ System .out .println ("Area: " + area ());
22+ }
23+
24+ public void printPerimeter () {
25+ System .out .println ("Perimeter: " + perimeter ());
26+ }
27+
28+ public int getLength () {
29+ return length ;
30+ }
31+
32+ public void setLength (int length ) {
33+ this .length = length ;
34+ }
35+
36+ public int getWidth () {
37+ return width ;
38+ }
39+
40+ public void setWidth (int width ) {
41+ this .width = width ;
42+ }
43+
44+ }
Original file line number Diff line number Diff line change 1+ Main-Class: com.baeldung.jar.JarExample
Original file line number Diff line number Diff line change 1+ 12,16
2+ 24,6
3+ 7,19
You can’t perform that action at this time.
0 commit comments