Question
sample1.txt QUARTER:10 DIME:5 NICKEL:2 PENNY:4 sample2.txt penny: 6 Dime: 3 Quarter: 6 PENNY: 4 NICKEL : 5 Dime: 2 sample3.txt penny: 6 Dime: 3 Quarters
sample1.txt
QUARTER:10 DIME:5 NICKEL:2 PENNY:4
sample2.txt
penny: 6 Dime: 3 Quarter: 6 PENNY: 4 NICKEL : 5 Dime: 2
sample3.txt
penny: 6 Dime: 3 Quarters 6 : 4 NICKEL : 5 cent: 2
Coin.java
/** * This enum represents an enumeration of the US Currency Coin constants * * */ public enum Coin { PENNY(1), NICKEL(5), DIME(10), QUARTER(25);
private final int value; // value of this coins
/** * The constructor for an enum type must be package-private or private access. This private * constructor is only used to create the constants that are defined at the beginning of this * enumeration's body. * * @param value value in cents of this coin */ private Coin(int value) { this.value = value; }
/** * Returns the value in cents of this coin * * @return the value of this coin */ public int value() { return value; }
}
ExceptionalBank.java
import java.util.Arrays; import java.util.Random;
/** * This class implements an expanded version of elastic bank application * */ public class ExceptionalBank { private Coin[] coins; // array which stores all coins held in this elastic bank private int size; // size of this elastic bank private int expansionsLeft; // number of expansions left for this elastic bank private static Random rand = new Random(100); // random integers generator
/** * Creates a new elastic bank object with a given initial capacity * * @param initialCapacity initial capacity of this elastic bank */ public ExceptionalBank(int initialCapacity) { coins = new Coin[initialCapacity]; this.expansionsLeft = 2; }
/** * Creates a new elastic bank object with an initial capacity equal to 10 */ public ExceptionalBank() { this(10); }
/** * Returns the capacity of this elastic bank * * @return the capacity of this elastic bank */ public int capacity() { return coins.length; }
/** * Returns the expansions left for this elastic bank * * @return the expansions left for this elastic bank */ public int getExpansions() { return this.expansionsLeft; }
/** * Returns the number of coins held in this elastic bank * * @return the size of this elastic bank */ public int getSize() { return this.size; }
/** * Returns the value in cents of coins held in this elastic bank * * @return the balance of this elastic bank */ public int getBalance() { int balance = 0; // add the value of each coin held in this bank to balance, then return it for (int i = 0; i
/** * Returns the number of coins with a specific coinName held in this bank. The coin name * comparison is case insensitive * * @param coinName name of a coin * @return the count of coins having the provided coinName, held in this bank */ public int getSpecificCoinCount(String coinName) { int count = 0; for (int i = 0; i
/** * Returns a string representation of all the coins held in this elastic bank. Each coin is * represented by the pair "(name, value)", and the string representation should contain all of * these pairs in one space-separated line. For example: "(PENNY, 1) (QUARTER, 25) (PENNY, 1) * (DIME, 10) (NICKEL, 5)" * * @return a String representation of the contents of the bank. */ public String getCoins() { String contents = ""; // traverse the coins oversize array and add each coin's string representation to the string to // be returned for (int i = 0; i
/** * Returns a summary of this bank contents * * @return an empty string if this bank is empty, and a string representation of the summary of * this bank otherwise. The summary of the bank is a set of lines. Each line is formatted * as follows "coin_name:coin_count" */ public String getSummary() { String summary = ""; Coin[] values = Coin.values(); // traverse this bank contents and update its summary for (int i = 0; i
}
/** * Removes and returns a coin at a random position from this elastic bank * * @return the removed coin or null if this bank is empty */ public Coin removeCoin() { if(size == 0) return null; int randPosition = rand.nextInt(size); // get a random position from 0 .. size-1 Coin removedCoin = coins[randPosition]; // store the coin to be removed // The order of the coins within this bank (a piggy bank) is not important // So, move the coin at the end of the coins array to the random position // and set that last element to null. coins[randPosition] = coins[size - 1]; coins[size - 1] = null; size--; // update size return removedCoin; }
/** * Removes all the coins from this elastic bank */ public void empty() { // set all the non-null references within the coins array to null for (int i = 0; i
/** * adds a Coin to the bank and adjusts the capacity of coins if necessary and possible * * @param c coin to be added to this elastic bank */ public void addCoin(Coin c) { // check if this bank is full if (size == coins.length) { // check whether there are expansions left if (this.expansionsLeft > 0) { // expand the capacity of this elastic bank by 10 coins = Arrays.copyOf(coins, coins.length + 10); this.expansionsLeft--; } else { // no expansions left // empty this elastic bank empty(); } } // add c at the end of this bank coins[size] = c; size++; }
}
JAVA DOC INFO:
addCoins
public void addCoins(java.lang.String command) throws java.util.zip.DataFormatException
Adds a number of the same coin type with respect to a provided command line. The format of the command line is "coin_name:coins_count". Such command line refers to adding coins_count of coin_name to this bank. For instance, "PENNY:5", or " Penny : 5 " refer to adding 5 pennies to this bank. If the format of the provided command line is incorrect, no coins
Parameters:
command - command line to add a number of coins of the same type to this bank.
Throws:
java.lang.IllegalArgumentException - with the following error message "WARNING! The addCoins() method does not accept a null reference as input." if command is null.
java.util.zip.DataFormatException - with the following error message "The format of the command line " + command + " is incorrect." if the format of the provided command is incorrect. An add command line is correctly formatted if it consists of two parts separated by a colon. The first part is a String and the second must be a positive Integer. Extra spaces at the beginning of each argument/part must be ignored.
java.util.NoSuchElementException - with the following error message "The coin name provided in the command line " + command + " is invalid." if coin_name argument within the provided command line does not refer to a valid coin name with respect to the constant names defined in the enum Coin. Note that the comparison to check the validity of coin_name must be case insensitive.
loadCoins
public void loadCoins(java.io.File file) throws java.io.FileNotFoundException
Load a list of coins from a file object which refers to a data file written in a specific format (a set of lines each formatted as follows "coin_name:coin_count"). Lines formatted correctly will be added as new coins to this elastic bank. Lines formatted incorrectly must be skipped (go to the next line). This method prints/displays the following message for every skipped line "WARNING! Skipping line. " + "raison_of_the error".
Parameters:
file - file object which refers to a data file of coin_names and their counts.
Throws:
java.lang.NullPointerException - with or without error message if file is null.
java.io.FileNotFoundException - with or without error message if file is not found.
saveBankSummary
public void saveBankSummary(java.io.File file)
Save the summary of this bank to the provided file in a specific format for each line: coin_name:coin_count. For instance, if a bank contains 2 quarters, 1 dime, 5 nickels, and 10 pennies, its contents will be saved as follows: PENNY:10 NICKEL:5 DIME:1 QUARTER:2 Note that the order of lines does not matter.
Parameters:
file - File object where a summary of the contents of this bank will be saved
Throws:
java.lang.NullPointerException - with or without error message if file is null.
The following goes into ExceptionalBankTester
-
testAddCoinsValidFormat
public static boolean testAddCoinsValidFormat()
This method checks whether the ExceptionalBank.addCoins() works appropriately when it is passed a String with a valid format. You can consider the following test scenarios, for instance. First, Create a new ExceptionalBank with initial capacity 20. Then, call .addCoins("QUARTER:2"). Then, verify that 2 quarters have been added to the bank without errors. Then, call .addCoins(" Penny : 3 "); Then, verify that 3 pennies have been added to the bank without errors. You can consider further scenarios. No exceptions must be thrown by the call of .addCoins() method with valid input arguments.
Returns:
true when this test verifies a correct functionality, and false otherwise.
-
testAddCoinsInvalidDataFormat
public static boolean testAddCoinsInvalidDataFormat()
This method checks whether ExceptionalBank.addCoins() method throws a DataFormatException with an appropriate error message when it is passed an incorrectly formatted string object, for instance "quarter: five", or ": 6", or "DIME:-5"
Returns:
true when this test verifies a correct functionality, and false otherwise.
-
testAddCoinsNoSuchElement
public static boolean testAddCoinsNoSuchElement()
This method checks whether ExceptionalBank.addCoins() method throws a NoSuchElementException with an appropriate error message when it is passed a String object with a correct format (meaning "string:positive_number"), but with a coin name not defined in the enum Coin's constants. For instance, when it is passed "blabla:10".
Returns:
true when this test verifies a correct functionality, and false otherwise.
-
testAddCoinsIllegalArgument
public static boolean testAddCoinsIllegalArgument()
This method checks whether ExceptionalBank.addCoins() method throws an IllegalArgumentException with an appropriate error message when it is passed a null reference as input argument.
Returns:
true when this test verifies a correct functionality, and false otherwise.
-
testLoadCoinsNullReference
public static boolean testLoadCoinsNullReference()
This method checks whether ExceptionalBank.loadCoins() method throws a NullPointerException when it is passed a null reference.
Returns:
true when this test verifies a correct functionality, and false otherwise.
-
testLoadCoinsFileNotFound
public static boolean testLoadCoinsFileNotFound()
This method checks whether ExceptionalBank.loadCoins() method throws a FileNotFoundException when it is passed a non found file.
Returns:
true when this test verifies a correct functionality, and false otherwise.
-
testLoadCoinsFileFound
public static boolean testLoadCoinsFileFound()
This method checks whether ExceptionalBank.loadCoins() method loads appropriately without throwing any exception when it is passed a found file.
Returns:
true when this test verifies a correct functionality, and false otherwise.
-
main
public static void main(java.lang.String[] args)
Calls unit tests implemented in this class
Parameters:
args - input arguments if any
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started