Skip to content

Instantly share code, notes, and snippets.

@ChristianAlexander
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChristianAlexander/31db1e462a9cbcb75109 to your computer and use it in GitHub Desktop.
Save ChristianAlexander/31db1e462a9cbcb75109 to your computer and use it in GitHub Desktop.
Tester for money module of Task 6, CSCD 349, Fall '14, EWU
package task6.money;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Christian Alexander on 11/10/14.
*/
public class Test {
public static void main(String[] args)
{
CurrencyManager manager = new CurrencyManager();
changeTest(manager);
sumCurrencyTest(manager);
testArith();
}
private static void changeTest(CurrencyManager manager)
{
System.out.println("\n------Time to test making change!------");
testChangeFor(manager, new Money(3, 73));
testChangeFor(manager, new Money(1, 50));
testChangeFor(manager, new Money(99));
testChangeFor(manager, new Money(4));
try {
testChangeFor(manager, new Money(-10));
System.out.println("Somehow made negative change");
} catch (RuntimeException e) {
System.out.println(e);
}
}
private static void sumCurrencyTest(CurrencyManager manager)
{
System.out.println("\n------Time to test currency summation!------");
List<A_Currency> currencies0 = new ArrayList<A_Currency>();
System.out.printf("Expecting %s\n", new Money());
System.out.println(testSumCurrency(manager, currencies0).isZero()?"Money.isZero() true? Pass":"Money.isZero() true? False");
List<A_Currency> currencies1 = new ArrayList<A_Currency>();
currencies1.add(new CurrencyPaperDollar_10());
System.out.printf("Expecting %s\n", new Money(10, 0));
testSumCurrency(manager, currencies1);
List<A_Currency> currencies2 = new ArrayList<A_Currency>();
currencies2.add(new CurrencyPaperDollar_1());
currencies2.add(new CurrencyPaperDollar_10());//11
currencies2.add(new CurrencyPaperDollar_5());//16
currencies2.add(new CurrencyPaperDollar_2());//18
currencies2.add(new CurrencyPaperDollar_1());//19
System.out.printf("Expecting %s\n", new Money(19, 0));
testSumCurrency(manager, currencies2);
List<A_Currency> currencies3 = new ArrayList<A_Currency>();
currencies3.add(new CurrencyPaperDollar_1());
currencies3.add(new CurrencyPaperDollar_10());//11
currencies3.add(new CurrencyPaperDollar_5());//16
currencies3.add(new CurrencyPaperDollar_2());//18
currencies3.add(new CurrencyPaperDollar_1());//19
currencies3.add(new CurrencyCoinCent_5());//19.05
currencies3.add(new CurrencyCoinCent_25());//19.30
currencies3.add(new CurrencyCoinCent_10());//19.40
System.out.printf("Expecting %s\n", new Money(19, 40));
testSumCurrency(manager, currencies3);
List<A_Currency> currencies4 = new ArrayList<A_Currency>();
currencies4.add(new CurrencyCoinCent_25());//.25
currencies4.add(new CurrencyCoinDollar_1());//1.25
currencies4.add(new CurrencyCoinCent_5());//1.30
System.out.printf("Expecting %s\n", new Money(1, 30));
testSumCurrency(manager, currencies4);
}
private static void testArith()
{
System.out.println("\n------Test Arith------");
testArithFor(new Money(), new Money());
testArithFor(new Money(), new Money(3, 76));
testArithFor(new Money(1, 25), new Money(1, 25, false));
testArithFor(new Money(1, 54, false), new Money(1, 54, false));
testArithFor(new CurrencyCoinCent_25(), new Money(2, 49));
testArithFor(new CurrencyPaperDollar_10(), new CurrencyCoinCent_5());
testArithFor(new CurrencyCoinDollar_1(), new CurrencyPaperDollar_1());
testArithFor(new Money(20, 43, false), new CurrencyPaperDollar_10());
}
private static void testChangeFor(CurrencyManager manager, Money money)
{
System.out.printf("Make change for %s\n", money);
for(A_Currency c : manager.makeChange(money))
{
System.out.println(c);
}
System.out.printf("Current Profit: %s\n\n", manager.getExchangeProfit());
}
private static Money testSumCurrency(CurrencyManager manager, java.util.List<A_Currency> list)
{
Money result = manager.sumCurrency(list);
System.out.printf("%s\n\n", result);
return result;
}
private static void testArithFor(Money m1, Money m2)
{
System.out.printf("\nCase: %s, %s\n", m1, m2);
System.out.printf("%s + %s = %s\n", m1, m2, m1.add(m2));
System.out.printf("%s - %s = %s\n", m1, m2, m1.subtract(m2));
System.out.printf("%s - %s = %s\n", m2, m1, m2.subtract(m1));
if(m1.equals(m2))
System.out.printf("%s = %s\n", m1, m2);
else
System.out.printf("%s != %s\n", m1, m2);
}
private static void testArithFor(A_Currency m1, Money m2)
{
System.out.printf("\nCase: %s, %s\n", m1, m2);
System.out.printf("%s + %s = %s\n", m1, m2, m1.add(m2));
System.out.printf("%s - %s = %s\n", m1, m2, m1.subtract(m2));
System.out.printf("%s - %s = %s\n", m2, m1, m2.subtract(m1.getValue()));
if(m1.getValue().equals(m2))
System.out.printf("%s = %s\n", m1, m2);
else
System.out.printf("%s != %s\n", m1, m2);
}
private static void testArithFor(Money m1, A_Currency m2)
{
System.out.printf("\nCase: %s, %s\n", m1, m2);
System.out.printf("%s + %s = %s\n", m1, m2, m1.add(m2.getValue()));
System.out.printf("%s - %s = %s\n", m1, m2, m1.subtract(m2.getValue()));
System.out.printf("%s - %s = %s\n", m2, m1, m2.subtract(m1));
if(m1.equals(m2.getValue()))
System.out.printf("%s = %s\n", m1, m2);
else
System.out.printf("%s != %s\n", m1, m2);
}
private static void testArithFor(A_Currency m1, A_Currency m2)
{
System.out.printf("\nCase: %s, %s\n", m1, m2);
System.out.printf("%s + %s = %s\n", m1, m2, m1.add(m2));
System.out.printf("%s - %s = %s\n", m1, m2, m1.subtract(m2));
System.out.printf("%s - %s = %s\n", m2, m1, m2.subtract(m1));
if(m1.getValue().equals(m2.getValue()))
System.out.printf("%s = %s\n", m1, m2);
else
System.out.printf("%s != %s\n", m1, m2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment