Created
February 21, 2017 04:46
MATH 2B: Uncle Bill
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
/** | |
* Mini Project: | |
* Two different methods to calculate the amount that Nancy is to receive from her Uncle Bill. | |
*/ | |
public class Bill { | |
public static void main(String[] args) { | |
List<Double> firstList = new ArrayList<>(); | |
List<Double> secondList = new ArrayList<>(); | |
int max = 15; | |
FirstCalculator c1 = new FirstCalculator(); | |
SecondCalculator c2 = new SecondCalculator(); | |
stackAmount(max, c1, firstList); | |
stackAmount(max, c2, secondList); | |
System.out.println(firstList); | |
System.out.println(secondList); | |
boolean[] comparedResult = new boolean[max - 1]; | |
for (int i = 0; i < firstList.size(); i++) { | |
comparedResult[i] = firstList.get(i) > secondList.get(i); | |
} | |
System.out.println(Arrays.toString(comparedResult)); | |
} | |
private static void stackAmount(int maxWeek, Calculator calculator, List<Double> list) { | |
for (int i = 1; i < maxWeek; i++) { | |
list.add(calculator.getAmmount(i)); | |
} | |
} | |
private interface Calculator { | |
double getAmmount(int week); | |
} | |
private static abstract class CommonCalculator { | |
double roundUp(double amount) { | |
return Double.parseDouble(new DecimalFormat("##.00").format(amount)); | |
} | |
} | |
private static class FirstCalculator extends CommonCalculator implements Calculator { | |
@Override | |
public double getAmmount(int week) { | |
switch (week) { | |
case 1: | |
return 0; | |
case 2: | |
return 0.01; | |
default: | |
double a = (5 * getAmmount(week - 1)); | |
double b = (6 * getAmmount(week - 2)); | |
double amount = a - b; | |
return roundUp(amount); | |
} | |
} | |
} | |
private static class SecondCalculator extends CommonCalculator implements Calculator { | |
@Override | |
public double getAmmount(int week) { | |
switch (week) { | |
case 1: | |
return 0; | |
case 2: | |
return 1; | |
default: | |
double a = getAmmount(week - 1); | |
double b = getAmmount(week - 2); | |
return roundUp(a + b); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output when
max = 15