Answer to Question #287910 in Java | JSP | JSF for Mengal

Question #287910

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.


1
Expert's answer
2022-01-16T09:03:12-0500



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);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog