Skip to content

Commit

Permalink
arthurshur (#13737)
Browse files Browse the repository at this point in the history
* 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 d9464aa.

* renamed package
  • Loading branch information
ginjardev committed Apr 6, 2023
1 parent 054c995 commit eb04de7
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}

}

0 comments on commit eb04de7

Please sign in to comment.