Let's consider we are a company who sells our products for other companies. In the programme the actors are an Agency (agencies of our company, we have a few in the country, all are listed in AGENCIES in TestMain) and Client.
- Every
Agencyhas anAddress - Every
AddresscontentspostalCode - Every
Clienthas a postal code
Now, in class Distance, the first method takes a postal code of the agency and the postal code of the client, calculates the longitude and latitude for these postal codes (by using DICTIONARY_POSTAL_CODES created in TestMain) and computes the distance. The second method calculates the distance from the client to the furthest agency.
It works, but is it written in a professional way?
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Address {
private String postalCode;
private String pattern = "[0-9][0-9]-[0-9][0-9][0-9]"; //postal codes for Poland
Pattern p = Pattern.compile(pattern);
private void setPostalCode(String pc) {
Matcher m = p.matcher(pc);
if (m.find()) {
postalCode = m.group();
} else {
postalCode = "-----";
}
}
public Address(String pc) {
setPostalCode(pc);
}
public String getPostalCode() {
return postalCode;
}
}
public class Agency {
private Address address;
public Agency(String postalCode) {
address = new Address(postalCode);
}
public Address getAddress(){
return address;
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestMain {
// ****************************************************************************************
// The dictionary of postal codes and corresponding longitude/latitude
public static Map<String, PairLongLat> createDictionaryPostalCodes() {
Map<String, PairLongLat> dictionary = new HashMap<>();
dictionary.put("00-000", new PairLongLat(21.04383, 52.2315136));
dictionary.put("00-001", new PairLongLat(21.010353, 52.235382));
dictionary.put("00-002", new PairLongLat(21.0131092, 52.2363414));
dictionary.put("02-092", new PairLongLat(20.9790658, 52.183422));
dictionary.put("53-605", new PairLongLat(17.0199851, 51.1088447));
dictionary.put("61-696", new PairLongLat(16.9488458, 52.4316596));
return dictionary;
}
public final static Map<String, PairLongLat> DICTIONARY_POSTAL_CODES = createDictionaryPostalCodes();
// ****************************************************************************************
// List of our agencies
public static List<Agency> createAgenciesList() {
List<Agency> agencies = new ArrayList<>();
agencies.add(new Agency("02-092"));
agencies.add(new Agency("53-605"));
agencies.add(new Agency("61-696"));
return agencies;
}
public final static List<Agency> AGENCIES = createAgenciesList();
public static void main(String[] args) {
}
}
public class Distance {
/**
*
* @param pc1
* - postal code first
* @param pc2
* - postal code second
* @return the euclidean distance between 2 postal codes
*/
public double getDistance(String pc1, String pc2) {
PairLongLat coordinates1, coordinates2;
double long1, long2, lat1, lat2;
coordinates1 = TestMain.DICTIONARY_POSTAL_CODES.get(pc1);
coordinates2 = TestMain.DICTIONARY_POSTAL_CODES.get(pc2);
long1 = coordinates1.getLongitude();
long2 = coordinates2.getLongitude();
lat1 = coordinates1.getLatitude();
lat2 = coordinates2.getLatitude();
return Math.sqrt((long1 - long2) * (long1 - long2) + (lat1 - lat2) * (lat1 - lat2));
}
/**
*
* @param pcRegCli
* - postal code of client
* @return the maximum distance between (all distances between agency and
* client's postal code) and (client's postal code).
*/
public double getDistanceMax(String pcRegCli) {
double distanceMax = 0;
double tmp = 0;
for (Agency a : TestMain.AGENCIES) {
String pcAgency = a.getAddress().getPostalCode(); // Postal Code of
// EFL's agency
tmp = getDistance(pcRegCli, pcAgency);
if (tmp > distanceMax)
distanceMax = tmp;
}
return distanceMax;
}
}
/**
/* this class PairLongLat is used only in creating DICTIONARY_POSTAL_CODES
/* pairs are the values of HashMap
*/
public class PairLongLat {
private double longitude, latitude;
public PairLongLat(double lon, double lat) {
longitude = lon;
latitude = lat;
}
public double getLongitude() {
return longitude;
}
public double getLatitude() {
return latitude;
}
}
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class TestUnit {
@Test
public void testGetDistance() {
Distance da = new Distance();
assertEquals(0.033699763316084305, da.getDistance("00-000", "00-001"), 0);
assertEquals(da.getDistance("00-000", "00-001"), da.getDistance("00-001", "00-000"), 0);
assertEquals(4.09987243942609, da.getDistance("00-000", "61-696"), 0);
}
@Test
public void testGetDistanceMax() {
DistanceAfs da = new DistanceAfs();
assertEquals(4.177524774107656, da.getDistanceMax("00-000"), 0);
assertEquals(4.146338416680638, da.getDistanceMax("00-001"), 0);
assertEquals(4.149251605592471, da.getDistanceMax("00-002"), 0);
}
}