arthurshur (#13737)
* Creating a deep vs shallow copy of an object in Java * Creating a deep vs shallow copy of an object in Java * Baeldung article converting number bases * Baeldung article converting number bases * edits made to Converting a Number from One Base to Another in Java * added braces to oneliners * added precondition to check input * String[] vs String... * helper vs Utility classes * helper vs utility classes code * helper vs utility classes refactor * utility vs helper class * Helper vs utility * helper vs utility classes * refactor package name * Revert "refactor package name" This reverts commit d9464aa2de272b75ff7efe1c5ffb4de7fd4506e0. * renamed package
This commit is contained in:
parent
9f179d687e
commit
f21cf9951b
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.helpervsutilityclasses;
|
||||
|
||||
class MyHelperClass {
|
||||
public double discount;
|
||||
public MyHelperClass(double discount) {
|
||||
if (discount > 0 && discount < 1) {
|
||||
this.discount = discount;
|
||||
}
|
||||
}
|
||||
public double discountedPrice(double price) {
|
||||
return price - (price * discount);
|
||||
}
|
||||
|
||||
public static int getMaxNumber(int[] numbers) {
|
||||
if (numbers.length == 0) {
|
||||
throw new IllegalArgumentException("Ensure array is not empty");
|
||||
}
|
||||
int max = numbers[0];
|
||||
for (int i = 1; i < numbers.length; i++) {
|
||||
if (numbers[i] > max) {
|
||||
max = numbers[i];
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public static int getMinNumber(int[] numbers) {
|
||||
if (numbers.length == 0) {
|
||||
throw new IllegalArgumentException("Ensure array is not empty");
|
||||
}
|
||||
int min = numbers[0];
|
||||
for (int i = 1; i < numbers.length; i++) {
|
||||
if (numbers[i] < min) {
|
||||
min = numbers[i];
|
||||
}
|
||||
}
|
||||
return min;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.helpervsutilityclasses;
|
||||
|
||||
public final class MyUtilityClass {
|
||||
|
||||
private MyUtilityClass(){}
|
||||
|
||||
public static String returnUpperCase(String stringInput) {
|
||||
return stringInput.toUpperCase();
|
||||
}
|
||||
|
||||
public static String returnLowerCase(String stringInput) {
|
||||
return stringInput.toLowerCase();
|
||||
}
|
||||
|
||||
public static String[] splitStringInput(String stringInput, String delimiter) {
|
||||
return stringInput.split(delimiter);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.helpervsutilityclasses;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MyHelperClassUnitTest {
|
||||
|
||||
@Test
|
||||
void whenCreatingHelperObject_thenHelperObjectShouldBeCreated() {
|
||||
MyHelperClass myHelperClassObject = new MyHelperClass(0.10);
|
||||
int[] numberArray = {15, 23, 66, 3, 51, 79};
|
||||
|
||||
assertNotNull(myHelperClassObject);
|
||||
|
||||
assertEquals(90, myHelperClassObject.discountedPrice(100.00));
|
||||
assertEquals( 79, MyHelperClass.getMaxNumber(numberArray));
|
||||
assertEquals(3, MyHelperClass.getMinNumber(numberArray));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.helpervsutilityclasses;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MyUtilityClassUnitTest {
|
||||
|
||||
@Test
|
||||
void whenUsingUtilityMethods_thenAccessMethodsViaClassName(){
|
||||
assertEquals( "INIUBONG", MyUtilityClass.returnUpperCase("iniubong"));
|
||||
assertEquals("accra", MyUtilityClass.returnLowerCase("AcCrA"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue