Discuss the use of generics in your own words.
Consider a Billings system. The organization pays the water bill, k electric bill, gas bill of the customer. The payment of bills follows the queue structure for submission. You are required to build a generic bill payment class for the submission of bills.
Bill payment class should build the collection of bills of generic type. The following should be included in the class.
1) A default constructor which sets up an empty dictionary collection.
2) A add method should take a parameterized object which should be the element of the collection with a key.
3) A delete method should remove object.
4) Override the delete method that parameterized key value to delete the object. 5) Search method will search element with object or key.
import java.util.HashMap;
import java.util.Map;
class BillPayment<K, V> {
private final Map<K, V> collection;
public BillPayment() {
collection = new HashMap<>();
}
public void add(V element, K key) {
collection.put(key, element);
}
public void delete(V element) {
collection.remove(element);
}
public void delete(K key, V element) {
collection.remove(key, element);
}
public V search(Object keyOrValue) {
return collection.get(keyOrValue);
}
}
Comments
Leave a comment