Merge remote-tracking branch 'upstream/master'
This commit is contained in:
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.maxdate;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class DateComparison {
|
||||
public int compareTodayWithMaxDate() {
|
||||
Date today = new Date();
|
||||
Date maxDate = new Date(Long.MAX_VALUE);
|
||||
|
||||
int comparisonResult = today.compareTo(maxDate);
|
||||
return comparisonResult;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
DateComparison comparator = new DateComparison();
|
||||
System.out.println(comparator.compareTodayWithMaxDate());
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.maxdate;
|
||||
|
||||
import java.util.Date;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class MaxDateDisplay {
|
||||
public String getMaxDateValue() {
|
||||
Date maxDate = new Date(Long.MAX_VALUE);
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
||||
return "The maximum date value in Java is: " + sdf.format(maxDate);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
MaxDateDisplay display = new MaxDateDisplay();
|
||||
System.out.println(display.getMaxDateValue());
|
||||
}
|
||||
}
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.maxdate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DateComparisonUnitTest {
|
||||
|
||||
@Test
|
||||
void whenCompareTodayWithMaxDate_thenCorrectResult() {
|
||||
DateComparison comparator = new DateComparison();
|
||||
int result = comparator.compareTodayWithMaxDate();
|
||||
|
||||
assertTrue(result < 0);
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.maxdate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MaxDateDisplayUnitTest {
|
||||
|
||||
@Test
|
||||
void whenGetMaxDate_thenCorrectResult() {
|
||||
MaxDateDisplay display = new MaxDateDisplay();
|
||||
String result = display.getMaxDateValue();
|
||||
assertEquals(
|
||||
"The maximum date value in Java is: 292278994-08-17 07:12:55.807",
|
||||
result
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
+44
@@ -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);
|
||||
}
|
||||
}
|
||||
+62
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.size;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class FileSizeUtils {
|
||||
public static long getFileSizeInBytes(File file) {
|
||||
if (file.exists()) {
|
||||
return file.length();
|
||||
} else {
|
||||
throw new IllegalArgumentException("File not found.");
|
||||
}
|
||||
}
|
||||
|
||||
public static double getFileSizeInKilobytes(File file) {
|
||||
long bytes = getFileSizeInBytes(file);
|
||||
return (double) bytes / 1024;
|
||||
}
|
||||
|
||||
public static double getFileSizeInMegabytes(File file) {
|
||||
double kilobytes = getFileSizeInKilobytes(file);
|
||||
return kilobytes / 1024;
|
||||
}
|
||||
|
||||
public static double getFileSizeInGigabytes(File file) {
|
||||
double megabytes = getFileSizeInMegabytes(file);
|
||||
return megabytes / 1024;
|
||||
}
|
||||
}
|
||||
+26
-5
@@ -1,6 +1,8 @@
|
||||
package com.baeldung.size;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
@@ -10,9 +12,9 @@ import java.net.URL;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class JavaFileSizeUnitTest {
|
||||
private static final long EXPECTED_FILE_SIZE_IN_BYTES = 11;
|
||||
@@ -85,4 +87,23 @@ public class JavaFileSizeUnitTest {
|
||||
assertEquals(EXPECTED_FILE_SIZE_IN_BYTES, stream.available());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetFileSizeInDifferentUnits_thenCorrect(){
|
||||
filePath = String.join(File.separator, new String[] { "src", "test", "resources", "size", "sample_file_1.in" });
|
||||
File file = new File(filePath);
|
||||
if (file.exists()) {
|
||||
long expectedBytes = file.length();
|
||||
double expectedKilobytes = (double) expectedBytes / 1024;
|
||||
double expectedMegabytes = expectedKilobytes / 1024;
|
||||
double expectedGigabytes = expectedMegabytes / 1024;
|
||||
|
||||
assertEquals(expectedBytes, FileSizeUtils.getFileSizeInBytes(file));
|
||||
assertEquals(expectedKilobytes, FileSizeUtils.getFileSizeInKilobytes(file), 0.01);
|
||||
assertEquals(expectedMegabytes, FileSizeUtils.getFileSizeInMegabytes(file), 0.01);
|
||||
assertEquals(expectedGigabytes, FileSizeUtils.getFileSizeInGigabytes(file), 0.01);
|
||||
} else {
|
||||
fail("File not found.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user