For this exercise, you will build a list of CreditCards, then use the Comparable
ID: 3777212 • Letter: F
Question
For this exercise, you will build a list of CreditCards, then use the Comparable and Comparator interfaces to sort the list in different ways.
To start, implement the following UML:
Notes:
CreditCard. This class will model the key date elements of a credit card as shown. Note that in addition to the getter methods, there is a toString() method. This method will print our the last name, first name, PAN, and expiration date in a nice, format. Use the String.format() method to print this data in fixed fields.
Use the SimpleDateFormat class to convert a string to a java Date, and vice versa. You can look up this technique on the internet. The data format is "MM/yy"
CreditPayments. This class will serve as a container for credit cards and will be used to print the cards in different order.
The add() methods adds a CreditCard object to the array list.
The reset() methods clears the array list.
The printByXX methods will:
use the Collections.sort() method to order the arraylist prior to printing
use an foreach loop to go through the ArrayList and print each CreditCard. Because you overrode the toString() method in the Credit Card class, you can just pass in the credit card instance to the System.out.println() method, and the toString() method will be called automatically for you.
return a sorted ArrayList of CreditCards according to the method name
The printByPAN will print the credit cards in the list using the natural ordering as implied by the Comparable interface. For us, the natural ordering will be by PAN.
The printByName will print the credit cards in the list by last name, then first name. The Collections.sort() method is overloaded, and there's one that accepts a Comparator. You'll pass in an instance of your NameCompare class to this sort method so that your list will be sorted as indicated by the NameCompare class.
The printByDate will print the credit cards in the list byDate. Again, use the overloaded sort() method that takes a Comparator, and pass in an instance of your DateCompare class.
NameCompare. This class implements the Comparator interface as shown and will be used to order the credit cards by last name, then first name. To do this correctly in the compare() method, you will need to first check if the last names are the same, and if so, then return the comparison of the first names. If not, then return the comparison of the last names.
DateCompare. This class implements the Comparator interface as shown and will be used to order the credit cards by the expiration date. This is accomplished by taking advantage of the fact that the date object has a compareTo() method. You can simply return the result of the Date's compareTo method.
The output after calling the printByXXX methods should look as close a possible to the following:
The above data was generated via the provided JUnit test class. You can use this class to test your solution.
Attachments
CreditPayments Hist: ArrayList treset0 void +add(CreditCard) void +printByPANO: ArrayList 0: ArrayList> Comparator CreditCard> Date Compare interface>> Comparable CreditCardExplanation / Answer
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
class CreditCard implements Comparable<CreditCard>{
private String lastName;
private String firstName;
private String PAN;
private Date expDate;
public CreditCard(String lastName, String firstName, String PAN, Date expDate) {
this.lastName = lastName;
this.firstName = firstName;
this.PAN = PAN;
this.expDate = expDate;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the pAN
*/
public String getPAN() {
return PAN;
}
/**
* @param pAN the pAN to set
*/
public void setPAN(String pAN) {
PAN = pAN;
}
/**
* @return the expDate
*/
public Date getExpDate() {
return expDate;
}
/**
* @param expDate the expDate to set
*/
public void setExpDate(Date expDate) {
this.expDate = expDate;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
SimpleDateFormat sd=new SimpleDateFormat("MM\yy");
return "" + lastName + " " + firstName + " " + PAN + " " + sd.format(expDate)+ "";
}
@Override
public int compareTo(CreditCard card) {
return this.PAN.compareTo(card.PAN);
}
}
class NameCompare implements Comparator<CreditCard>{
@Override
public int compare(CreditCard obj1, CreditCard obj2) {
if(obj1.getLastName().equalsIgnoreCase(obj2.getLastName())){
return obj1.getFirstName().compareTo(obj2.getFirstName());
}
return obj1.getLastName().compareTo(obj2.getLastName());
}
}
class DateCompare implements Comparator<CreditCard>{
@Override
public int compare(CreditCard obj1, CreditCard obj2) {
return obj1.getExpDate().compareTo(obj2.getExpDate());
}
}
class CreditPayments{
private List<CreditCard>list;
public void reset(){
list=new ArrayList<>();
}
public void add(CreditCard card){
if(list==null){
list=new ArrayList<>();
}
list.add(card);
}
public List<CreditCard>printByPAN(){
List<CreditCard>sortedList=new ArrayList<>(list);
Collections.sort(sortedList);
System.out.println("Credit Cards by PAN:");
System.out.println("Last Name First Name PAN Exp Date");
System.out.println(" --------- ---------- ----------- --------");
for (CreditCard creditCard : sortedList) {
System.out.println(creditCard);
}
System.out.println("------------------------------------------------------------");
return sortedList;
}
public List<CreditCard>printByName(){
List<CreditCard>sortedList=new ArrayList<>(list);
Collections.sort(sortedList,new NameCompare());
System.out.println("Credit Cards by Name:");
System.out.println("Last Name First Name PAN Exp Date");
System.out.println(" --------- ---------- ----------- --------");
for (CreditCard creditCard : sortedList) {
System.out.println(creditCard);
}
System.out.println("------------------------------------------------------------");
return sortedList;
}
public List<CreditCard>printByDate(){
List<CreditCard>sortedList=new ArrayList<>(list);
Collections.sort(sortedList,new DateCompare());
System.out.println("Credit Cards by Expiration Date:");
System.out.println("Last Name First Name PAN Exp Date");
System.out.println(" --------- ---------- ----------- --------");
for (CreditCard creditCard : sortedList) {
System.out.println(creditCard);
}
System.out.println("------------------------------------------------------------");
return sortedList;
}
}
public class CreditCardDemo {
public static void main(String[] args) {
try {
CreditCard card1=new CreditCard("Kishore", "Jay", "4111345234", new SimpleDateFormat("MM/yy").parse("09/18"));
CreditCard card2=new CreditCard("Kishore", "Abhishek", "1111345234", new SimpleDateFormat("MM/yy").parse("07/18"));
CreditCard card3=new CreditCard("Muray", "Andy", "5111345234", new SimpleDateFormat("MM/yy").parse("08/18"));
CreditCard card4=new CreditCard("Annie", "Chris", "3111345234", new SimpleDateFormat("MM/yy").parse("06/18"));
CreditPayments cp=new CreditPayments();
cp.add(card1);
cp.add(card2);
cp.add(card3);
cp.add(card4);
cp.printByPAN();
cp.printByName();
cp.printByDate();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
-----------------------------------------------------------------output---------------------------------------------------------------------
Credit Cards by PAN:
Last Name First Name PAN Exp Date
--------- ---------- ----------- --------
Kishore Abhishek 1111345234 078
Annie Chris 3111345234 068
Kishore Jay 4111345234 098
Muray Andy 5111345234 088
------------------------------------------------------------
Credit Cards by Name:
Last Name First Name PAN Exp Date
--------- ---------- ----------- --------
Annie Chris 3111345234 068
Kishore Abhishek 1111345234 078
Kishore Jay 4111345234 098
Muray Andy 5111345234 088
------------------------------------------------------------
Credit Cards by Expiration Date:
Last Name First Name PAN Exp Date
--------- ---------- ----------- --------
Annie Chris 3111345234 068
Kishore Abhishek 1111345234 078
Muray Andy 5111345234 088
Kishore Jay 4111345234 098
------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.