First, create the custom exception class called OutOfStockException by creating a new class that extends the Exception class The class has a single constructor that takes a single argument, a string called message. The message argument is passed to the parent class (Exception) via the super keyword. Create the Store class and import the java.util.HashMap and java.util.Map classes The Store class is defined with a private Map called products, which is used to store the product names and their corresponding quantities. Create a default constructor that initializes the products map with three items: "apple", "banana", and "orange", with respective quantities of 10, 5, and 0. Define thepurchase method , which takes two arguments: a product name and a quantity to be purchased. This method throws an OutOfStockException if either the specified product is not available in the store or if the requested quantity is greater than the quantity available in stock. The method first checks if the product exists in the store's products map by using the containsKey method of the Map class. If the product is not in the store, the method throws an instance of the OutOfStockException class with a message indicating that the ("Product " +product+" not available in store") The second check in the purchase method uses the get method of the Map class to retrieve the quantity of the specified product in the store. If the requested quantity is greater than the quantity in stock, the method throws an OutOfStockException with a message indicating that the ("Product " +product+" out of stock") The template code provided to you defines a Main class that contains a main method. This method creates an instance of the Store class and calls the purchase method on it to try to purchase 11 units of the "apple" product.The purchase method call is enclosed in a try-catch block. This is because the purchase method is declared to throw an OutOfStockException, which means it can throw an exception in certain situations(e.g. when the product is out of stock or not available in the store). --------------------------------------------------------------------------------------------------------------------------------------------------------- public class Main { //Do not modify this code     public static void main(String[] args) {         Store store = new Store();         try {             store.purchase("apple",11);             System.out.println("Purchase successful!");         } catch (OutOfStockException e) {             System.out.println(e.getMessage());         }     } } --------------------------------------------------------------------------------------------------------------------------------------------------------- public class OutOfStockException extends Exception {     // Custom exception class for when a product is out of stock     public OutOfStockException() {         // Call the superclass constructor to pass the error message to the parent class             } } --------------------------------------------------------------------------------------------------------------------------------------------------------- import java.util.HashMap; import java.util.Map; public class Store {     //create the map          public Store() {         // Initialize the products map with default values              }     public void purchase(String product, int quantity) throws OutOfStockException {         // Check if the product is available in the store         if (){             // If not, throw an OutOfStockException with a message indicating the product is not available                      }         // Check if there is enough stock for the desired quantity         if (){             // If not, throw an OutOfStockException with a message indicating the product is out of stock                      }     } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%
  1. First, create the custom exception class called OutOfStockException by creating a new class that extends the Exception class

  2. The class has a single constructor that takes a single argument, a string called message.

  3. The message argument is passed to the parent class (Exception) via the super keyword.

  4. Create the Store class and import the java.util.HashMap and java.util.Map classes

  5. The Store class is defined with a private Map called products, which is used to store the product names and their corresponding quantities.

  6. Create a default constructor that initializes the products map with three items: "apple", "banana", and "orange", with respective quantities of 10, 5, and 0.

  7. Define thepurchase method , which takes two arguments: a product name and a quantity to be purchased. This method throws an OutOfStockException if either the specified product is not available in the store or if the requested quantity is greater than the quantity available in stock.

  8. The method first checks if the product exists in the store's products map by using the containsKey method of the Map class.

  9. If the product is not in the store, the method throws an instance of the OutOfStockException class with a message indicating that the ("Product " +product+" not available in store")

  10. The second check in the purchase method uses the get method of the Map class to retrieve the quantity of the specified product in the store. If the requested quantity is greater than the quantity in stock, the method throws an OutOfStockException with a message indicating that the ("Product " +product+" out of stock")

  11. The template code provided to you defines a Main class that contains a main method. This method creates an instance of the Store class and calls the purchase method on it to try to purchase 11 units of the "apple" product.The purchase method call is enclosed in a try-catch block. This is because the purchase method is declared to throw an OutOfStockException, which means it can throw an exception in certain situations(e.g. when the product is out of stock or not available in the store).

---------------------------------------------------------------------------------------------------------------------------------------------------------


public class Main {
//Do not modify this code
    public static void main(String[] args) {
        Store store = new Store();
        try {
            store.purchase("apple",11);
            System.out.println("Purchase successful!");
        } catch (OutOfStockException e) {
            System.out.println(e.getMessage());
        }
    }


}
---------------------------------------------------------------------------------------------------------------------------------------------------------
public class OutOfStockException extends Exception {
    // Custom exception class for when a product is out of stock
    public OutOfStockException() {
        // Call the superclass constructor to pass the error message to the parent class
       
    }
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.HashMap;
import java.util.Map;

public class Store {
    //create the map
    
    public Store() {
        // Initialize the products map with default values
        
    }

    public void purchase(String product, int quantity) throws OutOfStockException {
        // Check if the product is available in the store
        if (){
            // If not, throw an OutOfStockException with a message indicating the product is not available
            
        }
        // Check if there is enough stock for the desired quantity
        if (){
            // If not, throw an OutOfStockException with a message indicating the product is out of stock
            
        }
    }
}




Expert Solution
steps

Step by step

Solved in 4 steps with 5 images

Blurred answer
Knowledge Booster
Custom Exception class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education