-
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.
add back in module3 solution (manually)
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 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 |
---|---|---|
@@ -1,6 +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())); | ||
} | ||
} |