Create Table using ASCII in a Console in Java (#15158)
This commit is contained in:
parent
4d9a5ea8b4
commit
de50130c74
|
@ -29,6 +29,11 @@
|
|||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.vandermeer</groupId>
|
||||
<artifactId>asciitable</artifactId>
|
||||
<version>${ascii.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -157,6 +162,7 @@
|
|||
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
||||
<source.version>1.8</source.version>
|
||||
<target.version>1.8</target.version>
|
||||
<ascii.version>0.3.2</ascii.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.consoletableoutput;
|
||||
|
||||
public class BodyMassIndex {
|
||||
|
||||
private String name;
|
||||
private double height;
|
||||
private double weight;
|
||||
|
||||
public BodyMassIndex(String name, double height, double weight) {
|
||||
this.name = name;
|
||||
this.height = height;
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public double getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public void setHeight(double height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public double getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public void setWeight(double weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public double calculate() {
|
||||
double bmi = weight / (height * height);
|
||||
String formattedBmi = String.format("%.2f", bmi);
|
||||
return Double.parseDouble(formattedBmi);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package com.baeldung.consoletableoutput;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import de.vandermeer.asciitable.AsciiTable;
|
||||
import de.vandermeer.skb.interfaces.transformers.textformat.TextAlignment;
|
||||
|
||||
public class BodyMassIndexApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
stringFormat();
|
||||
asciiTable();
|
||||
|
||||
}
|
||||
|
||||
public static void stringFormat() {
|
||||
List<BodyMassIndex> bodyMassIndices = new ArrayList<>();
|
||||
bodyMassIndices.add(new BodyMassIndex("Tom", 1.8, 80));
|
||||
bodyMassIndices.add(new BodyMassIndex("Elton", 1.9, 90));
|
||||
bodyMassIndices.add(new BodyMassIndex("Harry", 1.9, 90));
|
||||
bodyMassIndices.add(new BodyMassIndex("Hannah", 1.9, 90));
|
||||
|
||||
String leftAlignment = "| %-7s | %-7.2f | %-7.2f | %-5.2f |%n";
|
||||
|
||||
System.out.format("+---------+---------+---------+-------+%n");
|
||||
System.out.format("| Name | Height | Weight | BMI |%n");
|
||||
System.out.format("+---------+---------+---------+-------+%n");
|
||||
|
||||
for (BodyMassIndex bodyMassIndex : bodyMassIndices) {
|
||||
System.out.format(leftAlignment, bodyMassIndex.getName(), bodyMassIndex.getHeight(), bodyMassIndex.getWeight(), bodyMassIndex.calculate());
|
||||
System.out.format("+---------+---------+---------+-------+%n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void asciiTable() {
|
||||
List<BodyMassIndex> bodyMassIndices = new ArrayList<>();
|
||||
bodyMassIndices.add(new BodyMassIndex("Tom", 1.8, 80));
|
||||
bodyMassIndices.add(new BodyMassIndex("Elton", 1.9, 90));
|
||||
bodyMassIndices.add(new BodyMassIndex("Harry", 1.9, 90));
|
||||
bodyMassIndices.add(new BodyMassIndex("Hannah", 1.9, 90));
|
||||
|
||||
AsciiTable asciiTable = new AsciiTable();
|
||||
asciiTable.addRule();
|
||||
asciiTable.addRow("Name", "Height", "Weight", "BMI");
|
||||
asciiTable.addRule();
|
||||
|
||||
for (BodyMassIndex bodyMassIndex : bodyMassIndices) {
|
||||
|
||||
asciiTable.addRow(bodyMassIndex.getName(), bodyMassIndex.getHeight(), bodyMassIndex.getWeight(), bodyMassIndex.calculate());
|
||||
asciiTable.addRule();
|
||||
|
||||
}
|
||||
|
||||
asciiTable.setTextAlignment(TextAlignment.CENTER);
|
||||
String render = asciiTable.render();
|
||||
System.out.println(render);
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue