diff --git a/coding.iml b/coding.iml index 4ea17ef..624a7fa 100644 --- a/coding.iml +++ b/coding.iml @@ -1,3 +1,4 @@ + diff --git a/git b/git new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/org/fundacionjala/coding/juan/bank/BankOCR.java b/src/main/java/org/fundacionjala/coding/juan/bank/BankOCR.java new file mode 100644 index 0000000..9f5c926 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/juan/bank/BankOCR.java @@ -0,0 +1,214 @@ +package org.fundacionjala.coding.juan.bank; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by Juan P on 26/03/2017. + */ +public final class BankOCR { + + private static final Map MAP_NUMBERS = new HashMap<>(); + + private static final int START_STRING = 0; + private static final int END_STRING = 3; + private static final int MODULE_NINE = 9; + private static final int MODULE_ELEVEN = 11; + private static final int SCANNED_LENGTH = 72; + private static final int QUANTITY_LENGTH = 9; + + private static final int ZERO = 0; + private static final int ONE = 1; + private static final int TWO = 2; + private static final int THREE = 3; + private static final int FOUR = 4; + private static final int FIVE = 5; + private static final int SIX = 6; + private static final int SEVEN = 7; + private static final int EIGHT = 8; + private static final int NINE = 9; + + static { + MAP_NUMBERS.put(ZERO, + " _ " + + "| |" + + "|_|"); + MAP_NUMBERS.put(ONE, + " " + + " |" + + " |"); + + MAP_NUMBERS.put(TWO, + " _ " + + " _|" + + "|_ "); + + MAP_NUMBERS.put(THREE, + "__ " + + " _|" + + "__|"); + + MAP_NUMBERS.put(FOUR, + " " + + "|_|" + + " |"); + + MAP_NUMBERS.put(FIVE, + " _" + + "|_ " + + " _|"); + + MAP_NUMBERS.put(SIX, + " _ " + + "|_ " + + "|_|"); + + MAP_NUMBERS.put(SEVEN, + " _ " + + " |" + + " |"); + MAP_NUMBERS.put(EIGHT, + " _ " + + "|_|" + + "|_|"); + + MAP_NUMBERS.put(NINE, + " _ " + + "|_|" + + " _|"); + } + + /** + * Constructor. + */ + + private BankOCR() { + + } + + /** + * @param value to get int. + * @return characters in a range 0-9 + */ + + private static String getKey(final String value) { + String valueString = "?"; + for (Map.Entry inputData : MAP_NUMBERS.entrySet()) { + valueString = inputData.getValue().equalsIgnoreCase(value) + ? inputData.getKey().toString() : valueString; + } + return valueString; + } + + /** + * @param numberScanned String + * @return true or false + */ + + public static String convertStringToNumber(final String[] numberScanned) { + StringBuilder imageToString = new StringBuilder(); + + for (String numberString : numberScanned) { + imageToString.append(getKey(numberString)); + } + return imageToString.toString(); + } + + /** + * This method validate account MAP_NUMBERS. + * + * @param account quantity. + * @return true or false. + */ + + static boolean validateAccountNumbers(final String account) { + int sum = 0; + int factorMultiple = 9; + if ((account.length() == QUANTITY_LENGTH) && isLegible(account)) { + String[] accountSplit = account.split(""); + + for (String value : accountSplit) { + sum += Integer.parseInt(value) * factorMultiple; + factorMultiple--; + } + return sum % MODULE_ELEVEN == 0; + } + return false; + } + + /** + * Method to validate if value is legible. + * + * @param account account. + * @return true or false. + */ + static boolean isLegible(final String account) { + + for (int i = 0; i < account.length(); i++) { + if (!Character.isDigit(account.charAt(i))) { + return false; + } + } + return true; + } + + /** + * This method return the value scanned. + * + * @param scanned value scanned. + * @return value string. + */ + static String accountRepresentation(final String[] scanned) { + StringBuilder acctRepresentation = new StringBuilder(); + + for (String string : scanned) { + acctRepresentation.append(getKey(string)); + } + + return String.valueOf(acctRepresentation); + } + + /** + * Get status account. + * + * @param account account. + * @return ILL if isn't legible and ERR if not validate accounts MAP_NUMBERS. + */ + static String getStatusAccount(final String account) { + return !isLegible(account) ? "ILL" : !validateAccountNumbers(account) ? "ERR" : " "; + } + + /** + * This method scans an image and returns string. + * + * @param account account. + * @return string[] with nine digits. + */ + static String[] parseScannedString(final String account) { + + String[] scannedDigits = {"", "", "", "", "", "", "", "", ""}; + int stringFinal = 3; + + if (account.length() != SCANNED_LENGTH) { + int index = 0; + int start = START_STRING; + int end = END_STRING; + int indexModule = MODULE_NINE; + + for (int i = 0; i < account.length(); i += stringFinal) { + index = index % indexModule; + + scannedDigits[index] += account.substring(start, end); + + start = end; + end += stringFinal; + index++; + } + + return scannedDigits; + } + return scannedDigits; + } +} + + diff --git a/src/main/java/org/fundacionjala/coding/juan/eanvalidator/EanValidator.java b/src/main/java/org/fundacionjala/coding/juan/eanvalidator/EanValidator.java new file mode 100644 index 0000000..59f7de9 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/juan/eanvalidator/EanValidator.java @@ -0,0 +1,29 @@ +package org.fundacionjala.coding.juan.eanvalidator; + +/** + * @author Juan Pablo + */ +public final class EanValidator { + /** + * constructor. + */ + private EanValidator() { + + } + + /** + * @param eanCode is the code provided as input. + * @return the validation. + */ + public static boolean validating(final String eanCode) { + char[] code = eanCode.toCharArray(); + int checkSum = 0; + int digit = 0; + for (int i = 0; i < code.length; i++) { + digit = code[i]; + checkSum += (i + 1) % 2 == 0 ? digit * 3 : digit; + } + digit = (code[code.length - 1]); + return checkSum % 10 == 0 || 10 - (checkSum % 10) == digit; + } +} diff --git a/src/main/java/org/fundacionjala/coding/juan/gas/Evaporator.java b/src/main/java/org/fundacionjala/coding/juan/gas/Evaporator.java new file mode 100644 index 0000000..bb5dfc2 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/juan/gas/Evaporator.java @@ -0,0 +1,34 @@ +package org.fundacionjala.coding.juan.gas; + +/** + * Created by Administrator on 6/28/2017. + */ +public final class Evaporator { + + /** + * Constructor. + */ + private Evaporator() { + } + private static final int PERCENT = 100; + /** + * Calculate the life of gas. + * + * @param content how much gas have. + * @param evap how much waste. + * @param threshold the limit. + * @return how many days. + */ + public static int evaporator(final double content, final int evap, final int threshold) { + int days = 0; + double cont = content; + double limit1 = content * threshold / PERCENT; + while (cont >= limit1) { + cont = cont - cont * evap / PERCENT; + days++; + } + return days; + + + } +} diff --git a/src/main/java/org/fundacionjala/coding/juan/highestandlowest/HightestAndLowest.java b/src/main/java/org/fundacionjala/coding/juan/highestandlowest/HightestAndLowest.java new file mode 100644 index 0000000..4739d3a --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/juan/highestandlowest/HightestAndLowest.java @@ -0,0 +1,32 @@ +package org.fundacionjala.coding.juan.highestandlowest; + +import java.util.Arrays; + +/** + * Created by Administrator on 6/28/2017. + */ +public final class HightestAndLowest { + + /** + * Constructor. + */ + private HightestAndLowest() { + } + + /** + * Kata. + * + * @param number is the string of numbers + * @return the higtest and lowest numbers + */ + public static String hingAndLow(final String number) { + String[] numbers = number.split(" "); + Integer[] num = new Integer[numbers.length]; + for (int i = 0; i < numbers.length; i++) { + num[i] = Integer.parseInt(numbers[i]); + } + Arrays.sort(num); + return String.format("%d %d", num[num.length - 1], num[0]); + + } +} diff --git a/src/main/java/org/fundacionjala/coding/juan/movies/Children.java b/src/main/java/org/fundacionjala/coding/juan/movies/Children.java new file mode 100644 index 0000000..4978c1c --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/juan/movies/Children.java @@ -0,0 +1,31 @@ +package org.fundacionjala.coding.juan.movies; + +/** + * @author Juan Pablo + */ +public class Children extends Movie { + private static final int LIMINEWCHILD = 3; + private static final double PRICE = 1.5; + + /** + * constructor. + * @param title is the title of the movie. + */ + public Children(final String title) { + super(title); + } + + @Override + public double calculateAmount(final int daysRented) { + double amount = PRICE; + if (daysRented > LIMINEWCHILD) { + amount += (daysRented - LIMINEWCHILD) * PRICE; + } + return amount; + } + + @Override + public int calculateFrequentRenterPoints(final int daysRented) { + return 1; + } +} diff --git a/src/main/java/org/fundacionjala/coding/juan/movies/Customer.java b/src/main/java/org/fundacionjala/coding/juan/movies/Customer.java new file mode 100644 index 0000000..5b13ad0 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/juan/movies/Customer.java @@ -0,0 +1,72 @@ +package org.fundacionjala.coding.juan.movies; + + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Juan Pablo + */ +public final class Customer { + private String nameCustomer; + private List rentalsCustomer; + + /** + * @param nameCustomer String with words. + * Constructor. + */ + Customer(final String nameCustomer) { + this.nameCustomer = nameCustomer; + rentalsCustomer = new ArrayList<>(); + } + + /** + * @param rental String with words. + */ + public void addRental(final Rental rental) { + rentalsCustomer.add(rental); + } + + /** + * @return the nameCustomer string. + */ + public String getName() { + return this.nameCustomer; + } + + /** + * @return the string. + */ + public String generateDetail() { + StringBuilder result = new StringBuilder(); + result.append("Rental Record for " + getName() + "\n"); + for (Rental rental : rentalsCustomer) { + result.append(rental.getMovie().getTitle() + " "); + result.append(rental.calculateAmount() + "\n"); + } + result.append("Amount owed is " + calculateTotalAmount() + "\n"); + result.append("You earned " + calculateTotalFrequentRenterPoints() + " frequent renter points"); + return result.toString(); + } + + /** + * @return the total amount. + * @ this method will calculate the total amount of the customer. + */ + public double calculateTotalAmount() { + return rentalsCustomer.stream() + .mapToDouble(Rental::calculateAmount) + .sum(); + } + + /** + * @return the total points. + * @ this method will calcule the potins of the customer. + */ + public int calculateTotalFrequentRenterPoints() { + + return rentalsCustomer.stream() + .mapToInt(rental -> rental.calculateFrequentRenterPoint()) + .sum(); + } +} diff --git a/src/main/java/org/fundacionjala/coding/juan/movies/Movie.java b/src/main/java/org/fundacionjala/coding/juan/movies/Movie.java new file mode 100644 index 0000000..c54b0ae --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/juan/movies/Movie.java @@ -0,0 +1,37 @@ +package org.fundacionjala.coding.juan.movies; + + +/** + * @author Juan Pablo + */ +public abstract class Movie { + private String title; + + /** + * constructor. + * + * @param title is the title of the movie. + */ + public Movie(final String title) { + this.title = title; + } + + /** + * @param daysRented the method uses this parameter to calculate the amount + * @return the amount + */ + public abstract double calculateAmount(int daysRented); + + /** + * @param daysRented the mothod uses the parameter to calculate the points. + * @return the points + */ + public abstract int calculateFrequentRenterPoints(int daysRented); + + /** + * @return the moview title. + */ + public String getTitle() { + return title; + } +} diff --git a/src/main/java/org/fundacionjala/coding/juan/movies/NewReleases.java b/src/main/java/org/fundacionjala/coding/juan/movies/NewReleases.java new file mode 100644 index 0000000..7e57764 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/juan/movies/NewReleases.java @@ -0,0 +1,29 @@ +package org.fundacionjala.coding.juan.movies; + + +/** + * @author Juan Pablo + */ +public class NewReleases extends Movie { + + private static final int LIMIT_NEW_CHILD = 3; + + /** + * constructor. + * {@inheritDoc} + * @param title is the title of the moview. + */ + public NewReleases(final String title) { + super(title); + } + + @Override + public double calculateAmount(final int daysRented) { + return daysRented * LIMIT_NEW_CHILD; + } + + @Override + public int calculateFrequentRenterPoints(final int daysRented) { + return daysRented > 1 ? 2 : 1; + } +} diff --git a/src/main/java/org/fundacionjala/coding/juan/movies/Regular.java b/src/main/java/org/fundacionjala/coding/juan/movies/Regular.java new file mode 100644 index 0000000..287de5b --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/juan/movies/Regular.java @@ -0,0 +1,33 @@ +package org.fundacionjala.coding.juan.movies; + + +/** + * @author Juan Pablo + */ +public class Regular extends Movie { + + private static final double PRICE = 1.5; + private static final int ONE = 1; + private static final int LIMITREGULAR = 2; + + /** + * @param title is the movie title. + */ + public Regular(final String title) { + super(title); + } + + @Override + public double calculateAmount(final int daysRented) { + double amount = PRICE; + if (daysRented > LIMITREGULAR) { + amount += (daysRented - LIMITREGULAR) * PRICE; + } + return amount; + } + + @Override + public int calculateFrequentRenterPoints(final int daysRented) { + return ONE; + } +} diff --git a/src/main/java/org/fundacionjala/coding/juan/movies/Rental.java b/src/main/java/org/fundacionjala/coding/juan/movies/Rental.java new file mode 100644 index 0000000..d5f759d --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/juan/movies/Rental.java @@ -0,0 +1,48 @@ +package org.fundacionjala.coding.juan.movies; + + +/** + * @author Juan Pablo + */ +class Rental { + private Movie movie; + private int daysRented; + + /** + * @param movie Movie. + * @param daysRented DaysRented. + */ + Rental(final Movie movie, final int daysRented) { + this.movie = movie; + this.daysRented = daysRented; + } + + /** + * @return the daysRented. + */ + public int getDaysRented() { + return daysRented; + } + + /** + * @return movie object. + */ + public Movie getMovie() { + return this.movie; + } + + /** + * @return price object. + */ + public double calculateAmount() { + return movie.calculateAmount(daysRented); + } + + /** + * @return calculateFrequentRenterPoint int. + */ + public int calculateFrequentRenterPoint() { + return movie.calculateFrequentRenterPoints(daysRented); + } +} + diff --git a/src/main/java/org/fundacionjala/coding/juan/multiplesof3and5/MultiplesOf3And5.java b/src/main/java/org/fundacionjala/coding/juan/multiplesof3and5/MultiplesOf3And5.java new file mode 100644 index 0000000..dc7fd11 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/juan/multiplesof3and5/MultiplesOf3And5.java @@ -0,0 +1,32 @@ +package org.fundacionjala.coding.juan.multiplesof3and5; + +/** + * Created by Administrator on 6/28/2017. + */ +public final class MultiplesOf3And5 { + + + /** + * Constructor. + */ + private MultiplesOf3And5() { + } + + /** + * Kata. + * + * @param num is the number. + * @return the multiples. + */ + public static int solution(final int num) { + int number = 0; + final int three = 3; + final int five = 5; + for (int i = 0; i < num; i++) { + if (i % three == 0 || i % five == 0) { + number = number + i; + } + } + return number; + } +} diff --git a/src/main/java/org/fundacionjala/coding/juan/spinWords/SpinWords.java b/src/main/java/org/fundacionjala/coding/juan/spinWords/SpinWords.java new file mode 100644 index 0000000..f080c16 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/juan/spinWords/SpinWords.java @@ -0,0 +1,36 @@ +package org.fundacionjala.coding.juan.spinWords; + +/** + * Created by Juan P on 26/03/2017. + */ +public final class SpinWords { + private static final int LIMIT_WORD = 5; + + /** + * Constructor private. + */ + + private SpinWords() { + //no called + } + + /** + * Get the words spined if it have 5 or more letters. + * + * @param sentence is the word or words + * @return the word reversed. + */ + public static String spinWord(final String sentence) { + if (sentence == null) { + return ""; + } + String[] words = sentence.split(" "); + for (int i = 0; i < words.length; i++) { + if (words[i].length() >= LIMIT_WORD) { + words[i] = new StringBuilder(words[i]).reverse().toString(); + } + } + return String.join(" ", words); + } +} + diff --git a/src/main/java/org/fundacionjala/coding/juan/test/ArrayAverage.java b/src/main/java/org/fundacionjala/coding/juan/test/ArrayAverage.java new file mode 100644 index 0000000..4a9fd14 --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/juan/test/ArrayAverage.java @@ -0,0 +1,32 @@ +package org.fundacionjala.coding.juan.test; + +/** + * Created by Juan on 3/23/2017. + */ +final class ArrayAverage { + + /** + * constructor. + */ + private ArrayAverage() { + } + + /** + * this function will calculate the averrage of a given array. + * + * @param arrayInt is the given array + * @return final array. + */ + public static double[] getArrayAverage(final int[] arrayInt) { + if (arrayInt == null || arrayInt.length == 0 || arrayInt.length == 1) { + return new double[0]; + } + double[] finalArray = new double[arrayInt.length - 1]; + for (int i = 0; i < arrayInt.length - 1; i++) { + finalArray[i] = (double) (arrayInt[i] + arrayInt[i + 1]) / 2; + + } + return finalArray; + } + +} diff --git a/src/test/java/org/fundacionjala/coding/juan/bank/BankOCRTest.java b/src/test/java/org/fundacionjala/coding/juan/bank/BankOCRTest.java new file mode 100644 index 0000000..4760c27 --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/juan/bank/BankOCRTest.java @@ -0,0 +1,175 @@ +package org.fundacionjala.coding.juan.bank; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; + +import org.junit.Test; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * Created by Juan Pablo on 26/03/2017. + */ +public class BankOCRTest { + + /** + * testing the paser when a string is given. + */ + @Test + public void testingAnGivenStringToGetTheAccount() { + + //given + final String[] numberScanned = { + " _ " + + "| |" + + "|_|", + + " " + + " |" + + " |", + + " _ " + + " _|" + + "|_ ", + + "__ " + + " _|" + + "__|", + + " " + + "|_|" + + " |", + + " _" + + "|_ " + + " _|", + + " _ " + + "|_ " + + "|_|", + + " _ " + + " |" + + " |", + + " _ " + + "|_|" + + "|_|", + + " _ " + + "|_|" + + " _|" + }; + + //when + final String actualResult = BankOCR.convertStringToNumber(numberScanned); + + //then + final String expectedResult = "0123456789"; + assertEquals(expectedResult, actualResult); + } + + /** + * Test constructor. + * + * @throws Exception exception. + */ + @Test + public void testConstructorIsPrivate() throws Exception { + Constructor constructor = BankOCR.class.getDeclaredConstructor(); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + constructor.newInstance(); + } + + /** + * Test verify if the status account is illegible. + */ + @Test + public void verifyIfTheStatusAccountIsIllegible() { + // given: + final String account = " 560 |"; + + // when: + final String expectedResult = "ILL"; + + // then: + String actualResult = BankOCR.getStatusAccount(account); + assertEquals(expectedResult, actualResult); + } + + /** + * Test verify if the status account is incorrect. + */ + @Test + public void verifyIfTheStatusAccountIsIncorrect() { + // given: + final String account = "021453789"; + + // when: + final String expectedResult = "ERR"; + + // then: + final String actualResult = BankOCR.getStatusAccount(account); + assertEquals(expectedResult, actualResult); + } + + /** + * Test verify if an image contains nine digits and is parsed correctly. + */ + @Test + public void verifyIfAnImageContainsNineDigitsAndIsParsedCorrectly() { + // given: + final String figureScanned = + " _ _ _ _ _ _ _ " + + " ||_||_||_||_| _||_||_ |_|" + + " | | _||_||_||_ |_||_| _|"; + + final String[] accountArray = BankOCR.parseScannedString(figureScanned); + + // when: + final String actualResult = BankOCR.accountRepresentation(accountArray); + + // then: + final String expectedResult = "149882869"; + assertEquals(expectedResult, actualResult); + } + + /** + * Testing an invalid account. + */ + @Test + public void verifyIfaScannedFigureIsValid() { + // given: + final String figureScanned = + " _ _ _ _ _ _ _ " + + " ||_||_||_||_| _"; + // when: + final boolean actualResult = BankOCR.validateAccountNumbers(figureScanned); + + //then: + assertFalse(actualResult); + + } + + /** + * Testing a empty string as value. + */ + @Test + public void parceIntScannedFiguredReturnEmpty() { + // given: + final String figureScanned = ""; + // when: + final String[] actualResult = BankOCR.parseScannedString(figureScanned); + + // then: + final String[] expectedResult = new String[]{"", "", "", "", "", "", "", "", ""}; + assertArrayEquals(expectedResult, actualResult); + + + } + + +} diff --git a/src/test/java/org/fundacionjala/coding/juan/eanvalidator/EanValidatorTest.java b/src/test/java/org/fundacionjala/coding/juan/eanvalidator/EanValidatorTest.java new file mode 100644 index 0000000..4737675 --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/juan/eanvalidator/EanValidatorTest.java @@ -0,0 +1,100 @@ +package org.fundacionjala.coding.juan.eanvalidator; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; + +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * Created by Juan on 3/10/2017. + */ +public class EanValidatorTest { + /** + * Testing when has 13 digits. + */ + @Test + public void testCheckLengthWhenTheEANStringNumberHasExactly13Digits() { + // given: + final String eanStringNumber = "4003301018398"; + + // when: + final boolean actualResult = EanValidator.validating(eanStringNumber); + + // then: + assertTrue(actualResult); + } + + /** + * Test when the EAN string number has less than 13 digits. + */ + + @Test + public void testValidateTheCheckSumIsDifferentFromZero() { + // given: + final String eanStringNumber = "4003301018398"; + + // when: + final boolean actualResult = EanValidator.validating(eanStringNumber); + + // then: + assertTrue(actualResult); + } + + /** + * Test when the EAN string number checksum is equal to 0. + */ + @Test + public void testValidateTheCheckSumIsEqualToZero() { + // given: + final String eanStringNumber = "4003301018392"; + + // when: + final boolean actualResult = EanValidator.validating(eanStringNumber); + + // then: + assertFalse(actualResult); + } + + /** + * test. + */ + @Test + public void testtingEanValidaor() { + // given: + final String eanStringNumber = "9783815820864"; + //when: + final boolean actualResult = EanValidator.validating(eanStringNumber); + //then: + assertFalse(actualResult); + } + + /** + * Test. + */ + @Test + public void testingEanValidator() { + // given: + final String eanStringNumber = "9783827317100"; + //when: + final boolean actualResult = EanValidator.validating(eanStringNumber); + //then + assertTrue(actualResult); + + } + + /** + * Test constructor. + * + * @throws Exception exception. + */ + @Test + public void testConstructorIsPrivate() throws Exception { + Constructor constructor = EanValidator.class.getDeclaredConstructor(); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + constructor.newInstance(); + } +} diff --git a/src/test/java/org/fundacionjala/coding/juan/gas/EvaporatorTest.java b/src/test/java/org/fundacionjala/coding/juan/gas/EvaporatorTest.java new file mode 100644 index 0000000..4c90243 --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/juan/gas/EvaporatorTest.java @@ -0,0 +1,94 @@ +package org.fundacionjala.coding.juan.gas; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Created by Administrator on 6/28/2017. + */ +public class EvaporatorTest { + + + /** + * test. + */ + @Test + public void testEvaporatorOne() { + assertEquals(22, Evaporator.evaporator(10, 10, 10)); + } + + /** + * test. + */ + @Test + public void testEvaporatorTwo() { + assertEquals(29, Evaporator.evaporator(10, 10, 5)); + } + + /** + * test. + */ + @Test + public void testEvaporatorThree() { + assertEquals(59, Evaporator.evaporator(100, 5, 5)); + } + + /** + * test. + */ + @Test + public void testEvaporatorFour() { + assertEquals(37, Evaporator.evaporator(50, 12, 1)); + } + + /** + * test. + */ + @Test + public void testEvaporatorFive() { + assertEquals(31, Evaporator.evaporator(47.5, 8, 8)); + } + + /** + * test. + */ + @Test + public void testEvaporatorSix() { + assertEquals(459, Evaporator.evaporator(100, 1, 1)); + } + + /** + * test. + */ + @Test + public void testEvaporatorSeven() { + assertEquals(459, Evaporator.evaporator(10, 1, 1)); + } + + /** + * test. + */ + @Test + public void testEvaporatorEight() { + assertEquals(299, Evaporator.evaporator(100, 1, 5)); + } + + /** + * Test constructor. + * + * @throws Exception exception. + */ + @Test + public void testConstructorIsPrivate() throws Exception { + Constructor constructor = Evaporator.class.getDeclaredConstructor(); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + constructor.newInstance(); + } + +} + diff --git a/src/test/java/org/fundacionjala/coding/juan/highestandlowest/HightestAndLowestTest.java b/src/test/java/org/fundacionjala/coding/juan/highestandlowest/HightestAndLowestTest.java new file mode 100644 index 0000000..6db94af --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/juan/highestandlowest/HightestAndLowestTest.java @@ -0,0 +1,94 @@ +package org.fundacionjala.coding.juan.highestandlowest; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Created by Administrator on 6/28/2017. + */ +public class HightestAndLowestTest { + + + /** + * Test. + */ + @Test + public void someTest() { + assertEquals(HightestAndLowest.hingAndLow("4 5 29 54 4 0 -214 542 -64 1 -3 6 -6"), "542 -214"); + } + + /** + * Test. + */ + @Test + public void plusMinusTest() { + assertEquals(HightestAndLowest.hingAndLow("1 -1"), "1 -1"); + } + + /** + * Test. + */ + @Test + public void plusPlusTest() { + assertEquals(HightestAndLowest.hingAndLow("1 1"), "1 1"); + } + + /** + * Test. + */ + @Test + public void minusMinusTest() { + assertEquals(HightestAndLowest.hingAndLow("-1 -1"), "-1 -1"); + } + + /** + * Test. + */ + @Test + public void plusMinusZeroTest() { + assertEquals(HightestAndLowest.hingAndLow("1 -1 0"), "1 -1"); + } + + /** + * Test. + */ + @Test + public void plusPlusZeroTest() { + assertEquals(HightestAndLowest.hingAndLow("1 1 0"), "1 0"); + } + + /** + * Test. + */ + @Test + public void minusMinusZeroTest() { + assertEquals(HightestAndLowest.hingAndLow("-1 -1 0"), "0 -1"); + } + + /** + * Test. + */ + @Test + public void singleTest() { + assertEquals(HightestAndLowest.hingAndLow("42"), "42 42"); + } + + /** + * Test constructor. + * + * @throws Exception exception. + */ + @Test + public void testConstructorIsPrivate() throws Exception { + Constructor constructor = HightestAndLowest.class.getDeclaredConstructor(); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + constructor.newInstance(); + } + +} + diff --git a/src/test/java/org/fundacionjala/coding/juan/movies/ChildrenTest.java b/src/test/java/org/fundacionjala/coding/juan/movies/ChildrenTest.java new file mode 100644 index 0000000..af8fa87 --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/juan/movies/ChildrenTest.java @@ -0,0 +1,87 @@ +package org.fundacionjala.coding.juan.movies; + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +/** + * Created by Juan on 3/23/2017. + */ +public class ChildrenTest { + private Movie children; + + /** + * Initialize variables. + */ + @Before + public void setUp() { + children = new Children("Spy Kids"); + } + + /** + * Test children amount when days rented is less than the limit. + */ + @Test + public void testChildrenMovieAmountWhenDaysRentetIsLessThanTheLimit() { + + // given: + final int daysRentet = 2; + // when: + final double actualResult = children.calculateAmount(daysRentet); + + // then: + final double expectedResult = 1.5; + assertEquals(expectedResult, actualResult, 0); + } + + /** + * Test children amount when days rented are equals than the limit. + */ + @Test + public void testChildrenMovieAmountWhenDaysRentetIsequalsThanTheLimit() { + + // given: + final int daysRentet = 3; + // when: + final double actualResult = children.calculateAmount(daysRentet); + + // then: + final double expectedResult = 1.5; + assertEquals(expectedResult, actualResult, 0); + } + + /** + * Test children amount when days rented are equals than the limit. + */ + @Test + public void testChildrenMovieAmountWhenDaysRentetaremoreThanTheLimit() { + + // given: + final int daysRentet = 5; + // when: + final double actualResult = children.calculateAmount(daysRentet); + + // then: + final double expectedResult = 4.5; + assertEquals(expectedResult, actualResult, 0); + } + + /** + * Test calculate points. + */ + @Test + public void testChildrenMovieCalculatePoints() { + + // given: + final int daysRentet = 5; + // when: + final double actualResult = children.calculateFrequentRenterPoints(daysRentet); + + // then: + final double expectedResult = 1; + assertEquals(expectedResult, actualResult, 0); + } + + +} + diff --git a/src/test/java/org/fundacionjala/coding/juan/movies/CustomerTest.java b/src/test/java/org/fundacionjala/coding/juan/movies/CustomerTest.java new file mode 100644 index 0000000..a800eee --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/juan/movies/CustomerTest.java @@ -0,0 +1,80 @@ + +package org.fundacionjala.coding.juan.movies; + + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +/** + * @author Juan Pablo + */ +public class CustomerTest { + + private static final double MINORPRICE = 1.5; + private static final int DAYONE = 1; + private Customer customer; + + /** + * Initialize variables. + */ + @Before + public void setUp() { + customer = new Customer("Juan"); + } + + + /** + * Test Movie When NameCustomer Is Word. + */ + @Test + public void testingGivenaCustomerName() { + + + // when: + final String actualResult = customer.getName(); + + // then: + final String expectedResult = "Juan"; + + assertEquals(expectedResult, actualResult); + } + + /** + * test Movie When Day Rented Is less For Children. + */ + @Test + public void testMovieWhenDayRentedISLessForChildren() { + + customer.addRental(new Rental(new Children("The Revenant"), DAYONE)); + + // when: + final double actualResult = customer.calculateTotalAmount(); + + // then: + + assertEquals(MINORPRICE, actualResult, 0); + } + + /** + * Test customer class. + */ + @Test + public void testCustomer() { + customer.addRental(new Rental(new NewReleases("The Avengers"), 2)); + customer.addRental(new Rental(new Regular("Iron Man"), 1)); + + // when + final String actualResult = customer.generateDetail(); + + // then + final String expectedResult = "Rental Record for Juan\n" + + "The Avengers 6.0\n" + + "Iron Man 1.5\n" + + "Amount owed is 7.5\n" + + "You earned 3 frequent renter points"; + assertEquals(expectedResult, actualResult); + } + + +} diff --git a/src/test/java/org/fundacionjala/coding/juan/movies/NewReleaseTest.java b/src/test/java/org/fundacionjala/coding/juan/movies/NewReleaseTest.java new file mode 100644 index 0000000..2be9129 --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/juan/movies/NewReleaseTest.java @@ -0,0 +1,28 @@ +package org.fundacionjala.coding.juan.movies; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Created by Juan on 3/23/2017. + */ +public class NewReleaseTest { + /** + * Test New Release amount when days rented is less than the limit. + */ + @Test + public void testNewRealeasenMovieAmount() { + + // given: + final Movie newRelease = new NewReleases("Logan"); + final int daysRentet = 2; + // when: + final double actualResult = newRelease.calculateAmount(daysRentet); + + // then: + final double expectedResult = 6; + assertEquals(expectedResult, actualResult, 0); + } + +} diff --git a/src/test/java/org/fundacionjala/coding/juan/movies/RegularTest.java b/src/test/java/org/fundacionjala/coding/juan/movies/RegularTest.java new file mode 100644 index 0000000..71ced93 --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/juan/movies/RegularTest.java @@ -0,0 +1,107 @@ +package org.fundacionjala.coding.juan.movies; + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +/** + * Created by Juan on 3/23/2017. + */ +public class RegularTest { + private Movie regular; + + /** + * Initialize variables. + */ + @Before + public void setUp() { + regular = new Regular("Logan"); + } + + /** + * Test New Release amount when days rented is less than the limit. + */ + @Test + public void testRegularMovieWhenDaysRentedAreLessThanTheLimit() { + + // given: + final int daysRentet = 1; + // when: + final double actualResult = regular.calculateAmount(daysRentet); + + // then: + final double expectedResult = 1.5; + assertEquals(expectedResult, actualResult, 0); + } + + /** + * Test New Release amount when days rented is less than the limit. + */ + @Test + public void testRegularMovieWhenDaysRentedAreigualThanTheLimit() { + + // given: + final int daysRentet = 2; + // when: + final double actualResult = regular.calculateAmount(daysRentet); + + // then: + final double expectedResult = 1.5; + assertEquals(expectedResult, actualResult, 0); + } + + /** + * Test New Release amount when days rented is less than the limit. + */ + @Test + public void testRegularMovieWhenDaysRentedAreMoreThanTheLimit() { + + // given: + final int daysRentet = 5; + // when: + final double actualResult = regular.calculateAmount(daysRentet); + + // then: + final double expectedResult = 6; + assertEquals(expectedResult, actualResult, 0); + } + + /** + * Test title. + */ + @Test + public void testRegularMovieTitle() { + + // given: + final int daysRentet = 5; + // when: + final String actualResult = regular.getTitle(); + + // then: + final String expectedResult = "Logan"; + assertEquals(expectedResult, actualResult); + } + + /** + * Test points. + */ + @Test + public void testCalculeFrequentRentedPoints() { + + // given: + final int daysRentet = 2; + // when: + final double actualResult = regular.calculateAmount(daysRentet); + + // then: + final double expectedResult = 1.5; + assertEquals(expectedResult, actualResult, 0); + } +} + + + + + + + diff --git a/src/test/java/org/fundacionjala/coding/juan/movies/RentalTest.java b/src/test/java/org/fundacionjala/coding/juan/movies/RentalTest.java new file mode 100644 index 0000000..a4bbb3f --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/juan/movies/RentalTest.java @@ -0,0 +1,55 @@ +package org.fundacionjala.coding.juan.movies; + +import org.junit.Before; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +/** + * Created by Administrator on 7/2/2017. + */ +public class RentalTest { + private Movie regular; + + /** + * Initialize variables. + */ + @Before + public void setUp() { + regular = new Regular("Logan"); + } + + /** + * Test get title. + */ + @Test + public void testGetTitle() { + // given: + final int daysRentet = 2; + final Rental rental = new Rental(regular, daysRentet); + + // when: + final String actualResult = rental.getMovie().getTitle(); + + // then: + final String expectedResult = "Logan"; + assertEquals(expectedResult, actualResult); + } + + /** + * Test days rented. + */ + + @Test + public void testDaysRented() { + // given: + final int daysRentet = 2; + final Rental rental = new Rental(regular, daysRentet); + + // when: + final int actualResult = rental.getDaysRented(); + + // then: + final int expectedResult = 2; + assertEquals(expectedResult, actualResult); + } +} diff --git a/src/test/java/org/fundacionjala/coding/juan/multiplesof3and5/MultiplesOf3And5Test.java b/src/test/java/org/fundacionjala/coding/juan/multiplesof3and5/MultiplesOf3And5Test.java new file mode 100644 index 0000000..a178139 --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/juan/multiplesof3and5/MultiplesOf3And5Test.java @@ -0,0 +1,41 @@ +package org.fundacionjala.coding.juan.multiplesof3and5; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Created by Administrator on 6/28/2017. + */ +public class MultiplesOf3And5Test { + + + /** + * KAta. + */ + @Test + public void test() { + + assertEquals(23, MultiplesOf3And5.solution(10)); + assertEquals(78, MultiplesOf3And5.solution(20)); + assertEquals(9168, MultiplesOf3And5.solution(200)); + } + + /** + * Test constructor. + * + * @throws Exception exception. + */ + @Test + public void testConstructorIsPrivate() throws Exception { + Constructor constructor = MultiplesOf3And5.class.getDeclaredConstructor(); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + constructor.newInstance(); + } + +} + diff --git a/src/test/java/org/fundacionjala/coding/juan/spinwords/SpinWordsTest.java b/src/test/java/org/fundacionjala/coding/juan/spinwords/SpinWordsTest.java new file mode 100644 index 0000000..88a5276 --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/juan/spinwords/SpinWordsTest.java @@ -0,0 +1,113 @@ +package org.fundacionjala.coding.juan.spinwords; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; + +import org.junit.Test; + + +import org.fundacionjala.coding.juan.spinWords.SpinWords; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Created by Juan on 3/10/2017. + */ + +public class SpinWordsTest { + /** + * Testing when the word has five letters. + */ + @Test + public void testspinWordsWhenTheSentenseHasFiveWords() { + + + //Given + final String sentece = "holas"; + //When + final String actualResult = SpinWords.spinWord(sentece); + // then + final String exectedResult = "saloh"; + assertEquals(exectedResult, actualResult); + + + } + + /** + * Testing when the word has more than five letters. + */ + @Test + public void testSpinWordsWhenTheSenteseHasMoreThanFiveWords() { + + //Given + final String sentece = "Paralelepipedo"; + //When + final String actualResult = SpinWords.spinWord(sentece); + // then + final String exectedResult = "odepipelelaraP"; + assertEquals(exectedResult, actualResult); + } + + /** + * Testing when the sentese has spaces. + */ + @Test + public void testSpinWordsWhenTheSenteseHasSpaces() { + + //Given + final String sentece = "Wellcome to the jungle"; + final String senteceA = "Esta es una prueba"; + //When + final String actualResult = SpinWords.spinWord(sentece); + final String actualResultA = SpinWords.spinWord(senteceA); + // then + final String exectedResult = "emoclleW to the elgnuj"; + final String exectedResultA = "Esta es una abeurp"; + assertEquals(exectedResult, actualResult); + assertEquals(exectedResultA, actualResultA); + } + + /** + * Testing empty string. + */ + @Test + public void testingAnEmptyString() { + //given + final String sentence = null; + //When + final String actualResult = SpinWords.spinWord(sentence); + //Then + final String expectedResult = ""; + assertEquals(expectedResult, actualResult); + } + + /** + * Tesing a word with less than Five letters. + */ + + @Test + public void testAwordWithLessThanFiveLetters() { + //given + final String sentence = "hi"; + //When + final String actualResult = SpinWords.spinWord(sentence); + //Then + final String expectedResult = "hi"; + assertEquals(expectedResult, actualResult); + } + + /** + * Test constructor. + * + * @throws Exception exception. + */ + @Test + public void testConstructorIsPrivate() throws Exception { + Constructor constructor = SpinWords.class.getDeclaredConstructor(); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + constructor.newInstance(); + } + +} + diff --git a/src/test/java/org/fundacionjala/coding/juan/test/ArrayAvarageTest.java b/src/test/java/org/fundacionjala/coding/juan/test/ArrayAvarageTest.java new file mode 100644 index 0000000..46a7ada --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/juan/test/ArrayAvarageTest.java @@ -0,0 +1,83 @@ +package org.fundacionjala.coding.juan.test; + +import org.junit.Test; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Modifier; +import java.util.Arrays; + + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Created by Juan on 3/23/20. + */ + +public class ArrayAvarageTest { + /** + * Basics tests. + */ + + @Test + public void basicTests() { + + //Given + final int[] array1 = new int[]{2, 2, 2, 2, 2}; + final int[] array2 = new int[]{2, -2, 2, -2, 2}; + final int[] array3 = new int[]{1, 3, 5, 1, -10}; + + //When + double[] actualArray1 = ArrayAverage.getArrayAverage(array1); + double[] actualArray2 = ArrayAverage.getArrayAverage(array2); + double[] actualArray3 = ArrayAverage.getArrayAverage(array3); + + //final + final double[] expectedArray1 = new double[]{2, 2, 2, 2}; + final double[] expectedArray2 = new double[]{0, 0, 0, 0}; + final double[] expectedArray3 = new double[]{2, 4, 3, -4.5}; + assertEquals(Arrays.toString(actualArray1), Arrays.toString(expectedArray1)); + assertEquals(Arrays.toString(actualArray2), Arrays.toString(expectedArray2)); + assertEquals(Arrays.toString(actualArray3), Arrays.toString(expectedArray3)); + } + + /** + * testing null and empty values. + */ + + @Test + public void nullEmptyTests() { + //Given + int[] array1 = null; + int[] array2 = new int[0]; + int[] array3 = new int[]{2}; + + // when + double[] actualArray1 = ArrayAverage.getArrayAverage(array1); + double[] actualArray2 = ArrayAverage.getArrayAverage(array2); + double[] actualArray3 = ArrayAverage.getArrayAverage(array3); + + //final + int zero = 0; + + assertEquals(zero, actualArray1.length); + assertEquals(zero, actualArray2.length); + assertEquals(zero, actualArray3.length); + //ArrayAverage average = new ArrayAverage(); + + } + + /** + * Test constructor. + * + * @throws Exception exception. + */ + @Test + public void testConstructorIsPrivate() throws Exception { + Constructor constructor = ArrayAverage.class.getDeclaredConstructor(); + assertTrue(Modifier.isPrivate(constructor.getModifiers())); + constructor.setAccessible(true); + constructor.newInstance(); + } +} +