Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Create a Customer java class so that you can have the flexibility of having a

ID: 3596199 • Letter: 1

Question

1. Create a  Customer java class so that you can have the flexibility of having a generic type attribute "t" in your objects.   You may retain your previous customer class.

     In this new Customer class, add a new data field  " T t; ", add at least one constructor with T as an input, add a getter/setter for the attribute "t".

2.   Create another class (called CustomerComparator.java) to compare 2 customer using a criteria of your choice (e.g., credit score, transaction list size, or even using generic attribute T; but please only choose one).

3.   In a main method, create a java Map object (called it "customeCounts"), then create a dozen customer objects to populate the customerCounts object.   You may choose your own criteria for the Integer key according to your design of the Customer class.     Please use java comments to explain your choice.

Explanation / Answer

Implemented the code as per the requirement. Added comments also.

code:

-------------

package com.bhanu.chandra;

import java.util.HashMap;

import java.util.Map;

public class CustomerOperations {

public static void main(String[] args) {

Map<Integer, Customer> customerCounts = new HashMap<Integer, Customer>(); //Creating map object

//Creating customer object

// As it is having generic type we can use the any data type of our choice for the first parameter of constructor, which is generic

Customer<Integer> c1 = new Customer(1, 43);   

Customer<Integer> c2 = new Customer(5, 32);

Customer<Double> c3 = new Customer(5.6, 43);

Customer<Float> c4 = new Customer(2.3, 43);

Customer<String> c5 = new Customer("cust", 43);

Customer<Character> c6 = new Customer('A', 43);

Customer<Long> c7 = new Customer(444, 43);

Customer<Short> c8 = new Customer(7, 43);

Customer<Byte> c9 = new Customer(9, 43);

Customer<Integer> c10 = new Customer(1234, 56);

Customer<Integer> c11 = new Customer(12345, 43);

Customer<Integer> c12 = new Customer(123456, 43);

//Adding the customer objects to map

customerCounts.put(1001, c1);

customerCounts.put(1002, c2);

customerCounts.put(1003, c3);

customerCounts.put(1004, c4);

customerCounts.put(1005, c5);

customerCounts.put(1006, c6);

customerCounts.put(1007, c7);

customerCounts.put(1008, c8);

customerCounts.put(1009, c9);

customerCounts.put(1010, c10);

customerCounts.put(1011, c11);

customerCounts.put(1012, c12);

//Printing result for customer comparison

System.out.println(CustomerComparator.compareCust(c1, c2));

System.out.println(CustomerComparator.compareCust(c1, c10));

System.out.println(CustomerComparator.compareCust(c1, c11));

}

}

//Customer class with generic data type

class Customer<T>{

private T t;

int credit_score;

Customer(T t_c, int cre_sco){ //Constructor with generic data type

t = t_c;

credit_score = cre_sco;

}

//Getter, setter methods

public T getT() {

return t;

}

public void setT(T t) {

this.t = t;

}

}

//Customer compare class

class CustomerComparator {

public static int compareCust(Customer c1, Customer c2) {

//Comparing the customer with the criteria credit score

//If the first customer credit score is lesser than second customer => return -1

//If the first customer credit score is grater than second customer => return 1

//If the credit score are equal => return -1

if(c1.credit_score<c2.credit_score) {

return -1;

}

else if(c1.credit_score>c2.credit_score) {

return 1;

}

else {

return 0;

}

}

}