-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
address feedback comment: Consistent test file names
- Loading branch information
Showing
8 changed files
with
252 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters