Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PiggyBankTester {
- /**
- * Checks whether PiggyBank.getCoinName() method works as expected.
- * @return true when this test verifies a correct functionality, and false otherwise
- */
- public static boolean getCoinNameTestMethod() {
- // change some coin values and names
- PiggyBank.COINS_NAMES[1] = "Two cents";
- PiggyBank.COINS_NAMES[3] = "Fifty Cents";
- PiggyBank.COINS_VALUES[1] = 2;
- PiggyBank.COINS_VALUES[3] = 50;
- // consider all coin values as input arguments
- for(int i=0; i < PiggyBank.COINS_VALUES.length; i++)
- if(!PiggyBank.getCoinName(PiggyBank.COINS_VALUES[i]).equals(PiggyBank.COINS_NAMES[i]))
- return false;
- // consider input argument which does not correspond to a coin value
- if(!PiggyBank.getCoinName(7).equals(PiggyBank.NO_SUCH_COIN))
- return false;
- if(!PiggyBank.getCoinName(-10).equals(PiggyBank.NO_SUCH_COIN))
- return false;
- return true;
- }
- /**
- * Checks whether PiggyBank.getBalance() method works as expected.
- * @return true when this test verifies a correct functionality, and false otherwise
- */
- public static boolean getBalanceTestMethod() {
- // scenario 1 - empty piggy bank
- int[] coins = new int[10]; // array storing the coins held in a piggy bank
- int size = 0; // number of coins held in coins array
- if(PiggyBank.getBalance(coins, size)!= 0) {
- System.out.println("Problem detected. Your PiggyBank.getBalance() did not "
- + "return the expected output when passed an empty piggy bank.");
- return false;
- }
- // scenario 2 - non empty piggy bank
- coins = new int[] {10, 1, 5, 25, 1, 0, 0};
- size = 5;
- if(PiggyBank.getBalance(coins, size)!= 42) {
- System.out.println("Problem detected. Your PiggyBank.getBalance() did not "
- + "return the expected output when passed an piggy bank that holds "
- + "two pennies, a nickel, a dime, and a quarter.");
- return false;
- }
- // scenario 3 - another non empty piggy bank
- coins = new int[] {10, 1, 5, 25, 1, 0};
- size = 3;
- if(PiggyBank.getBalance(coins, size)!= 16) {
- System.out.println("Problem detected. Your PiggyBank.getBalance() did not "
- + "return the expected output when passed an piggy bank that holds "
- + "a penny, a nickel, and a dime, only.");
- return false;
- }
- return true;
- }
- public static boolean getSpecificCoinCountTestMethod() {
- //senario 1-empty piggy bank
- int[] coins = new int[10]; // array storing the coins held in a piggy bank
- int size = 0; // number of coins held in coins array
- int coinValue = 25;
- if( PiggyBank.getSpecificCoinCount(coinValue, coins,size)!= 0) {
- return false;
- }
- //senario 2-
- coins = new int[] {10, 1, 5, 25, 1, 1, 0};
- size = 6;
- coinValue=1;
- if(PiggyBank.getSpecificCoinCount(coinValue, coins,size)!= 3) {
- return false;
- }
- //senario 3
- coins = new int[] {10, 1, 5, 25, 1, 0, 0};
- size = 5;
- coinValue=5;
- if(PiggyBank.getSpecificCoinCount(coinValue, coins,size)!= 1) {
- return false;
- }
- return true;
- }
- public static boolean addCoinTestMethod() {
- //senario 1-
- int[] coins = new int[10]; // array storing the coins held in a piggy bank
- int size = 0; // number of coins held in coins array
- int coin = 25;
- if( PiggyBank.addCoin(coin, coins, size)!= 1) {
- return false;
- }
- //senario 2
- coins = new int[] {10, 1, 5, 25, 1, 5, 25};
- size = 7;
- coin=5;
- if(PiggyBank.addCoin(coin, coins, size)!= size) {
- return false;
- }
- //senario 3
- coins = new int[] {10, 1, 5, 25, 1, 0, 0};
- size = 5;
- coin=7;
- if(PiggyBank.addCoin(coin, coins, size)!= size) {
- return false;
- }
- return true;
- }
- public static boolean removeCoinTestMethod() {
- //senario 1- empty bank
- int[] coins = new int[10]; // array storing the coins held in a piggy bank
- int size = 0; // number of coins held in coins array
- if( PiggyBank.removeCoin(coins, size)!= size) {
- return false;
- }
- //senario 3
- coins = new int[] {10, 1, 5, 25, 1, 0, 0};
- size = 5;
- if( PiggyBank.removeCoin(coins, size)!= (size-1)) {
- return false;
- }
- return true;
- }
- public static boolean emptyPiggyBankTestMethod() {
- //senario 1- empty bank
- int[] coins = new int[10]; // array storing the coins held in a piggy bank
- int size = 0; // number of coins held in coins array
- if( PiggyBank.emptyPiggyBank( coins, size)!= 0) {
- return false;
- }
- //senario 3
- coins = new int[] {10, 1, 5, 25, 1, 0, 0};
- size = 5;
- if( PiggyBank.emptyPiggyBank( coins, size)!= 0) {
- return false;
- }
- return true;
- }
- public static boolean withdrawTestMethod() {
- //senrio 2
- int [] coins = new int[] {10, 1, 5, 25, 1, 0, 0};
- int[] result = new int [] {2,0,1,1,1};
- int size = 5;
- int amount = 16;
- if(PiggyBank.withdraw(amount, coins, size).equals(result)) {
- return false;
- }
- //senario 3
- coins = new int[] {5, 5, 5, 25, 1, 1, 25,10,10,0,0,0};
- result = new int [] {3,1,1,2,2};
- size = 9;
- amount = 40;
- int[] testResult = PiggyBank.withdraw(amount, coins, size);
- int newSize = 6;
- System.out.println("New Balance: " + PiggyBank.getBalance(coins, newSize));
- if(testResult.equals(result)) {
- return false;
- }
- //senario 3
- coins = new int[] {5,1, 1,10,0,0,0};
- result = new int [] {0,0,0,0,0};
- size = 4;
- amount = 40;
- if(PiggyBank.withdraw(amount, coins, size).equals(result)) {
- return false;
- }
- return true;
- }
- public static void main(String[] args) {
- System.out.println("getCoinNameTest(): "+ getCoinNameTestMethod());
- System.out.println("getBalanceTestMethod(): " +getBalanceTestMethod());
- System.out.println("getSpecificCoinCountTestMethod() " +getSpecificCoinCountTestMethod());
- System.out.println("addCoinTestMethod(): "+ addCoinTestMethod());
- System.out.println("removeCoinTestMethod(): "+removeCoinTestMethod());
- System.out.println("withdrawTestMethod(): "+withdrawTestMethod());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement