package test3; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; /** * A class that manages information regarding phone calls. * A call log records information about the time when a call * occurred and the phone number associated with the call. * *

* A call log has a name and a list of call log entries. * The call log and its name form an aggregation. The call * log and its list of entries form a composition. * *

* A call log ensures that no two log entries have dates that * are equal. * */ public class CallLog { /** * The name of this call log. DO NOT CHANGE THE NAME OR TYPE OF THIS FIELD. */ private String name; /** * The list of entries. DO NOT CHANGE THE NAME OR TYPE OF THIS FIELD. */ private List log; public CallLog() { try { Test3DUtils.initialize(this); } catch (Exception x) { System.out.println("something went wrong"); throw new NullPointerException(); } } /** * Initializes this call log to have the given name and * zero entries. * * @param name the non-null name of this call log */ public CallLog(String name) { this.name = name; this.log = new ArrayList<>(); } /** * Initializes this call log to have the given name and a size * equal to 1. The call log contains one CallLogEntry * whose date is equal to callDate and whose * phone number is equal to number. * * @param name the non-null name of this call log * @param callDate the non-null date at which the call occurred * @param number the non-null phone number of the call */ public CallLog(String name, Date callDate, PhoneNumber number) { Date d = new Date(callDate.getTime()); this.name = name; this.log = new ArrayList<>(); this.log.add(new CallLogEntry(d, number)); } /** * Return the name of this call log. * * @return the name of this call log */ public String getName() { return this.name; } /** * Set the name of this call log. * * @param name a non-null name for this call log */ public void setName(String name) { this.name = name; } /** * Returns the number of entries in this call log. * * @return the number of entries in this call log */ public int size() { return this.log.size(); } /** * Returns a set of all of the phone numbers in this call log. * There are no guarantees on the order of the phone numbers in * the returned set. * * @return a set of all of the phone numbers in this call log */ public Set getPhoneNumbers() { Set result = new HashSet<>(); for (CallLogEntry e : this.log) { result.add(e.getNumber()); } return result; } /** * Adds an entry to this call log. Throws a SameDateException * if the call log already contains an entry equal to * entry.getDate() * * @param entry an entry to add to this call log * @throws SameDateException if the call log already contains an * entry equal to entry.getDate() */ public void add(CallLogEntry entry) { for (CallLogEntry e : this.log) { if (entry.getDate().equals(e.getDate())) { throw new SameDateException(); } } CallLogEntry e = new CallLogEntry(new Date(entry.getDate().getTime()), entry.getNumber()); this.log.add(e); } /** * Returns a sorted list of all of the entries in this call log * that occurred after the given date. The entries in the list * are new independent copies of the entries in this call log. * * @param after a non-null date * @return a sorted list of all of the entries in this call log * that occurred after the given date */ public List callsAfter(Date after) { List result = new ArrayList<>(); for (CallLogEntry e : this.log) { Date d = e.getDate(); if (d.compareTo(after) > 0) { result.add(new CallLogEntry(new Date(d.getTime()), e.getNumber())); } } Collections.sort(result); return result; } }