From 3ac6ac122a5245342c3a431e9fe791e113a835b2 Mon Sep 17 00:00:00 2001 From: hhimanshu Date: Tue, 26 May 2020 16:09:36 -0700 Subject: [PATCH] address feedback comment: Consistent test file names --- src/main/java/Finance.java | 62 ++++++++++++++++++ src/main/java/com/h2/BestLoanRates.java | 38 +++++++++++ src/main/java/com/h2/MortgageCalculator.java | 51 +++++++++++++++ src/main/java/com/h2/SavingsCalculator.java | 63 +++++++++++++++++++ src/main/java/com/h2/Utilities.java | 34 ++++++++++ .../{Module04_Test.java => Module4_Test.java} | 2 +- .../{Module05_Test.java => Module5_Test.java} | 2 +- .../{Module06_Test.java => Module6_Test.java} | 4 +- 8 files changed, 252 insertions(+), 4 deletions(-) create mode 100644 src/main/java/Finance.java create mode 100644 src/main/java/com/h2/BestLoanRates.java create mode 100644 src/main/java/com/h2/MortgageCalculator.java create mode 100644 src/main/java/com/h2/SavingsCalculator.java create mode 100644 src/main/java/com/h2/Utilities.java rename src/test/java/com/h2/{Module04_Test.java => Module4_Test.java} (99%) rename src/test/java/com/h2/{Module05_Test.java => Module5_Test.java} (99%) rename src/test/java/com/h2/{Module06_Test.java => Module6_Test.java} (99%) diff --git a/src/main/java/Finance.java b/src/main/java/Finance.java new file mode 100644 index 000000000..9cedcddc2 --- /dev/null +++ b/src/main/java/Finance.java @@ -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 commandsToUsage = Map.of( + BEST_LOAN_RATES, "usage: bestLoanRates", + SAVINGS_CALCULATOR, "usage: savingsCalculator ", + MORTGAGE_CALCULATOR, "usage: mortgageCalculator " + ); + + 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)); + } +} diff --git a/src/main/java/com/h2/BestLoanRates.java b/src/main/java/com/h2/BestLoanRates.java new file mode 100644 index 000000000..027fc9108 --- /dev/null +++ b/src/main/java/com/h2/BestLoanRates.java @@ -0,0 +1,38 @@ +package com.h2; + +import java.util.Map; +import java.util.Scanner; + +public class BestLoanRates { + public final static Map 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; + } +} \ No newline at end of file diff --git a/src/main/java/com/h2/MortgageCalculator.java b/src/main/java/com/h2/MortgageCalculator.java new file mode 100644 index 000000000..02e3e4dfd --- /dev/null +++ b/src/main/java/com/h2/MortgageCalculator.java @@ -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); + } +} diff --git a/src/main/java/com/h2/SavingsCalculator.java b/src/main/java/com/h2/SavingsCalculator.java new file mode 100644 index 000000000..c9fcb9a19 --- /dev/null +++ b/src/main/java/com/h2/SavingsCalculator.java @@ -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())); + } +} diff --git a/src/main/java/com/h2/Utilities.java b/src/main/java/com/h2/Utilities.java new file mode 100644 index 000000000..4afb97a35 --- /dev/null +++ b/src/main/java/com/h2/Utilities.java @@ -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; + } +} diff --git a/src/test/java/com/h2/Module04_Test.java b/src/test/java/com/h2/Module4_Test.java similarity index 99% rename from src/test/java/com/h2/Module04_Test.java rename to src/test/java/com/h2/Module4_Test.java index 61e0181b6..1e2e2d99d 100644 --- a/src/test/java/com/h2/Module04_Test.java +++ b/src/test/java/com/h2/Module4_Test.java @@ -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; diff --git a/src/test/java/com/h2/Module05_Test.java b/src/test/java/com/h2/Module5_Test.java similarity index 99% rename from src/test/java/com/h2/Module05_Test.java rename to src/test/java/com/h2/Module5_Test.java index 899f7aa60..1b6533fc9 100644 --- a/src/test/java/com/h2/Module05_Test.java +++ b/src/test/java/com/h2/Module5_Test.java @@ -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; diff --git a/src/test/java/com/h2/Module06_Test.java b/src/test/java/com/h2/Module6_Test.java similarity index 99% rename from src/test/java/com/h2/Module06_Test.java rename to src/test/java/com/h2/Module6_Test.java index 7dfc08883..c0bd9c7f7 100644 --- a/src/test/java/com/h2/Module06_Test.java +++ b/src/test/java/com/h2/Module6_Test.java @@ -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"; @@ -139,7 +139,7 @@ public void m6_05_testGetFloatValueExistenceAndCorrectness() throws IllegalAcces public void m6_06_testMainMethodPrintsCorrectMortgageAmount() throws NoSuchMethodException { final String methodName = "main"; - final Optional> maybeMortgageCalculator = Module04_Test.getMortgageClass(); + final Optional> maybeMortgageCalculator = Module4_Test.getMortgageClass(); assertTrue(maybeMortgageCalculator.isPresent(), classToFind + " must exist"); final Class mortgageCalculator = maybeMortgageCalculator.get();