Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. public class PiggyBankTester {
  2.  
  3. /**
  4. * Checks whether PiggyBank.getCoinName() method works as expected.
  5. * @return true when this test verifies a correct functionality, and false otherwise
  6. */
  7. public static boolean getCoinNameTestMethod() {
  8. // change some coin values and names
  9. PiggyBank.COINS_NAMES[1] = "Two cents";
  10. PiggyBank.COINS_NAMES[3] = "Fifty Cents";
  11. PiggyBank.COINS_VALUES[1] = 2;
  12. PiggyBank.COINS_VALUES[3] = 50;
  13. // consider all coin values as input arguments
  14. for(int i=0; i < PiggyBank.COINS_VALUES.length; i++)
  15. if(!PiggyBank.getCoinName(PiggyBank.COINS_VALUES[i]).equals(PiggyBank.COINS_NAMES[i]))
  16. return false;
  17. // consider input argument which does not correspond to a coin value
  18. if(!PiggyBank.getCoinName(7).equals(PiggyBank.NO_SUCH_COIN))
  19. return false;
  20. if(!PiggyBank.getCoinName(-10).equals(PiggyBank.NO_SUCH_COIN))
  21. return false;
  22. return true;
  23. }
  24.  
  25.  
  26. /**
  27. * Checks whether PiggyBank.getBalance() method works as expected.
  28. * @return true when this test verifies a correct functionality, and false otherwise
  29. */
  30. public static boolean getBalanceTestMethod() {
  31. // scenario 1 - empty piggy bank
  32. int[] coins = new int[10]; // array storing the coins held in a piggy bank
  33. int size = 0; // number of coins held in coins array
  34. if(PiggyBank.getBalance(coins, size)!= 0) {
  35. System.out.println("Problem detected. Your PiggyBank.getBalance() did not "
  36. + "return the expected output when passed an empty piggy bank.");
  37. return false;
  38. }
  39. // scenario 2 - non empty piggy bank
  40. coins = new int[] {10, 1, 5, 25, 1, 0, 0};
  41. size = 5;
  42. if(PiggyBank.getBalance(coins, size)!= 42) {
  43. System.out.println("Problem detected. Your PiggyBank.getBalance() did not "
  44. + "return the expected output when passed an piggy bank that holds "
  45. + "two pennies, a nickel, a dime, and a quarter.");
  46. return false;
  47. }
  48. // scenario 3 - another non empty piggy bank
  49. coins = new int[] {10, 1, 5, 25, 1, 0};
  50. size = 3;
  51. if(PiggyBank.getBalance(coins, size)!= 16) {
  52. System.out.println("Problem detected. Your PiggyBank.getBalance() did not "
  53. + "return the expected output when passed an piggy bank that holds "
  54. + "a penny, a nickel, and a dime, only.");
  55. return false;
  56. }
  57. return true;
  58. }
  59.  
  60. public static boolean getSpecificCoinCountTestMethod() {
  61. //senario 1-empty piggy bank
  62. int[] coins = new int[10]; // array storing the coins held in a piggy bank
  63. int size = 0; // number of coins held in coins array
  64. int coinValue = 25;
  65. if( PiggyBank.getSpecificCoinCount(coinValue, coins,size)!= 0) {
  66. return false;
  67. }
  68. //senario 2-
  69. coins = new int[] {10, 1, 5, 25, 1, 1, 0};
  70. size = 6;
  71. coinValue=1;
  72. if(PiggyBank.getSpecificCoinCount(coinValue, coins,size)!= 3) {
  73. return false;
  74. }
  75. //senario 3
  76. coins = new int[] {10, 1, 5, 25, 1, 0, 0};
  77. size = 5;
  78. coinValue=5;
  79. if(PiggyBank.getSpecificCoinCount(coinValue, coins,size)!= 1) {
  80. return false;
  81. }
  82. return true;
  83. }
  84. public static boolean addCoinTestMethod() {
  85. //senario 1-
  86. int[] coins = new int[10]; // array storing the coins held in a piggy bank
  87. int size = 0; // number of coins held in coins array
  88. int coin = 25;
  89. if( PiggyBank.addCoin(coin, coins, size)!= 1) {
  90. return false;
  91. }
  92.  
  93. //senario 2
  94. coins = new int[] {10, 1, 5, 25, 1, 5, 25};
  95. size = 7;
  96. coin=5;
  97. if(PiggyBank.addCoin(coin, coins, size)!= size) {
  98. return false;
  99. }
  100. //senario 3
  101. coins = new int[] {10, 1, 5, 25, 1, 0, 0};
  102. size = 5;
  103. coin=7;
  104. if(PiggyBank.addCoin(coin, coins, size)!= size) {
  105. return false;
  106. }
  107. return true;
  108. }
  109.  
  110. public static boolean removeCoinTestMethod() {
  111. //senario 1- empty bank
  112. int[] coins = new int[10]; // array storing the coins held in a piggy bank
  113. int size = 0; // number of coins held in coins array
  114. if( PiggyBank.removeCoin(coins, size)!= size) {
  115. return false;
  116. }
  117. //senario 3
  118. coins = new int[] {10, 1, 5, 25, 1, 0, 0};
  119. size = 5;
  120. if( PiggyBank.removeCoin(coins, size)!= (size-1)) {
  121. return false;
  122. }
  123. return true;
  124. }
  125.  
  126. public static boolean emptyPiggyBankTestMethod() {
  127. //senario 1- empty bank
  128. int[] coins = new int[10]; // array storing the coins held in a piggy bank
  129. int size = 0; // number of coins held in coins array
  130. if( PiggyBank.emptyPiggyBank( coins, size)!= 0) {
  131. return false;
  132. }
  133. //senario 3
  134. coins = new int[] {10, 1, 5, 25, 1, 0, 0};
  135. size = 5;
  136. if( PiggyBank.emptyPiggyBank( coins, size)!= 0) {
  137. return false;
  138. }
  139. return true;
  140. }
  141.  
  142.  
  143. public static boolean withdrawTestMethod() {
  144. //senrio 2
  145. int [] coins = new int[] {10, 1, 5, 25, 1, 0, 0};
  146. int[] result = new int [] {2,0,1,1,1};
  147. int size = 5;
  148. int amount = 16;
  149. if(PiggyBank.withdraw(amount, coins, size).equals(result)) {
  150. return false;
  151. }
  152. //senario 3
  153. coins = new int[] {5, 5, 5, 25, 1, 1, 25,10,10,0,0,0};
  154. result = new int [] {3,1,1,2,2};
  155. size = 9;
  156. amount = 40;
  157. int[] testResult = PiggyBank.withdraw(amount, coins, size);
  158. int newSize = 6;
  159. System.out.println("New Balance: " + PiggyBank.getBalance(coins, newSize));
  160. if(testResult.equals(result)) {
  161. return false;
  162. }
  163. //senario 3
  164. coins = new int[] {5,1, 1,10,0,0,0};
  165. result = new int [] {0,0,0,0,0};
  166. size = 4;
  167. amount = 40;
  168. if(PiggyBank.withdraw(amount, coins, size).equals(result)) {
  169. return false;
  170. }
  171. return true;
  172. }
  173.  
  174.  
  175. public static void main(String[] args) {
  176. System.out.println("getCoinNameTest(): "+ getCoinNameTestMethod());
  177. System.out.println("getBalanceTestMethod(): " +getBalanceTestMethod());
  178. System.out.println("getSpecificCoinCountTestMethod() " +getSpecificCoinCountTestMethod());
  179. System.out.println("addCoinTestMethod(): "+ addCoinTestMethod());
  180. System.out.println("removeCoinTestMethod(): "+removeCoinTestMethod());
  181. System.out.println("withdrawTestMethod(): "+withdrawTestMethod());
  182. }
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement