geroza
2018-11-10 11:13:04 -02:00
parent 55c49b25f1
commit 7249a2c88d
44 changed files with 572 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
package com.baeldung.doubles;
import java.math.BigDecimal;
public class SplitFloatingPointNumbers {
public static void main(String[] args) {
double doubleNumber = 24.04;
splitUsingFloatingTypes(doubleNumber);
splitUsingString(doubleNumber);
splitUsingBigDecimal(doubleNumber);
}
private static void splitUsingFloatingTypes(double doubleNumber) {
System.out.println("Using Floating Point Arithmetics:");
int intPart = (int) doubleNumber;
System.out.println("Double Number: "+doubleNumber);
System.out.println("Integer Part: "+ intPart);
System.out.println("Decimal Part: "+ (doubleNumber - intPart));
}
private static void splitUsingString(double doubleNumber) {
System.out.println("Using String Operations:");
String doubleAsString = String.valueOf(doubleNumber);
int indexOfDecimal = doubleAsString.indexOf(".");
System.out.println("Double Number: "+doubleNumber);
System.out.println("Integer Part: "+ doubleAsString.substring(0, indexOfDecimal));
System.out.println("Decimal Part: "+ doubleAsString.substring(indexOfDecimal));
}
private static void splitUsingBigDecimal(double doubleNumber) {
System.out.println("Using BigDecimal Operations:");
BigDecimal bigDecimal = new BigDecimal(String.valueOf(doubleNumber));
int intValue = bigDecimal.intValue();
System.out.println("Double Number: "+bigDecimal.toPlainString());
System.out.println("Integer Part: "+intValue);
System.out.println("Decimal Part: "+bigDecimal.subtract(new BigDecimal(intValue)).toPlainString());
}
}

View File

@@ -0,0 +1,27 @@
package com.baeldung.inheritancecomposition.application;
import com.baeldung.inheritancecomposition.model.Actress;
import com.baeldung.inheritancecomposition.model.Computer;
import com.baeldung.inheritancecomposition.model.StandardMemory;
import com.baeldung.inheritancecomposition.model.Person;
import com.baeldung.inheritancecomposition.model.StandardProcessor;
import com.baeldung.inheritancecomposition.model.StandardSoundCard;
import com.baeldung.inheritancecomposition.model.Waitress;
public class Application {
public static void main(String[] args) {
Person person = new Person("John", "john@domain.com", 35);
Waitress waitress = new Waitress("Mary", "mary@domain.com", 22);
System.out.println(waitress.serveStarter("mixed salad"));
System.out.println(waitress.serveMainCourse("steak"));
System.out.println(waitress.serveDessert("cup of cofee"));
Actress actress = new Actress("Susan", "susan@domain.com", 30);
System.out.println(actress.readScript("Psycho"));
System.out.println(actress.performRole());
Computer computer = new Computer(new StandardProcessor("Intel I3"), new StandardMemory("Kingston", "1TB"));
computer.setSoundCard(new StandardSoundCard("Generic Sound Card"));
System.out.println(computer);
}
}

View File

@@ -0,0 +1,16 @@
package com.baeldung.inheritancecomposition.model;
public class Actress extends Person {
public Actress(String name, String email, int age) {
super(name, email, age);
}
public String readScript(String movie) {
return "Reading the script of " + movie;
}
public String performRole() {
return "Performing a role";
}
}

View File

@@ -0,0 +1,36 @@
package com.baeldung.inheritancecomposition.model;
import java.util.Optional;
public class Computer {
private Processor processor;
private Memory memory;
private SoundCard soundCard;
public Computer(Processor processor, Memory memory) {
this.processor = processor;
this.memory = memory;
}
public void setSoundCard(SoundCard soundCard) {
this.soundCard = soundCard;
}
public Processor getProcessor() {
return processor;
}
public Memory getMemory() {
return memory;
}
public Optional<SoundCard> getSoundCard() {
return Optional.ofNullable(soundCard);
}
@Override
public String toString() {
return "Computer{" + "processor=" + processor + ", memory=" + memory + ", soundcard=" + soundCard +"}";
}
}

View File

@@ -0,0 +1,8 @@
package com.baeldung.inheritancecomposition.model;
public interface Memory {
String getBrand();
String getSize();
}

View File

@@ -0,0 +1,31 @@
package com.baeldung.inheritancecomposition.model;
public class Person {
private final String name;
private final String email;
private final int age;
public Person(String name, String email, int age) {
this.name = name;
this.email = email;
this.age = age;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person{" + "name=" + name + ", email=" + email + ", age=" + age + "}";
}
}

View File

@@ -0,0 +1,6 @@
package com.baeldung.inheritancecomposition.model;
public interface Processor {
String getModel();
}

View File

@@ -0,0 +1,6 @@
package com.baeldung.inheritancecomposition.model;
public interface SoundCard {
String getBrand();
}

View File

@@ -0,0 +1,25 @@
package com.baeldung.inheritancecomposition.model;
public class StandardMemory implements Memory {
private String brand;
private String size;
public StandardMemory(String brand, String size) {
this.brand = brand;
this.size = size;
}
public String getBrand() {
return brand;
}
public String getSize() {
return size;
}
@Override
public String toString() {
return "Memory{" + "brand=" + brand + ", size=" + size + "}";
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.inheritancecomposition.model;
public class StandardProcessor implements Processor {
private String model;
public StandardProcessor(String model) {
this.model = model;
}
@Override
public String getModel() {
return model;
}
@Override
public String toString() {
return "Processor{" + "model=" + model + "}";
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.inheritancecomposition.model;
public class StandardSoundCard implements SoundCard {
private String brand;
public StandardSoundCard(String brand) {
this.brand = brand;
}
@Override
public String getBrand() {
return brand;
}
@Override
public String toString() {
return "SoundCard{" + "brand=" + brand + "}";
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.inheritancecomposition.model;
public class Waitress extends Person {
public Waitress(String name, String email, int age) {
super(name, email, age);
}
public String serveStarter(String starter) {
return "Serving a " + starter;
}
public String serveMainCourse(String mainCourse) {
return "Serving a " + mainCourse;
}
public String serveDessert(String dessert) {
return "Serving a " + dessert;
}
}

View File

@@ -0,0 +1,23 @@
package com.baeldung.sneakythrows;
import lombok.SneakyThrows;
public class SneakyRunnable implements Runnable {
@SneakyThrows
public void run() {
try {
throw new InterruptedException();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
new SneakyRunnable().run();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,25 @@
package com.baeldung.sneakythrows;
import java.io.IOException;
public class SneakyThrows {
public static <E extends Throwable> void sneakyThrow(Throwable e) throws E {
throw (E) e;
}
public static void throwsSneakyIOException() {
sneakyThrow(new IOException("sneaky"));
}
public static void main(String[] args) {
try {
throwsSneakyIOException();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

View File

@@ -0,0 +1,41 @@
package com.baeldung.inheritancecomposition.test;
import com.baeldung.inheritancecomposition.model.Actress;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
public class ActressUnitTest {
private static Actress actress;
@BeforeClass
public static void setUpActressInstance() {
actress = new Actress("Susan", "susan@domain.com", 30);
}
@Test
public void givenActressInstance_whenCalledgetName_thenEqual() {
assertThat(actress.getName()).isEqualTo("Susan");
}
@Test
public void givenActressInstance_whenCalledgetEmail_thenEqual() {
assertThat(actress.getEmail()).isEqualTo("susan@domain.com");
}
@Test
public void givenActressInstance_whenCalledgetAge_thenEqual() {
assertThat(actress.getAge()).isEqualTo(30);
}
@Test
public void givenActressInstance_whenCalledreadScript_thenEqual() {
assertThat(actress.readScript("Psycho")).isEqualTo("Reading the script of Psycho");
}
@Test
public void givenActressInstance_whenCalledperfomRole_thenEqual() {
assertThat(actress.performRole()).isEqualTo("Performing a role");
}
}

View File

@@ -0,0 +1,24 @@
package com.baeldung.inheritancecomposition.test;
import com.baeldung.inheritancecomposition.model.Computer;
import com.baeldung.inheritancecomposition.model.Memory;
import com.baeldung.inheritancecomposition.model.Processor;
import com.baeldung.inheritancecomposition.model.StandardMemory;
import com.baeldung.inheritancecomposition.model.StandardProcessor;
import com.baeldung.inheritancecomposition.model.StandardSoundCard;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class CompositionUnitTest {
@Test
public void givenComputerInstance_whenExtractedEachField_thenThreeAssertions() {
Computer computer = new Computer(new StandardProcessor("Intel I3"), new StandardMemory("Kingston", "1TB"));
computer.setSoundCard(new StandardSoundCard("Generic Sound Card"));
assertThat(computer.getProcessor()).isInstanceOf(Processor.class);
assertThat(computer.getMemory()).isInstanceOf(Memory.class);
assertThat(computer.getSoundCard()).isInstanceOf(Optional.class);
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.inheritancecomposition.test;
import com.baeldung.inheritancecomposition.model.Actress;
import com.baeldung.inheritancecomposition.model.Person;
import com.baeldung.inheritancecomposition.model.Waitress;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class InheritanceUnitTest {
@Test
public void givenWaitressInstance_whenCheckedType_thenIsInstanceOfPerson() {
assertThat(new Waitress("Mary", "mary@domain.com", 22)).isInstanceOf(Person.class);
}
@Test
public void givenActressInstance_whenCheckedType_thenIsInstanceOfPerson() {
assertThat(new Actress("Susan", "susan@domain.com", 30)).isInstanceOf(Person.class);
}
}

View File

@@ -0,0 +1,31 @@
package com.baeldung.inheritancecomposition.test;
import com.baeldung.inheritancecomposition.model.Person;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
public class PersonUnitTest {
private static Person person;
@BeforeClass
public static void setPersonInstance() {
person = new Person("John", "john@domain.com", 35);
}
@Test
public void givenPersonInstance_whenCalledgetName_thenEqual() {
assertThat(person.getName()).isEqualTo("John");
}
@Test
public void givenPersonInstance_whenCalledgetEmail_thenEqual() {
assertThat(person.getEmail()).isEqualTo("john@domain.com");
}
@Test
public void givenPersonInstance_whenCalledgetAge_thenEqual() {
assertThat(person.getAge()).isEqualTo(35);
}
}

View File

@@ -0,0 +1,46 @@
package com.baeldung.inheritancecomposition.test;
import com.baeldung.inheritancecomposition.model.Waitress;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.assertj.core.api.Assertions.*;
public class WaitressUnitTest {
private static Waitress waitress;
@BeforeClass
public static void setUpWaitressInstance() {
waitress = new Waitress("Mary", "mary@domain.com", 22);
}
@Test
public void givenWaitressInstance_whenCalledgetName_thenOneAssertion() {
assertThat(waitress.getName()).isEqualTo("Mary");
}
@Test
public void givenWaitressInstance_whenCalledgetEmail_thenOneAssertion() {
assertThat(waitress.getEmail()).isEqualTo("mary@domain.com");
}
@Test
public void givenWaitressInstance_whenCalledgetAge_thenOneAssertion() {
assertThat(waitress.getAge()).isEqualTo(22);
}
@Test
public void givenWaitressInstance_whenCalledserveStarter_thenOneAssertion() {
assertThat(waitress.serveStarter("mixed salad")).isEqualTo("Serving a mixed salad");
}
@Test
public void givenWaitressInstance_whenCalledserveMainCourse_thenOneAssertion() {
assertThat(waitress.serveMainCourse("steak")).isEqualTo("Serving a steak");
}
@Test
public void givenWaitressInstance_whenCalledserveDessert_thenOneAssertion() {
assertThat(waitress.serveDessert("cup of coffee")).isEqualTo("Serving a cup of coffee");
}
}

View File

@@ -0,0 +1,17 @@
package com.baeldung.sneakythrows;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
public class SneakyRunnableUnitTest {
@Test
public void whenCallSneakyRunnableMethod_thenThrowException() {
try {
new SneakyRunnable().run();
} catch (Exception e) {
assertEquals(InterruptedException.class, e.getStackTrace());
}
}
}

View File

@@ -0,0 +1,18 @@
package com.baeldung.sneakythrows;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
public class SneakyThrowsUnitTest {
@Test
public void whenCallSneakyMethod_thenThrowSneakyException() {
try {
SneakyThrows.throwsSneakyIOException();
} catch (Exception ex) {
assertEquals("sneaky", ex.getMessage().toString());
}
}
}