made better use of address class as mediator between CSV format of address and human-readable format of address. Fixed population of edit dialogue box for address too

This commit is contained in:
bobbert committed 2025-04-03 14:31:19 +01:00
1 parent 056a7a4e8d
commit 6de8d6620e
2 files changed
+128 -49

No files matched your search

+83 -39
View File
@@ -7,46 +7,82 @@ public class Address {
private String postcode;
public Address(String address) {
this.flat = ""; // Initialize flat to an empty string
this.addressLine1 = ""; // Initialize addressLine1 to an empty string
this.addressLine2 = ""; // Initialize addressLine2 to an empty string
this.postcode = ""; // Initialize postcode to an empty string
if (address == null || address.isEmpty()) {
this.flat = "";
this.addressLine1 = "";
this.addressLine2 = "";
this.postcode = "";
// Initialize fields to empty strings
this.flat = "";
this.addressLine1 = "";
this.addressLine2 = "";
this.postcode = "";
// Return early if address is empty
if (address == null || address.isEmpty() || "Unknown".equalsIgnoreCase(address)) {
return;
}
// First check if we have flat information (contains ////)
String[] flatParts = address.split("////", 2);
if (flatParts.length > 1) {
// We have flat information
this.flat = flatParts[0].trim();
address = flatParts[1].trim(); // Continue parsing with the rest
} else {
String[] parts = address.split("%%%%");
if (parts.length == 1) {
this.addressLine1 = parts[0];
} else if (parts.length == 2) {
this.addressLine1 = parts[0];
this.postcode = parts[1];
} else if (parts.length == 3) {
this.addressLine1 = parts[0];
this.addressLine2 = parts[1];
this.postcode = parts[2];
} else {
String[] flatParts = parts[0].split("////");
if (flatParts.length > 1) {
this.flat = flatParts[0];
this.addressLine1 = flatParts[1];
address = flatParts[0].trim();
}
// Now split by %%%% delimiter
String[] addressParts = address.split("%%%%");
// Parse based on the number of parts
if (addressParts.length >= 1) {
this.addressLine1 = addressParts[0].trim();
}
if (addressParts.length >= 2) {
// Check if the second part might be addressLine2 or postcode
if (addressParts.length == 2) {
// If only two parts and one looks like a postcode, it's probably the postcode
if (isLikelyPostcode(addressParts[1].trim())) {
this.postcode = addressParts[1].trim();
} else {
this.addressLine1 = parts[0];
this.addressLine2 = addressParts[1].trim();
}
if (parts.length > 1) this.postcode = parts[1];
if (parts.length > 2) this.addressLine2 = parts[2];
} else {
// If more than 2 parts, second is always addressLine2
this.addressLine2 = addressParts[1].trim();
}
}
if (addressParts.length >= 3) {
this.postcode = addressParts[addressParts.length - 1].trim();
}
}
// Helper method to check if a string looks like a UK postcode
private boolean isLikelyPostcode(String text) {
// Simple check - postcodes typically contain a space and are short
return text.contains(" ") && text.length() <= 10;
}
public String getHumanReadableFormat() {
StringBuilder sb = new StringBuilder();
if (!flat.isEmpty()) sb.append(flat).append(", ");
// Add flat with a dot separator if present
if (!flat.isEmpty()) {
sb.append(flat).append(". ");
}
// Add address line 1
sb.append(addressLine1);
if (!addressLine2.isEmpty()) sb.append(", ").append(addressLine2);
if (!postcode.isEmpty()) sb.append(", ").append(postcode);
// Add address line 2 with comma separator if present
if (!addressLine2.isEmpty()) {
sb.append(", ").append(addressLine2);
}
// Add postcode with comma separator if present
if (!postcode.isEmpty()) {
sb.append(", ").append(postcode);
}
return sb.toString();
}
@@ -62,15 +98,23 @@ public class Address {
public String getDisplayFormat() {
StringBuilder sb = new StringBuilder();
// If flat exists, add it as a separate line
// If flat exists, use dot notation
if (!flat.isEmpty()) {
sb.append(flat).append("\n\n");
sb.append(flat).append(". ");
}
// Add the rest in a comma-separated format
// Add address line 1
sb.append(addressLine1);
if (!addressLine2.isEmpty()) sb.append(", ").append(addressLine2);
if (!postcode.isEmpty()) sb.append(", ").append(postcode);
// Add address line 2 with comma if it exists
if (!addressLine2.isEmpty()) {
sb.append(", ").append(addressLine2);
}
// Add postcode with comma if it exists
if (!postcode.isEmpty()) {
sb.append(", ").append(postcode);
}
return sb.toString();
}
@@ -81,7 +125,7 @@ public class Address {
}
public void setFlat(String flat) {
this.flat = flat;
this.flat = flat != null ? flat.trim() : "";
}
public String getAddressLine1() {
@@ -89,7 +133,7 @@ public class Address {
}
public void setAddressLine1(String addressLine1) {
this.addressLine1 = addressLine1;
this.addressLine1 = addressLine1 != null ? addressLine1.trim() : "";
}
public String getAddressLine2() {
@@ -97,7 +141,7 @@ public class Address {
}
public void setAddressLine2(String addressLine2) {
this.addressLine2 = addressLine2;
this.addressLine2 = addressLine2 != null ? addressLine2.trim() : "";
}
public String getPostcode() {
@@ -105,6 +149,6 @@ public class Address {
}
public void setPostcode(String postcode) {
this.postcode = postcode;
this.postcode = postcode != null ? postcode.trim() : "";
}
}
+45 -10
View File
@@ -305,32 +305,67 @@ public class MainViewer {
}
private String showAddressDialog(String title, String initialValue) {
// Create the form fields
JTextField flatField = new JTextField();
JTextField addressLine1Field = new JTextField();
JTextField addressLine2Field = new JTextField();
JTextField postcodeField = new JTextField();
// Use the Address class to parse the address string
Address address = new Address(initialValue);
// Find the user and get the actual address object
String panelName = title.substring(5).toLowerCase(); // Extract "address" from "Edit address"
// Set field values from the Address object
flatField.setText(address.getFlat());
addressLine1Field.setText(address.getAddressLine1());
addressLine2Field.setText(address.getAddressLine2());
postcodeField.setText(address.getPostcode());
// Find the selected user by looking through users
User selectedUser = null;
for (User user : userGroup.getUsers()) {
// The initialValue is the display text from the radio button
if (user.getAddress().getDisplayFormat().equals(initialValue)) {
selectedUser = user;
break;
}
}
JPanel panel = new JPanel(new GridLayout(0, 1));
// If we found the user, use their actual address object
if (selectedUser != null) {
Address address = selectedUser.getAddress();
flatField.setText(address.getFlat());
addressLine1Field.setText(address.getAddressLine1());
addressLine2Field.setText(address.getAddressLine2());
postcodeField.setText(address.getPostcode());
} else {
// Fallback to parsing the display text if we can't find the user
Address address = new Address(initialValue);
flatField.setText(address.getFlat());
addressLine1Field.setText(address.getAddressLine1());
addressLine2Field.setText(address.getAddressLine2());
postcodeField.setText(address.getPostcode());
}
// Create the panel with properly spaced components
JPanel panel = new JPanel(new GridLayout(0, 1, 0, 5));
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
// Add flat field (optional)
panel.add(new JLabel("Flat (optional):"));
panel.add(flatField);
// Add address line 1 field (required)
panel.add(new JLabel("Address line 1:"));
panel.add(addressLine1Field);
// Add address line 2 field (optional)
panel.add(new JLabel("Address line 2 (optional):"));
panel.add(addressLine2Field);
// Add postcode field
panel.add(new JLabel("Postcode:"));
panel.add(postcodeField);
int result = JOptionPane.showConfirmDialog(null, panel, title, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
// Show the dialog
int result = JOptionPane.showConfirmDialog(null, panel, title,
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
// Get values from fields - handle empty fields gracefully
String newFlat = flatField.getText().trim();
String newAddressLine1 = addressLine1Field.getText().trim();
String newAddressLine2 = addressLine2Field.getText().trim();
@@ -338,7 +373,7 @@ public class MainViewer {
// Create an Address object with the new values
Address newAddress = new Address("");
newAddress.setFlat(newFlat);
newAddress.setFlat(newFlat); // Will handle null/empty values
newAddress.setAddressLine1(newAddressLine1);
newAddress.setAddressLine2(newAddressLine2);
newAddress.setPostcode(newPostcode);