diff --git a/src/main/java/Finance.java b/src/main/java/Finance.java deleted file mode 100644 index 9cedcddc2..000000000 --- a/src/main/java/Finance.java +++ /dev/null @@ -1,62 +0,0 @@ -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 deleted file mode 100644 index 027fc9108..000000000 --- a/src/main/java/com/h2/BestLoanRates.java +++ /dev/null @@ -1,38 +0,0 @@ -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 deleted file mode 100644 index 02e3e4dfd..000000000 --- a/src/main/java/com/h2/MortgageCalculator.java +++ /dev/null @@ -1,51 +0,0 @@ -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 deleted file mode 100644 index c9fcb9a19..000000000 --- a/src/main/java/com/h2/SavingsCalculator.java +++ /dev/null @@ -1,63 +0,0 @@ -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 deleted file mode 100644 index 4afb97a35..000000000 --- a/src/main/java/com/h2/Utilities.java +++ /dev/null @@ -1,34 +0,0 @@ -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; - } -}