GUI Java String转换为Address

时间:2014-12-15 12:47:26

标签: java string user-interface

我正在尝试通过GUI输入添加客户。用户输入包含下面显示的部分的地址,这将成为defaultColAddress。有一个问题,因为defaultColAddress必须是Address,但构成这个的组件是String,我试图将这些转换为Address但不能解决问题的行:

Address defaultColAddress = Address.parseAddress(addrLine1, addrLine2, city, postcode);

我在Address.parseAddress上收到错误我知道地址没有解析但我也尝试了所有变种,例如toAddress等。

任何帮助都将不胜感激。

 private void saveItem()
    {
        if(this.isSaveable()){
            double costPerKg = Integer.parseInt(txtDefaultCost.getText());
            String addrLine1 = txtAddressLine1.getText();
            String addrLine2 = txtAddressLine2.getText();
            String city = txtCity.getText();
            String postcode = txtPostcode.getText();
            Person itemToAdd = null;
            Address defaultColAddress = Address.parseAddress(addrLine1, addrLine2, city, postcode);
            //Address defaultColAddress = Address.parseAddress(txtAddressLine1.getText(),txtAddressLine2.getText(),txtCity.getText(), txtPostcode.getText());

                itemToAdd = new Customer(txtForename.getText(),txtSurname.getText(), costPerKg, defaultColAddress);
                peopleList.addCustomer((Customer) itemToAdd);
        }
    }

    private Boolean isSaveable()
    {
        Boolean blnValid = false;
        try
        {
            Integer intQty = Integer.parseInt(txtDefaultCost.getText());
            if (txtForename.getText().length() > 0 && intQty >= 0)
            {
                blnValid = true;
            }
        } catch (NumberFormatException ex)
        {
            JOptionPane.showMessageDialog(this, "The provided data is invalid", "Error reading data", JOptionPane.ERROR_MESSAGE);
        }
        return blnValid;
    }

这是请求的整个Address类

public class Address实现ISubject,Serializable {

private String addressLine1;
private String addressLine2;
private String city;
private String postcode;

private ISubject subjectDelegate;

/**
 * Default constructor initialises all attributes to the string "UNKNOWN".
 * Required for serialisation
 */
public Address(){
    this.subjectDelegate = new ISubjectImpl();
    this.addressLine1 = "UNKNOWN";
    this.addressLine2 = "UNKNOWN";
    this.city = "UNKNOWN";
    this.postcode = "UNKNOWN";
}

/**
 * Constructor that initialises the object with the provided address details
 * @param addrLine1 - String being line 1 of the address
 * @param addrLine2 - String being line 2 of the address
 * @param city - String being the city in which the address is located
 * @param postcode - String being the postcode / zip code of the address
 */
public Address(String addrLine1, String addrLine2, String city, String postcode){
    this();
    this.addressLine1 = addrLine1;
    this.addressLine2 = addrLine2;
    this.city = city;
    this.postcode = postcode;
}

/**
 * Accessor method to retrieve the first line of the address
 * @return - String being the first line of the postal address
 */
public String getAddressLine1() {
    return addressLine1;
}

/**
 * Accessor method to set the first line of the address
 * @param addressLine1 - String being the first line of the postal address
 */
public void setAddressLine1(String addressLine1) {
    this.addressLine1 = addressLine1;
    this.subjectDelegate.notifyObservers();
}

/**
 * Accessor method to retrieve the second line of the address
 * @return - String being the second line of the postal address
 */
public String getAddressLine2() {
    return addressLine2;
}

/**
 * Accessor method to set the second line of the address
 * @param addressLine2 - String being the second line of the postal address
 */
public void setAddressLine2(String addressLine2) {
    this.addressLine2 = addressLine2;
    this.subjectDelegate.notifyObservers();
}

/**
 * Accessor method to retrieve the city from the address
 * @return - String being the city in which the address is located
 */
public String getCity() {
    return city;
}

/**
 * Accessor method to set the city in which the address is located
 * @param city - String being the city in which the address is located
 */
public void setCity(String city) {
    this.city = city;
    this.subjectDelegate.notifyObservers();
}

/**
 * Accessor method to retrieve the postcode of the address
 * @return - String being the postcode of the address
 */
public String getPostcode() {
    return postcode;
}

/**
 * Accessor method to set the postcode of the address
 * @param postcode - String being the postcode of the address
 */
public void setPostcode(String postcode) {
    this.postcode = postcode;
    this.subjectDelegate.notifyObservers();
}

@Override
public Boolean registerObserver(IObserver o) {
    return this.subjectDelegate.registerObserver(o);
}

@Override
public Boolean removeObserver(IObserver o) {
    return this.subjectDelegate.removeObserver(o);
}

@Override
public void notifyObservers() {
    this.subjectDelegate.notifyObservers();
}

@Override
public String toString() {
    //Using a StringBuilder it is more memory efficient than concatenating strings with "+"
    //This is because strings are "immutable objects" so building up a string using "+"
    //means allocating memory for a whole new object each time we add to the string.
    //StringBuilder uses a "mutable array of characters" allocating memory for 
    //only one object.
    StringBuilder resultBuilder = new StringBuilder();
    resultBuilder.append(this.addressLine1);
    resultBuilder.append(", ");
    if(null != this.addressLine2 && !this.addressLine2.isEmpty()){
        resultBuilder.append(this.addressLine2);
        resultBuilder.append(", ");
    }
    if(null != this.city && !this.city.isEmpty()){
        resultBuilder.append(this.city);
        resultBuilder.append(", ");
    }
    if(null != this.postcode && !this.postcode.isEmpty()){
        resultBuilder.append(this.postcode);
    }
    return resultBuilder.toString();
}

}

1 个答案:

答案 0 :(得分:0)

我认为你想要的是

        Address defaultColAddress = new Address(addrLine1, addrLine2, city, postcode);