2021-09-30

实现多对多的映射

class Country{
    private String cid ;
    private String name ;
    private Province provinces[] ;
    private City cities[] ;
    private CountyTown countyTowns[] ;
    public Country(String cid,String name) {
        this.cid = cid ;
        this.name = name ;
    }
    public void setProvinces(Province provinces[]) {
        this.provinces = provinces ;
    }
    public Province [] getProvinces() {
        return this.provinces ;
    }
    public void setCities( City cities[] ) {
        this.cities = cities ;
    }
    public City [] getCities() {
        return this.cities ;
    }
    public void setCountyTonwns(CountyTown countyTowns[]) {
        this.countyTowns = countyTowns ;
    }
    public CountyTown [] getCountyTowns() {
        return this.countyTowns ;
    }
    public String getInfo() {
        return "国家编号: " + this.cid + ",名称:" + this.name ;
    }
}
class Province{
    private String pid ;
    private String name ;
    private Country country ;
    private City cities[] ;
    private CountyTown countyTowns[] ;
    public Province(String pid,String name) {
        this.pid = pid;
        this.name = name;
        
    }
    public void setCountry(Country country) {
        this.country = country ;
    }
    public Country getCountry() {
        return this.country ;
    }
    public void setCities( City cities[] ) {
        this.cities = cities ;
    }
    public City [] getCities() {
        return this.cities ;
    }
    public void setCountyTonwns(CountyTown countyTowns[]) {
        this.countyTowns = countyTowns ;
    }
    public CountyTown [] getCountyTowns() {
        return this.countyTowns ;
    }
    public String getInfo() {
        return "省份编号: " + this.pid + ",名称 :" + this.name ;
    }
    
}
class City{
    private int cid ;
    private String name ;
    private Country country ;
    private Province province ;
    private CountyTown countyTowns[] ;
    public Object getCountyTowns;
    public City(int cid,String name) {
        this.cid = cid;
        this.name = name;
    }
    public void setCountry(Country country) {
        this.country = country ;
    }
    public Country getCountry() {
        return this.country ;
    }
    public void setProvince(Province province ) {
        this.province = province ;
    }
    public Province getProvince() {
        return this.province;
    }
    public void setCountyTonwns(CountyTown countyTowns[]) {
        this.countyTowns = countyTowns ;
    }
    public CountyTown [] getCountyTowns() {
        return this.countyTowns ;
    }
    public String getInfo() {
        return "城市编号: " + this.cid + ",名称 :" + this.name; 
    }
}
class CountyTown{
    private int cid;
    private String name;
    private Country country ;
    private Province province ;
    private City city ;
    public CountyTown(int cid,String name) {
        this.cid = cid;
        this.name = name;
    }
    public void setCountry(Country country) {
        this.country = country ;
    }
    public Country getCountry() {
        return this.country ;
    }
    public void setProvince(Province province ) {
        this.province = province ;
    }
    public Province getProvince() {
        return this.province;
    }
    public void setCity(City city) {
        this.city = city ;    
    }
    public City getCity() {
        return this.city ;
    }
    public String getInfo() {
        return "县城编号: " + this.cid + ",名称: " + this.name ;
    }
}
public class TestCPC {

    public static void main(String[] args){
        Country c = new Country("01","中国") ;
        Province p1 = new Province("001","安徽省") ;
        Province p2 = new Province("002","江苏省") ;
        p1.setCountry(c);
        p2.setCountry(c);
        c.setProvinces(new Province[] {p1,p2});
        
        
        City c1 = new City(1001 ,"宿州市") ;
        City c2 = new City(1002 ,"合肥市") ;
        City c3 = new City(1003 ,"芜湖市") ;
        City c4 = new City(1004 ,"滁州市") ;
        c1.setProvince(p1);
        c2.setProvince(p1);
        c3.setProvince(p1);
        c4.setProvince(p1);
        p1.setCities(new City[] {c1,c2,c3,c4});
        c.setCities(new City[] {c1,c2,c3,c4});
        
        CountyTown ct1 = new CountyTown(10001,"灵璧县") ;
        CountyTown ct2 = new CountyTown(10002,"萧县") ;
        CountyTown ct3 = new CountyTown(10003,"砀山县") ;
        CountyTown ct4 = new CountyTown(10004,"泗县") ;
        CountyTown ct5 = new CountyTown(10005,"埇桥区") ;
        ct1.setCity(c1);
        ct2.setCity(c1);
        ct3.setCity(c1);
        ct4.setCity(c1);
        ct5.setCity(c1);
        c1.setCountyTonwns(new CountyTown[] {ct1,ct2,ct3,ct4,ct5});
        p1.setCountyTonwns(new CountyTown[] {ct1,ct2,ct3,ct4,ct5});
        c.setCountyTonwns(new CountyTown[] {ct1,ct2,ct3,ct4,ct5});
        
        p2.setCities(null);
        
        
        
        
//        printCS(c);
//        
//        printSC(p1);
//        printCX(c1);
//        printCSCX(c);
//        
//        printCPC(p1);
        
        printCS(count(c));
    
    
    }
    
    
    //根据国家找到国家对应得省份
    public static void printCS(Country c) {
        System.out.println("--" + c.getInfo());
        if(c.getProvinces() != null) {
            for(int x = 0 ;x < c.getProvinces().length;x ++) {
                System.out.println("\t--" + c.getProvinces()[x].getInfo());
            }
        }
    }
    //根据省份找到对应得城市输出
    public static void printSC(Province p) {
        System.out.println("--" + p.getInfo());
        if(p.getCities() != null) {
            for(int x = 0 ;x < p.getCities().length;x ++) {
                System.out.println("\t--" + p.getCities()[x].getInfo());
            }
        }
    }
    //根据城市找到对应得县城输出
    public static void printCX(City c) {
        System.out.println("--" + c.getInfo());
        if(c.getCountyTowns() != null) {
            for(int x = 0 ;x < c.getCountyTowns().length ;x ++) {
                System.out.println("\t--" + c.getCountyTowns()[x].getInfo());
            }
        }
    }
    //根据省份找到对应得城市县城输出
    public static void printCPC(Province p) {
        System.out.println("--" + p.getInfo());
        if(p.getCities() != null) {
            for(int x = 0 ;x < p.getCities().length ;x ++) {
                System.out.println("\t--" + p.getCities()[x].getInfo());
                if(p.getCities()[x].getCountyTowns() != null) {
                    for(int y = 0 ;y < p.getCities()[x].getCountyTowns().length ; y ++) {
                        System.out.println("\t\t--" + p.getCities()[x].getCountyTowns()[y].getInfo());
                    }
                }
            }
        }
    }
    //根据国家找到对应得省份城市县城输出
    public static void printCSCX(Country c) {
        System.out.println("--" + c.getInfo());
        if(c.getProvinces() != null) {
            for(int x = 0 ;x< c.getProvinces().length ;x ++) {
                System.out.println("\t--" + c.getProvinces()[x].getInfo());
                if(c.getProvinces()[x].getCities() != null) {
                    for(int y = 0 ;y < c.getProvinces()[x].getCities().length ;y ++) {
                        System.out.println("\t\t--" +c.getProvinces()[x].getCities()[y].getInfo());
                        if(c.getProvinces()[x].getCities()[y].getCountyTowns != null) {
                            for(int z = 0 ;z < c.getProvinces()[x].getCities()[y].getCountyTowns().length ;z ++) {
                                System.out.println("\t\t\t--" + c.getProvinces()[x].getCities()[y].getCountyTowns()[z].getInfo());
                            }
                        }
                    }
                }
            }            
        }
    }

}

测试程序匹配关系太复杂太繁琐了,有哪位大神可以告诉我怎么处理一下

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值