Question: Given a Customer class (Customer.java Download Customer.java), modify the class to throw an IllegalArgumentException under the following three conditions: The object is constructed with a
Given a Customer class (Customer.java Download Customer.java), modify the class to throw an IllegalArgumentException under the following three conditions:
The object is constructed with a negative customer ID (exception message: "Customer ID cannot be negative")
The object is constructed with a blank name (exception message: "Customer name cannot be blank")
The object is constructed with a negative customer balance (exception message: "Customer balance cannot be negative")
An attempt is made to adjust the customer balance that would result in the balance being negative (exception message: "Cannot adjust balance to negative amount")
Example: try { Customer george = new Customer(-1, "George Glass", 30); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); }
// The above prints out "Customer ID cannot be negative"
try { Customer george = new Customer(2, "", 30); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); }
// The above prints out "Customer name cannot be blank"
try { Customer george = new Customer(2, "George Glass", -30); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); }
// The above prints out "Customer balance cannot be negative"
Customer robert = new Customer(5, "Robert Navarro", 30); try { robert.adjustBalance(-31); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); }
// The above prints out "Cannot adjust balance to negative amount" Customer.java:
/** * A customer in a customer database */ public class Customer { // ADD YOUR INSTANCE VARIABLES HERE /** * Constructs a customer with the given id, name, and with a zero balance * * @param id customer id * @param name customer name */ public Customer(int id, String name) { // FILL IN } /** * Constructs a customer with the given id, name, and balance. * * Note: For info on why you don't need to include a throws clause in the method * declaration, it's because it throws an un-checked (runtime) exception. Only * include a throws clause if it throws a checked exception. See * https://stackoverflow.com/questions/4392446/when-to-use-throws-in-a-java- method-declaration * * @param id customer id * @param name customer name * @param balance customer balance */ public Customer(int id, String name, double balance) { // FILL IN } public int getID() { return -1; // FIX ME } public String getName() { return ""; // FIX ME } /** * Gets the current customer balance * * @return the current balance */ public double getBalance() { return -1.0; // FIX ME }
/** * Adjusts customer balance by the given amount * * @param amount the amount by which to adjust the balance */ public void adjustBalance(double amount) { // FILL IN } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
