TurkuBasicOOPinJava / Week 5: Class hierarchies /09B. Organic version of a shopping list
KaiquanMah's picture
String var1 = super.method1();
5f7cf40 verified
In the attached program, the category Shopping list is defined.
Write the class 'OrganicShoppingList', which inherits the class 'ShoppingList'.
The class must have the following properties:
a constructor with no parameters
An overridden method getList() that takes advantage of the superclass implementation.
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.
An example of the use of the class:
public static void main(String[] args) {
OrganicShoppingList list = new OrganicShoppingList();
list.addProduct("carrots");
list.addProduct("organic bananas");
list.addrProduct("pineapple");
list.addProduct("organic toilet paper");
System.out.println(list.getList());
}
Program outputs:
organic bananas
organic toilet paper
import java.util.Random;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Test {
public static void main(String[] args) {
final Random r = new Random();
String[] items = {"milk", "butter", "cocoa", "coffee", "beans", "peas", "carrots",
"pineapple", "sauce", "porridge", "cereals", "bread", "biscuits", "bun",
"chocolate", "buttermilk", "water"};
ArrayList<String> products = new ArrayList<>(Arrays.asList(items));
Collections.shuffle(products, r);
System.out.println("Testing the class OrganicShoppingList...");
OrganicShoppingList lk = new OrganicShoppingList();
System.out.println("Object created!");
// ensuring inheritance
ShoppingList k = (ShoppingList) lk;
int size = r.nextInt(5) + 10;
for (int i = 0; i < size; i++) {
String product = products.remove(r.nextInt(products.size()));
if (r.nextInt(2) == 1) {
product = "organic" + product;
}
System.out.println("Adding to the list " + product);
k.addProduct(product);
}
String product = "organic" + products.get(0);
System.out.println("Adding to the list " + product);
k.addProduct(product);
System.out.println("Products in the Organic List:");
String list = lk.getList();
if (list.endsWith("\n")) {
list = list.substring(0, list.length() - 1);
}
System.out.println(list);
}
}
class ShoppingList {
private ArrayList<String> products;
public ShoppingList() {
products = new ArrayList<>();
}
public void addProduct(String product) {
products.add(product);
}
public String getList() {
return String.join("\n", products);
}
}
//ADD
class OrganicShoppingList extends ShoppingList {
// initialise list
private ArrayList<String> organicProducts = new ArrayList<String>();
public OrganicShoppingList() {}
// round1
// //OVERWRITTEN METHOD
// public String getList() {
// for (String product: this.products) {
// if (product.contains("organic")) {
// organicProducts.add(product);
// }
// };
// return String.join("\n", organicProducts);
// }
// round2
// we use the getList() method from the parent class
// because we cannot access the private attribute 'products' from parent class
//OVERWRITTEN METHOD
public String getList() {
String allProducts = super.getList();
// split on linebreaks/newlines
String[] products = allProducts.split("\n");
for (String product: products) {
if (product.contains("organic")) {
organicProducts.add(product);
}
};
return String.join("\n", organicProducts);
}
}
Testing the class OrganicShoppingList...
Object created!
Adding to the list coffee
Adding to the list organiccocoa
Adding to the list organicporridge
Adding to the list organicchocolate
Adding to the list bread
Adding to the list organicbuttermilk
Adding to the list organicbutter
Adding to the list organicmilk
Adding to the list organiccarrots
Adding to the list organicpineapple
Adding to the list cereals
Adding to the list organicsauce
Adding to the list organicbun
Adding to the list organicpeas
Products in the Organic List:
organiccocoa
organicporridge
organicchocolate
organicbuttermilk
organicbutter
organicmilk
organiccarrots
organicpineapple
organicsauce
organicbun
organicpeas