In: Computer Science
Using Java
Create two exception classes to capture the improper entering of coin names and their corresponding values. Name one class CoinNameException and the other CoinValueException
Design and write a program that asks a young child to input a set of coin names and their corresponding values (in cents) in their piggybank. The program should work in a loop:
All exception classes should have at least two constructors:
Thanks for the question. Below is the code you will be needing Let me know if you have any doubts or if you need anything to change. Thank You !! =========================================================================== public class CoinValueException extends Exception{ public CoinValueException(String message) { super(message); } }
=========================================================
public class CoinNameException extends Exception { public CoinNameException(String message) { super(message); } }
=========================================================
import java.util.Scanner; public class PiggyBank { private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int[] coins = new int[5]; String name = ""; int count = 0; while (true) { for (int attempts = 1; attempts <= 2; attempts++) { try { name = getCoinName(); break; } catch (CoinNameException e) { System.out.println(e.getMessage()); if (attempts == 2) { System.out.println("Max attempts reached.\nPiggy Bank will close now."); System.exit(1); } } } for (int attempts = 1; attempts <= 2; attempts++) { try { count = getCoinValue(); break; } catch (CoinValueException e) { System.out.println(e.getMessage()); if (attempts == 2) { System.out.println("Max attempts reached.\nPiggy Bank will close now."); System.exit(1); } } } if (name.equalsIgnoreCase("penny")) coins[0]+=count; else if (name.equalsIgnoreCase("nickel")) coins[1]+=count; else if (name.equalsIgnoreCase("dime")) coins[2]+=count; else if (name.equalsIgnoreCase("quarter")) coins[3]+=count; else if (name.equalsIgnoreCase("half-dollar")) coins[4]+=count; System.out.print("Do you want to add another coin: (yes/no)? "); String again = scanner.nextLine(); if (again.equalsIgnoreCase("no")) break; } printCollections(coins); } private static void printCollections(int[] coins) { System.out.println("Piggy Bank Collection"); System.out.println("=====================\n"); System.out.println("Pennies : " + coins[0]); System.out.println("Nickel : " + coins[1]); System.out.println("Dime : " + coins[2]); System.out.println("Quarter : " + coins[3]); System.out.println("Half-Dollar : " + coins[4]); double amount = coins[0] * 0.01 + coins[1] * .05 + coins[2] * .10 + coins[3] * .25 + coins[4] * .5; System.out.println("Total Amount: $" + amount); } private static int getCoinValue() throws CoinValueException { System.out.print("Enter the number of coins: "); try { int count = scanner.nextInt(); scanner.nextLine(); if (count < 0) throw new CoinValueException("ERROR: Count is in negative."); else return count; } catch (NumberFormatException e) { throw new CoinValueException("ERROR: Input is not a number."); } } private static String getCoinName() throws CoinNameException { System.out.print("Enter coin name [penny,nickel,dime,quarter,half-dollar]: "); String name = scanner.nextLine(); if (name.equalsIgnoreCase("penny") || name.equalsIgnoreCase("nickel") || name.equalsIgnoreCase("dime") || name.equalsIgnoreCase("quarter") || name.equalsIgnoreCase("half-dollar")) { return name.toLowerCase(); } else { throw new CoinNameException("ERROR: Invalid coin name: " + name + " entered."); } } }
=========================================================