-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTest2.java
More file actions
56 lines (39 loc) · 1.64 KB
/
Test2.java
File metadata and controls
56 lines (39 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**Filename: Test2
* @version:1.0
* @author: Mwiche Dina Nachilongo 202208650
* Program to: Test custom exception for subclasses (ProductNotInStockException)
* To compile: javac Test2.java
* To execute: java Test2
*/
import java.util.Scanner;
public class Test2 {
public static void main (String[] args) throws ProductNotInStockException{
Scanner till = new Scanner (System.in);
//instantiate objects in both subclasses
Fruit f1 = new Fruit("apple", 800.0, false);
Meat m1 = new Meat("beef", 400.0, "loin");
Fruit f2= new Fruit("banana", 600.00, false);
Meat m2 = new Meat("pork", 700.00, "belly");
Fruit l1 = new Fruit("Lemons", 0.00, true);
Fruit l2 = new Fruit ("Avocados", 0.00, true);
Fruit l3 = new Fruit("Oranges", 0.00, true);
System.out.println("***Catalogue of items in stock***");
f1.displayInfo();
f2.displayInfo();
m1.displayInfo();
m2.displayInfo(); //display objects instantiated
l1.displayInfo();
l2.displayInfo();
l3.displayInfo();
System.out.println("Select name of fruit in catalogue to validate availability: "); //prompt user input
String name = till.next();
stockValidation(name);
till.close();
}
//create validation method for seasonal fruits
private static void stockValidation(String name) throws ProductNotInStockException {
if (name.equalsIgnoreCase("Avocados") && name.equalsIgnoreCase("Lemons") && name.equalsIgnoreCase("Oranges")) {
throw new ProductNotInStockException("Product not in stock, apologies.");
}
}
}