the groceries List array and calculate the total price for all of the items purchased by a customer. The object added to the array represents the item purchased by a customer. (2 marks) (vi) Define the print method. The method is used to display the list and the total price of items purchased by a customer (if any) in the following format (please refer to the sample output given in Figure 3): (5 marks) Number of items purchased: 2 Item 1 {Information for Item 1} Item 2 {Information for Item 2} TOTAL PRICE: MYR ##.## (b) Write an interface named Convertible and declare two final static variables. (c) Write an abstract superclass named Groceries that implements the Convertible interface. The class provides the default constructor that does nothing and the constructor with arguments that initialize all the member attributes to the values passed as arguments. It also provides the getDescription method, which returns the value of description member variable, as well as the getPrice method, which returns the value of price member variable. The calcPriceInMYR method will return the value of the converted price in MYR. (5 Marks) (1 Mark) (d) Write a subclass Fruit with the following codes: (7 Marks) (1) Define the constructor with arguments that will initialize all of the class's member attributes, including the superclass's attributes. (1.5 marks) (ii) Define the calcWeightInGram method, which will return the value of the converted weight in gram. (1 mark) (iii) Define the displayInfo method, which will display the grocery description, weight in pound and gram, and price in USD and MYR in the following format (please refer to the sample output given in Figure 3): (3.5 marks) Grocery description: ???? ???? Weight in pound: #.## Weight in gram: ###.## Price: USD #.## Price converted to Malaysian: MYR ##.## 3
(e) Write a subclass Canned Food with the following codes: (7 Marks) (i) Define the constructor with arguments that will initialize all of the class's member attributes, including the superclass's attributes. (2 marks) (ii) Define the displayInfo method, which will display the grocery description, weight in pound and gram, and price in USD and MYR in the following format (please refer to the sample output given in Figure 3): (3.5 marks) Grocery description: ???? ???? Canned Food Type: ???? ???? Expiry date: dd/mm/yyyy Price: USD #.## Price converted to Malaysian: MYR ##.## (f) Write a driver class named GroceriesApp. The class provides the following methods: (26.5 Marks) (1) Define the dispMenu method that will provide the user a menu-driven interaction as follows (please refer to the sample output given in Figure 3): =========== Menu =========== [1] Add Customer [2] Buy Fruit [3] Buy Canned Food [4] Display Grocery Shopping List [5] Exit Select task: The definition for the dispMenu method is fully given in Figure 2. public static void dispMenu() { System.out.println("========== Menu =========="); 2 3 System.out.println("[1] Add Customer"); 4 System.out.println(" [2] Buy Fruit"); 5 System.out.println("[3] Buy Canned Food"); 6 System.out.println("[4] Display Grocery Shopping List"); 7 System.out.println("[5] Exit"); 8 System.out.println("===== 9 System.out.print("\nSelect task: "); 10 Figure 2: Menu-driven interaction method. ="); (ii) Define the main method that will produce the output like the sample output given in Figure 3 according to the following descriptions: (26 marks) Identify the suitable variables and/ or instances where appropriate. 4
Create a Scanner object for input purpose. Ask the user to enter the chosen task. Handling the user input error using an exception approach. Note that, all the interactions shown in Figure 3 are continuous in a single run. Note also that, the bold texts indicate input entered by the user. [1] Add Customer - Ask the user to enter the customer's name. Create a new Customer object. [2] Buy Fruit [5.5 marks] If there is no customer, display an appropriate message, i.e. "There is no customer. Please start by entering the customer's name.". Ask the user to enter the fruit's description, price in USD and weight in pounds. Create a new Fruit object. Note: You should use the polymorphism concept. Using an appropriate method defined above, insert the new Fruit object into the groceries List array for the customer. [3] Buy Canned Food [6 marks] If there is no customer, display an appropriate message, i.e. "There is no customer. Please start by entering the customer's name." Ask the user to enter the fruit's description, price in USD and weight in pounds. Create a new Canned Food object. Note: You should use the polymorphism concept. Using an appropriate method defined above, insert the new Canned Food object into the groceries List array for the customer. [4] Display Grocery Shopping List [6 marks] - If there is no customer, display an appropriate message, i.e. "There is no customer. - 55 [1.5 marks] Please start by entering the customer's name If there is no item purchased, display an appropriate message, i.e. "No items purchased!!". Display the information of all the items purchased by the customer (please refer to the sample output given in Figure 3). [5] Exit End the program by displaying an appropriate message, i.e. "Thank (g) The program is able to run, work, and display the output as required. were [0.5 mark] you!

=========== Menu =========== [1] Add Customer [2] Buy Fruit [3] Buy Canned Food [4] Display Grocery Shopping List [5] Exit ======= Select task: 2 There is no customer. Please start by entering the customer's name. =========== Menu =========== [1] Add Customer [2] Buy Fruit [3] Buy Canned Food [4] Display Grocery Shopping List [5] Exit Select task: 3 There is no customer. Please start by entering the customer's name. ========== Menu ========== [1] Add Customer [2] Buy Fruit. [3] Buy Canned Food [4] Display Grocery Shopping List [5] Exit ======= ======== Select task: 4 There is no customer. Please start by entering the customer's name. ========== Menu =========== [1] Add Customer [2] Buy Fruit [3] Buy Canned Food [4] Display Grocery Shopping List [5] Exit ==== Select task: 1 <<< Add Customer >>> Name: Hanif Iskandar ========== Menu =========== [1] Add Customer [2] Buy Fruit [3] Buy Canned Food [4] Display Grocery Shopping List [5] Exit Select task: 4 No items were purchased!! 6
========== Menu ========== [1] Add Customer [2] Buy Fruit [3] Buy Canned Food [4] Display Grocery Shopping List [5] Exit ======== ============== Select task: 2 <<< Buy Fruit >>> Description: Mango Price (in USD): 4 Weight (in pound): 2 ========== Menu ========== [1] Add Customer [2] Buy Fruit [3] Buy Canned Food. [4] Display Grocery Shopping List [5] Exit Select task: 3 <<< Buy Canned Food >>> Description: Straw Mushrooms Price (in USD): 6 Type: Vegetable Expiry Date: 15/10/2023 ========== Menu ========== [1] Add Customer [2] Buy Fruit [3] Buy Canned Food. [4] Display Grocery Shopping List [5] Exit Select task: 2 <<< Buy Fruit >>> Description: Watermelon Price (in USD): 3.5 Weight (in pound): 1.5 ========== Menu ========== [1] Add Customer [2] Buy Fruit [3] Buy Canned Food [4] Display Grocery Shopping List [5] Exit == ======================= Select task: 4 <<< Hanif Iskandar's grocery shopping list >>> Number of items purchased: 3 7
Item 1 Grocery description: Mango Weight in pound: 2.0 Weight in gram: 907.18 Price: USD 4.00 Price converted to Malaysian: MYR 17.72 Item 2 Grocery description: Straw Mushrooms Canned Food Type: Vegetable Expiry date: 15/10/2023 Price: USD 6.00 Price converted to Malaysian: MYR 26.58 Item 3 Grocery description: Watermelon Weight in pound: 1.5 Weight in gram: 680.385 Price: USD 3.50 Price converted to Malaysian: MYR 15.50 TOTAL PRICE: MYR 59.80 ===== ===== Menu ========== [1] Add Customer [2] Buy Fruit [3] Buy Canned Food [4] Display Grocery Shopping List [5] Exit Select task: 0 ======== java.lang.Exception: Invalid Input!! Select task: 6 (a) Example output of the first run =========== Menu =========== [1] Add Customer [2] Buy Fruit [3] Buy Canned Food [4] Display Grocery Shopping List [5] Exit ===== =========== java.lang. Exception: Invalid Input!! (b) Example output of the second run
========== Menu ========== [1] Add Customer [2] Buy Fruit [3] Buy Canned Food. [4] Display Grocery Shopping List [5] Exit ===== Select task: 5 Thank you!
