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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
In the attached program, the category Shopping list is defined.
|
| 2 |
+
Write the class 'OrganicShoppingList', which inherits the class 'ShoppingList'.
|
| 3 |
+
|
| 4 |
+
The class must have the following properties:
|
| 5 |
+
a constructor with no parameters
|
| 6 |
+
An overridden method getList() that takes advantage of the superclass implementation.
|
| 7 |
+
The method returns a string of all products added to the list that contain the word 'organic'. You can assume that all products are written in all lower case. The products are separated by line breaks. See the sample output below.
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
An example of the use of the class:
|
| 11 |
+
public static void main(String[] args) {
|
| 12 |
+
OrganicShoppingList list = new OrganicShoppingList();
|
| 13 |
+
list.addProduct("carrots");
|
| 14 |
+
list.addProduct("organic bananas");
|
| 15 |
+
list.addrProduct("pineapple");
|
| 16 |
+
list.addProduct("organic toilet paper");
|
| 17 |
+
|
| 18 |
+
System.out.println(list.getList());
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
Program outputs:
|
| 22 |
+
organic bananas
|
| 23 |
+
organic toilet paper
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
import java.util.Random;
|
| 32 |
+
import java.util.ArrayList;
|
| 33 |
+
import java.util.Arrays;
|
| 34 |
+
import java.util.Collections;
|
| 35 |
+
|
| 36 |
+
public class Test {
|
| 37 |
+
public static void main(String[] args) {
|
| 38 |
+
final Random r = new Random();
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
String[] items = {"milk", "butter", "cocoa", "coffee", "beans", "peas", "carrots",
|
| 42 |
+
"pineapple", "sauce", "porridge", "cereals", "bread", "biscuits", "bun",
|
| 43 |
+
"chocolate", "buttermilk", "water"};
|
| 44 |
+
ArrayList<String> products = new ArrayList<>(Arrays.asList(items));
|
| 45 |
+
Collections.shuffle(products, r);
|
| 46 |
+
|
| 47 |
+
System.out.println("Testing the class OrganicShoppingList...");
|
| 48 |
+
|
| 49 |
+
OrganicShoppingList lk = new OrganicShoppingList();
|
| 50 |
+
System.out.println("Object created!");
|
| 51 |
+
|
| 52 |
+
// ensuring inheritance
|
| 53 |
+
ShoppingList k = (ShoppingList) lk;
|
| 54 |
+
|
| 55 |
+
int size = r.nextInt(5) + 10;
|
| 56 |
+
for (int i = 0; i < size; i++) {
|
| 57 |
+
String product = products.remove(r.nextInt(products.size()));
|
| 58 |
+
if (r.nextInt(2) == 1) {
|
| 59 |
+
product = "organic" + product;
|
| 60 |
+
}
|
| 61 |
+
System.out.println("Adding to the list " + product);
|
| 62 |
+
k.addProduct(product);
|
| 63 |
+
}
|
| 64 |
+
String product = "organic" + products.get(0);
|
| 65 |
+
System.out.println("Adding to the list " + product);
|
| 66 |
+
k.addProduct(product);
|
| 67 |
+
|
| 68 |
+
System.out.println("Products in the Organic List:");
|
| 69 |
+
String list = lk.getList();
|
| 70 |
+
if (list.endsWith("\n")) {
|
| 71 |
+
list = list.substring(0, list.length() - 1);
|
| 72 |
+
}
|
| 73 |
+
System.out.println(list);
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
class ShoppingList {
|
| 78 |
+
private ArrayList<String> products;
|
| 79 |
+
|
| 80 |
+
public ShoppingList() {
|
| 81 |
+
products = new ArrayList<>();
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
public void addProduct(String product) {
|
| 85 |
+
products.add(product);
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
public String getList() {
|
| 89 |
+
return String.join("\n", products);
|
| 90 |
+
}
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
//ADD
|
| 97 |
+
class OrganicShoppingList extends ShoppingList {
|
| 98 |
+
// initialise list
|
| 99 |
+
private ArrayList<String> organicProducts = new ArrayList<String>();
|
| 100 |
+
|
| 101 |
+
public OrganicShoppingList() {}
|
| 102 |
+
|
| 103 |
+
// round1
|
| 104 |
+
// //OVERWRITTEN METHOD
|
| 105 |
+
// public String getList() {
|
| 106 |
+
// for (String product: this.products) {
|
| 107 |
+
// if (product.contains("organic")) {
|
| 108 |
+
// organicProducts.add(product);
|
| 109 |
+
// }
|
| 110 |
+
// };
|
| 111 |
+
// return String.join("\n", organicProducts);
|
| 112 |
+
// }
|
| 113 |
+
|
| 114 |
+
// round2
|
| 115 |
+
// we use the getList() method from the parent class
|
| 116 |
+
// because we cannot access the private attribute 'products' from parent class
|
| 117 |
+
//OVERWRITTEN METHOD
|
| 118 |
+
public String getList() {
|
| 119 |
+
String allProducts = super.getList();
|
| 120 |
+
// split on linebreaks/newlines
|
| 121 |
+
String[] products = allProducts.split("\n");
|
| 122 |
+
|
| 123 |
+
for (String product: products) {
|
| 124 |
+
if (product.contains("organic")) {
|
| 125 |
+
organicProducts.add(product);
|
| 126 |
+
}
|
| 127 |
+
};
|
| 128 |
+
return String.join("\n", organicProducts);
|
| 129 |
+
}
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
Testing the class OrganicShoppingList...
|
| 138 |
+
Object created!
|
| 139 |
+
Adding to the list coffee
|
| 140 |
+
Adding to the list organiccocoa
|
| 141 |
+
Adding to the list organicporridge
|
| 142 |
+
Adding to the list organicchocolate
|
| 143 |
+
Adding to the list bread
|
| 144 |
+
Adding to the list organicbuttermilk
|
| 145 |
+
Adding to the list organicbutter
|
| 146 |
+
Adding to the list organicmilk
|
| 147 |
+
Adding to the list organiccarrots
|
| 148 |
+
Adding to the list organicpineapple
|
| 149 |
+
Adding to the list cereals
|
| 150 |
+
Adding to the list organicsauce
|
| 151 |
+
Adding to the list organicbun
|
| 152 |
+
Adding to the list organicpeas
|
| 153 |
+
Products in the Organic List:
|
| 154 |
+
organiccocoa
|
| 155 |
+
organicporridge
|
| 156 |
+
organicchocolate
|
| 157 |
+
organicbuttermilk
|
| 158 |
+
organicbutter
|
| 159 |
+
organicmilk
|
| 160 |
+
organiccarrots
|
| 161 |
+
organicpineapple
|
| 162 |
+
organicsauce
|
| 163 |
+
organicbun
|
| 164 |
+
organicpeas
|
| 165 |
+
|