Skip to content

all katas #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions src/main/java/org/fundacionjala/coding/Library.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.fundacionjala.coding.danielcabero.averageofnumbers;

import java.util.stream.IntStream;

/**
* Created by administrator on 6/20/2017.
*/

public final class AverageOfNumbers {

/**
* Private constructor.
*/
private AverageOfNumbers() {
}

/**
* The method returns the average of the elements in the array as a double array.
*
* @param numbers is an array of int numbers.
* @return the average of the numbers as an array.
*/
public static double[] numberAverages(final int[] numbers) {
return (numbers == null || numbers.length <= 1)
? new double[]{}
: IntStream.range(0, numbers.length - 1)
.mapToDouble(i -> (numbers[i] + numbers[i + 1]) / 2.0)
.toArray();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package org.fundacionjala.coding.danielcabero.bank_ocr;

import java.util.HashMap;
import java.util.Map;

/**
* Created by administrator on 3/10/2017.
*/

final class BankOcr {

private static final int MODULUS_FACTOR = 11;
private static final int ACCOUNT_LENGTH = 9;
private static final int SCANNED_ACCOUNT_LENGTH = 72;
private static final int MULTIPLY_FACTOR = 9;
private static final int INDEX_MODULUS_FACTOR = 9;
private static final int NUMBER_START = 0;
private static final int NUMBER_END = 3;
private static final Map<Integer, String> MAPS_OF_NUMBERS = new HashMap<>();

static {
MAPS_OF_NUMBERS.put(NUMBERS.ZERO.ordinal(),
" _ "
+ "| |"
+ "|_|");
MAPS_OF_NUMBERS.put(NUMBERS.ONE.ordinal(),
" "
+ " |"
+ " |");
MAPS_OF_NUMBERS.put(NUMBERS.TWO.ordinal(),
" _ "
+ " _|"
+ "|_ ");
MAPS_OF_NUMBERS.put(NUMBERS.THREE.ordinal(),
"__ "
+ " _|"
+ "__|");
MAPS_OF_NUMBERS.put(NUMBERS.FOUR.ordinal(),
" "
+ "|_|"
+ " |");
MAPS_OF_NUMBERS.put(NUMBERS.FIVE.ordinal(),
" _ "
+ "|_ "
+ " _|");
MAPS_OF_NUMBERS.put(NUMBERS.SIX.ordinal(),
" _ "
+ "|_ "
+ "|_|");
MAPS_OF_NUMBERS.put(NUMBERS.SEVEN.ordinal(),
"__ "
+ " |"
+ " |");
MAPS_OF_NUMBERS.put(NUMBERS.EIGHT.ordinal(),
" _ "
+ "|_|"
+ "|_|");
MAPS_OF_NUMBERS.put(NUMBERS.NINE.ordinal(),
" _ "
+ "|_|"
+ " _|");
}

/**
* Constructor private.
*/
private BankOcr() {

}

/**
* @param value used to obtain its int representation
* @return "?" if the value is not in range 0-9 otherwise
* return a number between 0 and 9.
*/
private static String getKey(final String value) {
String key = "?";
for (Map.Entry<Integer, String> entry : MAPS_OF_NUMBERS.entrySet()) {
if (entry.getValue().equals(value)) {
key = entry.getKey().toString();
}
}
return key;
}

/**
* This method determines if an scanned numberAccount is between the paraments corrects.
*
* @param numberAccount image of type String.
* @return <code>true</code> \\ <code>false</code>
*/
private static boolean isCorrectLength(final String numberAccount) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can still refactor this

for (int i = 0; i < numberAccount.length(); i++) {
if (Character.isDigit(numberAccount.charAt(i))) {
return false;
}
}
return true;
}

/**
* This method validate if an account is correct or not.
*
* @param account String variable is the account to validate
* @return true if the passed account is true, false otherwise.
*/
static boolean validateNumberAccount(final String account) {
int checksum = 0;
int multi = MULTIPLY_FACTOR;
for (char num : account.toString().toCharArray()) {
checksum += Integer.parseInt(String.valueOf(num)) * multi;
multi--;
}
return checksum % MODULUS_FACTOR == 0;
}

/**
* This method is in charge to give one of three posibilities errors to the numberAccountUser".
* "", "ERR" or "ILL"
*
* @param numberAccountUser String variable to determine its status.
* @return an String result with the values mentioned earlier.
*/
static String getNumberAccount(final String numberAccountUser) {
String error = "";

if (!isCorrectLength(numberAccountUser)) {
error = "ILL";
} else if (!validateNumberAccount(numberAccountUser)) {
error = "ERR";
}
return error;
}

/**
* This method returns the string representation of the scanned image.
*
* @param scannedImage array string.
* @return String representation of scanned image.
*/
static String numberAccountPresentImage(final String[] scannedImage) {
StringBuilder actPresent = new StringBuilder();

for (String number : scannedImage) {
actPresent.append(getKey(number));
}
return actPresent.toString();
}

/**
* This method validates if an scanned account has the correct digit
* numbers.
*
* @param scannedAccount of String type.
* @return true.
*/
private static boolean sizeImageIsValid(final String scannedAccount) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this method?

return scannedAccount.length() == SCANNED_ACCOUNT_LENGTH;
}

/**
* This enum type will contain the key values.
*/
private enum NUMBERS {
ZERO(0), ONE(1), TWO(2), THREE(3),
FOUR(4), FIVE(5), SIX(6),
SEVEN(7), EIGHT(8), NINE(9);

/**
* Construct for Enum type.
*
* @param i of int type.
*/
NUMBERS(final int i) {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.fundacionjala.coding.danielcabero.eanvalidator;

/**
* Created administrat on 3/10/2017.
*/

final class EANValidator {
private static final int ODD_DIGIT_MULTIPLIER = 3;
private static final int DIVISIBILITY_FACTOR = 10;
private static final int EAN_CORRECTLY_LENGTH = 13;

/**
* Constructor private.
*/

private EANValidator() {
}

/**
* Takes an string number to verify it is checksum it's correct.
*
* @param eAN String number with exactly 13 digits.
* @return true if the checksum is ok.
*/

public static boolean validate(final String eAN) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactor this code
avoid the else condition

int sum = 0;

if (!checkCorrectLength(eAN)) {
return false;
} else {
for (int i = 1; i < eAN.length(); i++) {
int numericValue = Character.getNumericValue(eAN.charAt(i - 1));
sum += i % 2 == 0 ? numericValue * ODD_DIGIT_MULTIPLIER : numericValue;
}

int module = sum % DIVISIBILITY_FACTOR;
int check = module != 0 ? DIVISIBILITY_FACTOR - module : 0;

return check == Character.getNumericValue(eAN.charAt(eAN.length() - 1));
}
}

/**
* Takes an String number digits and returns true if the String length
* is exactly 13.
*
* @param stringNumber number
* @return true if stringNumber length is exactly 13.
*/
public static boolean checkCorrectLength(final String stringNumber) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this method?

return stringNumber.length() == EAN_CORRECTLY_LENGTH;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.fundacionjala.coding.danielcabero.evaporator;

/**
* Created by administrator on 06/26/2017.
*/
final class Evaporator {

private static final int PORCENT = 100;

/**
* Constructor.
*/
private Evaporator() {
}

/**
* @param content of the gas in the tank.
* @param percentDay of loss in the day.
* @param threshold of gas loss that not use.
* @return days that save the gas.
*/
static int evaporator(final double content, final double percentDay, final double threshold) {

int days = 0;
double contentPercentage = PORCENT;

while (contentPercentage > threshold) {
contentPercentage *= 1 - percentDay / PORCENT;
days++;
}

return days;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.fundacionjala.coding.danielcabero.highestandlowest;

import java.util.Arrays;
import java.util.stream.Stream;

/**
* Created by Administrator on 6/19/2017.
*/

final class HighestAndLowest {

/**
* Private constructor.
*/
private HighestAndLowest() {
}

/**
* The method return the highest and lowest value of a number array.
*
* @param numbers is the String of numbers.
* @return a String with the highest and lowest values.
*/
static String highAndLowest(final String numbers) {
int[] digits = Stream.of(numbers.split(" ")).mapToInt(Integer::parseInt).toArray();

int high = Arrays.stream(digits).max().getAsInt();

int lowest = Arrays.stream(digits).min().getAsInt();

return String.format("%d %d", high, lowest);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.fundacionjala.coding.danielcabero.movies;

/**
* Created by administrator on 3/10/2017.
*/
public class Children extends Movie {
private static final int CHILDREN_DAYS = 3;
private static final double PRICE = 1.5;

/**
* Children constructor.
*
* @param title of String type.
*/
Children(final String title) {
super(title);
}

@Override
public double calculateAmount(final int daysRented) {
double amount = PRICE;
if (daysRented > CHILDREN_DAYS) {
amount += (daysRented - CHILDREN_DAYS) * PRICE;
}
return amount;
}

@Override
public int frequentPointsRenter(final int daysRented) {
return 1;
}
}
Loading