Skip to content

Commit

Permalink
address feedback comment: Consistent test file names
Browse files Browse the repository at this point in the history
  • Loading branch information
hhimanshu committed May 26, 2020
1 parent 9287a2a commit 3ac6ac1
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 4 deletions.
62 changes: 62 additions & 0 deletions src/main/java/Finance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import com.h2.BestLoanRates;
import com.h2.MortgageCalculator;
import com.h2.SavingsCalculator;

import java.util.Arrays;
import java.util.Map;

public class Finance {
public static final String BEST_LOAN_RATES = "bestLoanRates";
public static final String SAVINGS_CALCULATOR = "savingsCalculator";
public static final String MORTGAGE_CALCULATOR = "mortgageCalculator";

public static final Map<String, String> commandsToUsage = Map.of(
BEST_LOAN_RATES, "usage: bestLoanRates",
SAVINGS_CALCULATOR, "usage: savingsCalculator <credits separated by ','> <debits separated by ','>",
MORTGAGE_CALCULATOR, "usage: mortgageCalculator <loanAmount> <termInYears> <annualRate>"
);

private static boolean validateCommandArguments(String[] args) {
switch (args[0]) {
case BEST_LOAN_RATES:
return args.length == 1;
case SAVINGS_CALCULATOR:
return args.length == 3;
case MORTGAGE_CALCULATOR:
return args.length == 4;
}
return false;
}

private static void executeCommand(String command, String[] arguments) {
switch (command) {
case BEST_LOAN_RATES:
System.out.println("Finding best loan rates ...");
BestLoanRates.main(arguments);
return;
case SAVINGS_CALCULATOR:
System.out.println("Finding your net savings ...");
SavingsCalculator.main(arguments);
return;
case MORTGAGE_CALCULATOR:
System.out.println("Finding your monthly payment ...");
MortgageCalculator.main(arguments);
}
}

public static void main(String[] args) {
final String command = args[0];
if (!commandsToUsage.containsKey(command)) {
System.out.println(command + ": command not found");
return;
}

final boolean isValidCommand = validateCommandArguments(args);
if (!isValidCommand) {
System.out.println(commandsToUsage.get(args[0]));
return;
}

executeCommand(command, Arrays.copyOfRange(args, 1, args.length));
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/h2/BestLoanRates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.h2;

import java.util.Map;
import java.util.Scanner;

public class BestLoanRates {
public final static Map<Integer, Float> bestRates = Map.of(
1, 5.50f,
2, 3.45f,
3, 2.67f
);

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("Enter your name");
String name = scanner.nextLine();
System.out.println("Hello " + name);

System.out.println("Enter the loan term (in years)");
int loanTermInYears = scanner.nextInt();
float bestRate = getRates(loanTermInYears);
if (bestRate == 0.0f) {
System.out.println("No available rates for term: " + loanTermInYears + " years");
} else {
System.out.println("Best Available Rate: " + getRates(loanTermInYears) + "%");
}

scanner.close();
}

public static float getRates(int loanTermInYears) {
if (bestRates.containsKey(loanTermInYears)) {
return bestRates.get(loanTermInYears);
}
return 0.0f;
}
}
51 changes: 51 additions & 0 deletions src/main/java/com/h2/MortgageCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.h2;

import java.text.DecimalFormat;

public class MortgageCalculator {
private long loanAmount;
private int termInYears;
private float annualRate;
private double monthlyPayment;

public MortgageCalculator(long loanAmount, int termInYears, float annualRate) {
this.loanAmount = loanAmount;
this.termInYears = termInYears;
this.annualRate = annualRate;
}

private int getNumberOfPayments() {
return termInYears * 12;
}

private float getMonthlyInterestRate() {
float interestRate = annualRate / 100;
return interestRate / 12;
}

public void calculateMonthlyPayment() {
long P = loanAmount;
float r = getMonthlyInterestRate();
int n = getNumberOfPayments();

double M = P * (((r * Math.pow(1 + r, n))) / ((Math.pow((1 + r), n)) - 1));

this.monthlyPayment = M;
}

public static void main(String[] args) {
final long loanAmount = Utilities.getLongValue(args[0]);
final int termInYears = Utilities.getIntValue(args[1]);
final float annualRate = Utilities.getFloatValue(args[2]);
final MortgageCalculator c = new MortgageCalculator(loanAmount, termInYears, annualRate);

c.calculateMonthlyPayment();

System.out.println(c.toString());
}

public String toString() {
DecimalFormat df = new DecimalFormat("####0.00");
return "monthlyPayment: " + df.format(monthlyPayment);
}
}
63 changes: 63 additions & 0 deletions src/main/java/com/h2/SavingsCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.h2;

import java.time.LocalDate;
import java.time.YearMonth;

public class SavingsCalculator {
private float[] credits;
private float[] debits;

public SavingsCalculator(float[] credits, float[] debits) {
this.credits = credits;
this.debits = debits;
}

private float sumOfCredits() {
float sum = 0.0f;
for (float credit : credits) {
sum += credit;
}
return sum;
}

private float sumOfDebits() {
float sum = 0.0f;
for (float debit : debits) {
sum += debit;
}
return sum;
}

private static int remainingDaysInMonth(LocalDate date) {
YearMonth yearMonth = YearMonth.of(date.getYear(), date.getMonth());
int totalDaysInMonth = yearMonth.lengthOfMonth();
int remainingDays = totalDaysInMonth - date.getDayOfMonth();
return remainingDays;
}

public float calculate() {
return sumOfCredits() - sumOfDebits();
}

public static void main(String[] args) {
final String[] creditsAsString = args[0].split(",");
final String[] debitsAsString = args[1].split(",");

final float[] credits = new float[creditsAsString.length];
final float[] debits = new float[debitsAsString.length];

for (int i = 0; i < creditsAsString.length; i++) {
credits[i] = Float.parseFloat(creditsAsString[i]);
}

for (int i = 0; i < debitsAsString.length; i++) {
debits[i] = Float.parseFloat(debitsAsString[i]);
}

final SavingsCalculator calculator = new SavingsCalculator(credits, debits);

float netSavings = calculator.calculate();

System.out.println("Net Savings = " + netSavings + ", remaining days in month = " + remainingDaysInMonth(LocalDate.now()));
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/h2/Utilities.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.h2;

public class Utilities {

public static long getLongValue(String in) {
long out = Long.MIN_VALUE;
try {
out = Long.parseLong(in);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(in + " cannot be converted into a 'long' value. Exiting program.");
}
return out;
}

public static int getIntValue(String in) {
int out = Integer.MIN_VALUE;
try {
out = Integer.parseInt(in);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(in + " cannot be converted into a 'int' value. Exiting program.");
}
return out;
}

public static float getFloatValue(String in) {
float out = Float.MIN_VALUE;
try {
out = Float.parseFloat(in);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(in + " cannot be converted into a 'float' value. Exiting program.");
}
return out;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.platform.commons.util.ReflectionUtils.*;

public class Module04_Test {
public class Module4_Test {
private static final String classToFind = "com.h2.MortgageCalculator";
private final InputStream systemIn = System.in;
private final PrintStream systemOut = System.out;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.platform.commons.util.ReflectionUtils.*;

public class Module05_Test {
public class Module5_Test {
private final String classToFind = "Finance";

private final InputStream systemIn = System.in;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.platform.commons.util.ReflectionUtils.*;

public class Module06_Test {
public class Module6_Test {
private final String classToFind = "com.h2.Utilities";


Expand Down Expand Up @@ -139,7 +139,7 @@ public void m6_05_testGetFloatValueExistenceAndCorrectness() throws IllegalAcces
public void m6_06_testMainMethodPrintsCorrectMortgageAmount() throws NoSuchMethodException {
final String methodName = "main";

final Optional<Class<?>> maybeMortgageCalculator = Module04_Test.getMortgageClass();
final Optional<Class<?>> maybeMortgageCalculator = Module4_Test.getMortgageClass();
assertTrue(maybeMortgageCalculator.isPresent(), classToFind + " must exist");
final Class<?> mortgageCalculator = maybeMortgageCalculator.get();

Expand Down

0 comments on commit 3ac6ac1

Please sign in to comment.