Write tests and code for each of the following requirements
Write tests and code for each of the following requirements, in order. The words in bold indicate message names. Whenever a requirement says the user can \"ask whether...\", the expected answer is boolean. Whenever a requirement speaks of a \"particular\" item, then that item will be an argument to the method. 1. The user can create an Association between a key and a value. The Association responds to a toString() message by returning a string of the form: [key]:[value] 2. The user can ask an Association whether its keyContains a particular substring. 3. The user can ask an Association whether its valueContains a particular substring. 4. The user can ask an Association whether it contains a particular substring in either its key or value. 5. The user can ask an Association whether it hasKey that is a particular string. 6. The user can create an AssociationList that contains no Associations. 7. The user can add an existing Association to an AssociationList. 8. The user can add a new Association to an AssociationList by sending a key and a value. 9. The user can ask an AssociationList whether it contains a particular Association. 10. The user can ask an AssociationList whether it containsKey for a particular key. 11. The user can ask an AssociationList to lookup a particular key, returning a ArrayList of the Associations it contains that have that key. 12. The user can ask an AssociationList to findAll matches for a particular string, returning a ArrayList containing all Associations that have that string anywhere in their key or value. 13. The user can ask an AssociationList to remove a particular Association. 14. The user can ask an AssociationList to update every Association having a particular key by appending a particular string to its value. You may not add so-called access methods to the Association class. That is, you may not add \"get\" or \"set\" methods, whose only behavior is to return a field\'s value or assign a field\'s value, respectively. I need these files for it: Part Two o AssociationTest.java o Association.java o AssociationListTest.java o AssociationList.java
Solution
Association.java
public class Association{
String key;
String value;
public Association(String key, String value){
this.key = key;
this.value = value;
}
public String toString(){
return \"[\"+this.key+\"]:[\"+this.value+\"]\";
}
public boolean keyContains(String substring){
return this.key.contains(substring);
}
public boolean valueContains(String substring){
return this.value.contains(substring);
}
public boolean keyOrValueContains(String substring){
return this.key.contains(substring) || this.value.contains(substring);
}
public boolean hasKey(String key){
return this.key.equals(key);
}
}
AssociationList.java
import java.util.ArrayList;
public class AssociationList{
ArrayList<Association> associationList;
public AssociationList(){
associationList = new ArrayList<Association>();
}
public void addAssociation(Association association){
associationList.add(association);
}
public void addAssociation(String key, String value){
associationList.add(new Association(key, value));
}
public boolean containsAssociation(Association association){
return associationList.contains(association);
}
public boolean containsKey(String key){
for (Association association: this.associationList){
if (association.key.equals(key)){
return true;
}
}
return false;
}
public ArrayList associationsWithKey(String key){
ArrayList<Association> associations = new ArrayList<Association>();
for (Association association: this.associationList){
if (association.key.equals(key)){
associations.add(association);
}
}
return associations;
}
public ArrayList<Association> associationsKeyOrValueContainsKey(String key){
ArrayList<Association> associations = new ArrayList<Association>();
for (Association association: this.associationList){
if (association.key.contains(key)){
associations.add(association);
}
}
return associations;
}
public void removeAssociation(Association association){
if (this.associationList.contains(association)){
this.associationList.remove(association);
}
}
public void updateAssociations(String key, String append){
for (Association association: this.associationList){
if (association.key.equals(key)){
association.value = association.value + append;
}
}
}
}
AssociationTest.java
public class AssociationTest{
public static void main(String[] args) {
Association association = new Association(\"key\", \"value\");
System.out.println(association.keyContains(\"key\"));
System.out.println(association.keyOrValueContains(\"val\"));
System.out.println(association.hasKey(\"key\"));
}
}
AssociationListTest.java
public class AssociationListTest{
public static void main(String[] args) {
Association association = new Association(\"key\", \"value\");
System.out.println(association.keyContains(\"key\"));
System.out.println(association.keyOrValueContains(\"val\"));
System.out.println(association.hasKey(\"key\"));
AssociationList associationList = new AssociationList();
associationList.addAssociation(association);
associationList.addAssociation(\"key2\", \"value2\");
System.out.println(associationList.containsAssociation(association));
System.out.println(associationList.containsKey(\"key3\"));
}
}


