140 lines
3.9 KiB
Java
140 lines
3.9 KiB
Java
/*
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
* To change this template file, choose Tools | Templates
|
|
* and open the template in the editor.
|
|
*/
|
|
package problem2;
|
|
|
|
import java.util.Arrays;
|
|
import javax.naming.SizeLimitExceededException;
|
|
|
|
/**
|
|
*
|
|
* @author ar114
|
|
*/
|
|
public class MyContacts implements Scramble
|
|
{
|
|
|
|
private Contact[] contacts;
|
|
private int size;
|
|
public final static int CAPACITY = 2;
|
|
|
|
/**
|
|
* Adds a new contact.
|
|
*
|
|
* @param c the contact to be added
|
|
* @throws SizeLimitExceededException if we exceed the MyContacts.CAPACITY
|
|
*/
|
|
public void add(Contact c) throws SizeLimitExceededException
|
|
{
|
|
if (this.size != CAPACITY) {
|
|
contacts[size] = c;
|
|
} else {
|
|
throw new SizeLimitExceededException("Capacity Exceeded!");
|
|
}
|
|
size++;
|
|
}
|
|
|
|
public MyContacts()
|
|
{
|
|
contacts = new Contact[CAPACITY];
|
|
}
|
|
|
|
@Override
|
|
public String toString()
|
|
{
|
|
String s = "";
|
|
|
|
for (int i = 0; i < this.size; ++i)
|
|
{
|
|
s += contacts[i].toString() + "\n";
|
|
}
|
|
|
|
return s;
|
|
}
|
|
|
|
/**
|
|
* Encrypts by adding the encyptionKey(String) parameter in the front of every
|
|
* property of every contact in the array .
|
|
*
|
|
* @param encyptionKey the object to be added as String to the front of
|
|
* every property of the Contact.
|
|
* @return the encrypted array as Object type
|
|
*/
|
|
@Override
|
|
public Object encrypt(Object encyptionKey)
|
|
{
|
|
for (int i = 0; i < size; ++i)
|
|
{
|
|
|
|
contacts[i].setName(encyptionKey.toString() + contacts[i].getName());
|
|
contacts[i].setAddress((encyptionKey.toString() + new String(contacts[i].getAddress())).toCharArray()
|
|
);
|
|
contacts[i].setPhone(encyptionKey.toString() + contacts[i].getPhone());
|
|
}
|
|
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Decrypts by removing the encyptionKey parameter from the front of every
|
|
* property of every contact in the array.
|
|
*
|
|
* @param decryptionKey the object(String) to be removed from the front of
|
|
* every property of the Contact in the array.
|
|
* @return the decrypted array as Object type
|
|
*/
|
|
@Override
|
|
public Object decrypt(Object decryptionKey)
|
|
{
|
|
String name, address, phone;
|
|
String decrypt = ((String) decryptionKey);
|
|
for (int i = 0; i < size; ++i) {
|
|
// get
|
|
name = contacts[i].getName();
|
|
address = new String(contacts[i].getAddress());
|
|
phone = contacts[i].getPhone();
|
|
// decrypt
|
|
name = (name.split(decrypt))[1];
|
|
address = (address.split(decrypt))[1];
|
|
phone = (phone.split(decrypt))[1];
|
|
// set
|
|
contacts[i].setName(name);
|
|
contacts[i].setAddress(address.toCharArray());
|
|
contacts[i].setPhone(phone);
|
|
}
|
|
|
|
return this;
|
|
}
|
|
|
|
public static void main(String[] args)
|
|
{
|
|
MyContacts mc = new MyContacts();
|
|
char[] address1 =
|
|
{
|
|
'S', 'P', 'A', 'R', 'T', 'A'
|
|
};
|
|
char[] address2 =
|
|
{
|
|
'A', 'T', 'H', 'E', 'N', 'S'
|
|
};
|
|
try
|
|
{
|
|
mc.add(new Contact("Leonidas", address1, "888-123-4577"));
|
|
mc.add(new Contact("Pericles", address2, "422-511-1232"));
|
|
mc.add(new Contact("Athena", address2, "422-224-1151"));
|
|
}
|
|
catch (SizeLimitExceededException e)
|
|
{
|
|
System.err.println("%%%%%%% " + e);
|
|
}
|
|
System.out.println("Original Contacts\n" + mc);
|
|
|
|
mc.encrypt("abc");
|
|
System.out.println("Encrypted Contacts\n" + mc);
|
|
|
|
Object o = mc.decrypt("abc");
|
|
System.out.println("Decrypted Contacts Using the returned o\n" + o);
|
|
}
|
|
}
|