吼吼吼吼吼吼吼吼吼吼吼吼吼吼吼吼

public class Address {

    // constants
    private static final String NEW_STREET = "New Street";
    private static final String NOWHERE_CITY = "Nowhere City";
    private static final String FINAL_POSTCODE = "Z9 9ZZ";

    // instance fields
    private int houseNumber;
    private String street;
    private String city;
    private String postcode;

    // constructors
    public Address() {
	houseNumber = 0;
	street = NEW_STREET;
	city = NOWHERE_CITY;
	postcode = FINAL_POSTCODE;
    }
    public Address(int h, String s, String c, String p) {
	houseNumber = h;
	street = s;
	city = c;
	postcode = p;
    }

    // set methods
    public void setPostcode(String p) {
	postcode = p;
    }

    // testing the postcode format
    public boolean isValidPostcode() {
	boolean flag = false;
	if (postcode.length()==6) {
	    flag = true;
	    String space = " ";
	    for (int i=0; i<5; i++) {
		boolean test = false;
		switch(i) {
		case 0:
		case 4:
		case 5:
		    char a = postcode.charAt(i);
		    test = Character.isUpperCase(a);
		    break;
		case 2:
		    String b = postcode.substring(i, i+1);
		    test = space.equals(b);
		    break;
		case 1:
		case 3:
		    char c = postcode.charAt(i);
		    test = Character.isDigit(c);
		    break;
		}
		flag = flag && test;
	    }
	}
	return(flag);
    }

    // deep copy
    public Address copy() {
	Address a = new Address();
	a.houseNumber = houseNumber;
	a.street = street;
	a.city = city;
	a.postcode = postcode;
	return(a);
    }

    // toString method
    public String toString() {
	return(houseNumber + " " + street + ", " + city + " " + postcode);
    }
}

Address.java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值