Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,28 @@

public class CustomerBOImpl implements CustomerBO {

@Override
public Amount getCustomerProductsSum(List<Product> products)
throws DifferentCurrenciesException {
BigDecimal temp = BigDecimal.ZERO;

if (products.size() == 0)
return new AmountImpl(temp, Currency.EURO);

// Throw Exception If Any of the product has a currency different from
// the first product
Currency firstProductCurrency = products.get(0).getAmount()
.getCurrency();

for (Product product : products) {
boolean currencySameAsFirstProduct = product.getAmount()
.getCurrency().equals(firstProductCurrency);
if (!currencySameAsFirstProduct) {
throw new DifferentCurrenciesException();
}
}

// Calculate Sum of Products
for (Product product : products) {
temp = temp.add(product.getAmount().getValue());
}

// Create new product
return new AmountImpl(temp, firstProductCurrency);
}
@Override
public Amount getCustomerProductsSum(List<Product> products) throws DifferentCurrenciesException {
if (products.isEmpty()) {
return new AmountImpl(BigDecimal.ZERO, Currency.EURO);
}
Currency firstProductCurrency = products.get(0).getAmount().getCurrency();
if (!doAllProductsHaveSameCurrency(products, firstProductCurrency)) {
throw new DifferentCurrenciesException();
}

return new AmountImpl(calculateSumOfProducts(products), firstProductCurrency);
}

private static BigDecimal calculateSumOfProducts(List<Product> products) {
return products.stream()
.map(product -> product.getAmount().getValue())
.reduce(BigDecimal.ZERO, BigDecimal::add);
}

private static boolean doAllProductsHaveSameCurrency(List<Product> products, Currency currency) {
return products.stream()
.map(product -> product.getAmount().getCurrency())
.allMatch(productCurrency -> productCurrency.equals(currency));
}
}
Original file line number Diff line number Diff line change
@@ -1,66 +1,71 @@
package com.b.simple.design.business.student;

public class StudentHelper {

/* PROBLEM 1 */
/*
* You get a grade B if marks are between 51 and 80 (both inclusive). Except for Maths where the upper limit is increased by 10.
*/
public boolean isGradeB(int marks, boolean isMaths) {
return isMaths ? marks>=51 && marks<=90 : marks>=51 && marks<=80;
}
public static final int GRADE_B_UPPER_LIMIT = 80;
public static final int GRADE_B_LOWER_LIMIT = 51;
public static final int EXTRA_FOR_MATH = 10;

/* PROBLEM 1 */
/*
* You get a grade B if marks are between 51 and 80 (both inclusive).
* Except for Maths where the upper limit is increased by 10.
*/
public boolean isGradeB(int marks, boolean isMaths) {
int extraLimit = isMaths ? EXTRA_FOR_MATH : 0;
int upperLimit = GRADE_B_UPPER_LIMIT + extraLimit;
return marks >= GRADE_B_LOWER_LIMIT && marks <= upperLimit;
}

/* PROBLEM 2 */
/* PROBLEM 2 */
/*
You are awarded a grade based on your marks.
Grade A = 91 to 100, Grade B = 51 to 90, Otherwise Grade C
Except for Maths where marks to get a Grade are 5 higher than required for other subjects.
*/

public String getGrade(int mark, boolean isMaths) {
String grade = "C";

if (isGradeA(mark, isMaths))
grade = "A";
else if (isBGrade(mark, isMaths)) {
grade = "B";
}
return grade;
}
public String getGrade(int mark, boolean isMaths) {
int extraLimit = isMaths ? 5 : 0;

private boolean isGradeA(int mark, boolean isMaths) {
int lowerLimitForAGrade = isMaths ? 95
: 90;
return mark > lowerLimitForAGrade;
}

private boolean isBGrade(int mark, boolean isMaths) {
int lowerLimitGradeB = isMaths ? 55
: 50;
return mark > lowerLimitGradeB && mark < 90;
}
if (mark >= 91 + extraLimit) {
return "A";
}
if (mark >= 51 + extraLimit) {
return "B";
}
return "C";
}

/* PROBLEM 3
* You and your Friend are planning to enter a Subject Quiz.
* However, there is a marks requirement that you should attain to qualify.
*
*
* Return value can be YES, NO or MAYBE.
*
*
* YES If either of you are very good at the subject(has 80 or more marks)
* However, there is an exception that if either of you is not good in the subject(20 or less marks), it is NO.
* In all other conditions, return MAYBE.
*
*
* However, the definition for good and not good are 5 marks higher if the subject is Mathematics.
*
*
* marks1 - your marks
* marks2 - your friends marks
*/
*/

public String willQualifyForQuiz(int marks1, int marks2, boolean isMaths) {
if ((isMaths ? marks1 <= 25 : marks1 <= 20)
|| (isMaths ? marks2 <= 25 : marks2 <= 20)) return "NO";
if ((isMaths ? marks1 >= 85 : marks1 >= 80)
|| (isMaths ? marks2 >= 85 : marks2 >= 80)) return "YES";
if (isNotGood(marks1, isMaths) || isNotGood(marks2, isMaths)) return "NO";
if (isGood(marks1, isMaths) || isGood(marks2, isMaths)) return "YES";
return "MAYBE";
}
}

private static boolean isGood(int marks, boolean isMaths) {
int extraLimit = isMaths ? 5 : 0;
return marks >= 80 + extraLimit;
}

private static boolean isNotGood(int marks, boolean isMaths) {
int extraLimit = isMaths ? 5 : 0;
return marks <= 20 + extraLimit;
}

}
76 changes: 70 additions & 6 deletions src/main/java/com/b/simple/design/business/text/TextHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,75 @@

public class TextHelper {

public String swapLastTwoCharacters(String str) {
return null;
}
public String swapLastTwoCharactersMine(String str) {
int len = str.length();
if (len <= 1) {
return str;
}

public String truncateAInFirst2Positions(String str) {
return null;
}
StringBuilder res = new StringBuilder();
res.append(str, 0, len - 2);
char lastCharacter = str.charAt(len - 1);
char prevLast = str.charAt(len - 2);
res.append(lastCharacter);
res.append(prevLast);

return res.toString();
}

public String swapLastTwoCharacters(String str) {
int len = str.length();
if (len <= 1) {
return str;
}
char lastCharacter = str.charAt(len - 1);
char prevLast = str.charAt(len - 2);

String restOfString = str.substring(0, len - 2);

return restOfString + lastCharacter + prevLast;
}

public String truncateAInFirst2PositionsAttempt1(String str) {
int len = str.length();
if (len == 0) {
return str;
}
StringBuilder res = new StringBuilder();
char firstCharacter = str.charAt(0);
if (firstCharacter != 'A') {
res.append(firstCharacter);
}
if (len >= 2) {
char secondCharacter = str.charAt(2);
if (secondCharacter != 'A') {
res.append(secondCharacter);
}
}
if (len >= 3) {
String restOfString = str.substring(3);
res.append(restOfString);
}

return res.toString();
}

public String truncateAInFirst2PositionsAttempt2(String str) {
if (str.length() <= 2) {
return str.replaceAll("^A+", "");
}
String firstTwo = str.substring(0, 2).replaceAll("A", "");
return firstTwo + str.substring(2);
}

public String truncateAInFirst2Positions(String str) {
if (str.length() <= 2) {
return str.replaceAll("A", "");
}
String firstTwo = str.substring(0, 2);
String firstTwoCharactersRemoved = firstTwo.replaceAll("A", "");
String restOfTheString = str.substring(2);

return firstTwoCharactersRemoved + restOfTheString;
}
}
38 changes: 23 additions & 15 deletions src/main/java/com/c/refactoring/movie/Movie.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

import com.c.refactoring.StringUtils;

import java.util.HashSet;
import java.util.Set;

public class Movie {

String rating;

private static final Set<String> VALID_B_RATING = new HashSet<String>() {{
add("B1");
add("B2");
add("B3");
add("B4");
}};
public Movie(String rating) {
super();
this.rating = rating;
Expand All @@ -19,21 +27,21 @@ public String getRating() {
Where x represents any digit between 0 and 9, and y represents
any digit between 1 and 4*/
public boolean isValidRating() {
if (this.getRating() != null) {
if (this.getRating().substring(0, 1).equalsIgnoreCase("B")
&& this.getRating().length() == 2) {
if (StringUtils.isNumeric(this.getRating().substring(1, 2))
&& Integer.parseInt(this.getRating().substring(1, 2)) > 0
&& Integer.parseInt(this.getRating().substring(1, 2)) < 5)
return true;

} else if (this.getRating().substring(0, 1).equalsIgnoreCase("A")
&& this.getRating().length() == 3
&& StringUtils.isNumeric(this.getRating().substring(1, 3)))
return true;

if (getRating() == null) {
return false;
}
return false;
return isValidBRating() || isValidARating();
}

private boolean isValidBRating() {
return VALID_B_RATING.contains(getRating());
}

private boolean isValidARating() {
String rating = this.getRating();
String firstCharacter = rating.substring(0, 1);
return firstCharacter.equalsIgnoreCase("A")
&& rating.length() == 3 && StringUtils.isNumeric(rating.substring(1, 3));
}

public void setRating(String rating) {
Expand Down
Loading