diff --git a/src/main/java/com/h2/App.java b/src/main/java/com/h2/App.java index 011ff16b2..4440c210a 100644 --- a/src/main/java/com/h2/App.java +++ b/src/main/java/com/h2/App.java @@ -12,6 +12,28 @@ public static void main( String[] args ) } public static int doubleTheNumber(int number) { - return -1; + + return number*2; + } + + /* + Next, create a new method called `add()`. + This method should be `private`, `static`, + and should have a return type of `int`. + The method should take one parameter as its input. + The parameter name should be `numbers`, and should be of type `int[]`. + + */ + + private static int add(int[] numbers) { + /*For the implementation of `add()` method, + create a variable called `sum`, and initialize its value to `0`. + Then, using a `for` loop over `numbers`, + add every item in the `numbers` array to the `sum` variable. Finally, return `sum`.*/ + var sum = 0; + for (var n: numbers) { + sum += n; + } + return sum; } } diff --git a/src/main/java/com/h2/BestLoanRates.java b/src/main/java/com/h2/BestLoanRates.java new file mode 100644 index 000000000..80661046f --- /dev/null +++ b/src/main/java/com/h2/BestLoanRates.java @@ -0,0 +1,32 @@ +package com.h2; + +import java.util.Map; +import java.util.Scanner; + +public class BestLoanRates { + + public static final Map bestRates = Map.of(1, 5.50f, 2, 3.45f, 3, 2.67f) ; + + public static float getRates(int loanTermInYears) { + if (bestRates.containsKey(loanTermInYears)) { + return bestRates.get(loanTermInYears); + } + return 0.0f; + } + 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(); + } +} diff --git a/src/main/java/com/h2/MortgageCalculator.java b/src/main/java/com/h2/MortgageCalculator.java new file mode 100644 index 000000000..ea4829ce9 --- /dev/null +++ b/src/main/java/com/h2/MortgageCalculator.java @@ -0,0 +1,71 @@ +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 this.termInYears * 12; + } + + /* +Inside `getMonthlyInterestRate()`, create a new `float` variable +called `interestRate`, and assign to it the result of dividing +`annualRate` by `100`. Then, divide the `interestRate` by `12` +to calculate the monthly interest rate. Finally return the result of the calculation by replacing `0.0f`. + */ + private float getMonthlyInterestRate() { + float interestRate = this.annualRate/100; + return interestRate / 12; + } + + /* + P of type long, and assign it loanAmount +r of type float, and initialize it to the return value of getMonthlyInterestRate() +n of type int, and initialize the value by calling getNumberOfPayments() +*/ + public void calculateMonthlyPayment() + { + long P = this.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 String toString() { + DecimalFormat df = new DecimalFormat("####0.00"); + return "monthlyPayment: " + df.format(monthlyPayment); + } + + public static void main(String[] args) { + /* + A variable called loanAmount of type long. Initialize it by parsing the args[0] value (which + is of type String) to long using Long.parseLong(). +A variable called termInYears of type int and initialize by parsing args[1] using Integer.parseInt(). +A variable called annualRate of type float and initialize by calling Float.parseFloat(). + */ + + long loanAmount = Long.parseLong(args[0]); + int termInYears = Integer.parseInt(args[1]); + float annualRate = Float.parseFloat(args[2]); + + MortgageCalculator calculator = new MortgageCalculator(loanAmount, termInYears,annualRate); + calculator.calculateMonthlyPayment(); + + //System.out.println("Monthly Payment: " + calculator.toString()); + System.out.println(calculator.toString()); + } +} diff --git a/src/main/java/com/h2/SavingsCalculator.java b/src/main/java/com/h2/SavingsCalculator.java new file mode 100644 index 000000000..55cb6d1f7 --- /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 final float[] credits; + private final float[] debits; + + public SavingsCalculator(float[] credits, float[] debits) { + this.credits = credits; + this.debits = debits; + } + + public float calculate() { + return sumOfCredits() - sumOfDebits(); + } + + 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 d) { + YearMonth yearMonth = YearMonth.of(d.getYear(), d.getMonth()); + int totalDaysInMonth = yearMonth.lengthOfMonth(); + return totalDaysInMonth - d.getDayOfMonth(); + + } + + 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]); + } + + SavingsCalculator c = new SavingsCalculator(credits, debits); + float netSavings = c.calculate(); + System.out.println("Net Savings = " + netSavings + ", remaining days in month = " + remainingDaysInMonth(LocalDate.now())); + } +} diff --git a/src/test/java/Finance.java b/src/test/java/Finance.java new file mode 100644 index 000000000..f33069c86 --- /dev/null +++ b/src/test/java/Finance.java @@ -0,0 +1,66 @@ +import com.h2.BestLoanRates; +import com.h2.MortgageCalculator; +import com.h2.SavingsCalculator; + +import java.util.Arrays; +import java.util.Map; + +public class Finance { + public final static String BEST_LOAN_RATES = "bestLoanRates"; + public final static String SAVINGS_CALCULATOR = "savingsCalculator"; + public final static String MORTGAGE_CALCULATOR = "mortgageCalculator"; + + public final static 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); + return; + } + + } + + public static void main(String[] args) { + String command = args[0]; + if (!commandsToUsage.containsKey(command)) { + System.out.println(command + ": command not found"); + return; + } + + boolean isValidCommand = validateCommandArguments(args); + if (!isValidCommand) { + System.out.println(commandsToUsage.get(args[0])); + return; + } + + executeCommand(command, Arrays.copyOfRange(args, 1, args.length)); + + } + +}