All Downloads are FREE. Search and download functionalities are using the official Maven repository.

z11.vietnam.VietnamRegions Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
package z11.vietnam;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class VietnamRegions {
    
    private static VietnamRegions instance = null;
    
    public static VietnamRegions getInstance(){
        if (instance == null) {
            instance = new VietnamRegions();
        }
        return instance;
    }

    private JSONArray tinhJosnData = null;
    
    private VietnamRegions() {

        try {
            String fileContent = z11.F_ile.getUtf8ResourceFile("vietnamese_regions.json");
            tinhJosnData = new JSONArray(fileContent);
        } catch (IOException | JSONException ex) {
            tinhJosnData = null;
            z11.L_ogger.print(z11.S_ystem.getStackTree(ex));
        }
    }

    public HashMap getTinhs() {
        HashMap result = new HashMap<>();
        for (int tinh_i = 0; tinh_i < tinhJosnData.length(); tinh_i++) {
            JSONObject tinhObj = (JSONObject) tinhJosnData.get(tinh_i);
            Tinh t = new Tinh(tinhObj);
            result.put(t.ma_tinh, t);
        }

        return result;
    }

    public ArrayList getTinhArray() {
        ArrayList result = new ArrayList<>();
        for (int tinh_i = 0; tinh_i < tinhJosnData.length(); tinh_i++) {
            JSONObject tinhObj = (JSONObject) tinhJosnData.get(tinh_i);
            Tinh t = new Tinh(tinhObj);
            result.add(t);
        }

        return result;
    }

    public Tinh getTinh(String tinh_code) {
        HashMap objs = getTinhs();
        return objs.get(tinh_code);
    }

    public HashMap getHuyens(String tinh_code) throws Exception {
        JSONArray array = getHuyenObjs(tinh_code);
        HashMap result = new HashMap<>();
        for (int i = 0; i < array.length(); i++) {
            JSONObject obj = (JSONObject) array.get(i);
            Huyen t = new Huyen(obj);
            result.put(t.ma_huyen, t);
        }

        return result;
    }

    public ArrayList getHuyenArray(String tinh_code) throws Exception {
        JSONArray array = getHuyenObjs(tinh_code);
        ArrayList result = new ArrayList<>();
        for (int i = 0; i < array.length(); i++) {
            JSONObject obj = (JSONObject) array.get(i);
            Huyen t = new Huyen(obj);
            result.add(t);
        }
        return result;
    }

    public Huyen getHuyen(String tinh_code, String huyen_code) throws Exception {
        HashMap objs = getHuyens(tinh_code);
        return objs.get(huyen_code);
    }

    public HashMap getXas(String tinh_code, String huyen_code) throws Exception {
        JSONArray array = getXaObjs(tinh_code, huyen_code);
        HashMap result = new HashMap<>();
        for (int i = 0; i < array.length(); i++) {
            JSONObject obj = (JSONObject) array.get(i);
            Xa t = new Xa(obj);
            result.put(t.ma_xa, t);
        }

        return result;
    }

    public ArrayList getXaArray(String tinh_code, String huyen_code) throws Exception {
        JSONArray array = getXaObjs(tinh_code, huyen_code);
        ArrayList result = new ArrayList<>();
        for (int i = 0; i < array.length(); i++) {
            JSONObject obj = (JSONObject) array.get(i);
            Xa t = new Xa(obj);
            result.add(t);
        }

        return result;
    }

    public Xa getXa(String tinh_code, String huyen_code, String xa_code) throws Exception {
        HashMap objs = getXas(tinh_code, huyen_code);
        return objs.get(xa_code);
    }

    public HashMap getTos(String tinh_code, String huyen_code, String xa_code) throws Exception {
        JSONArray array = getToObjs(tinh_code, huyen_code, xa_code);
        HashMap result = new HashMap<>();
        for (int i = 0; i < array.length(); i++) {
            JSONObject obj = (JSONObject) array.get(i);
            To t = new To(obj);
            result.put(t.ma_to, t);
        }

        return result;
    }

    public ArrayList getToArray(String tinh_code, String huyen_code, String xa_code) throws Exception {
        JSONArray array = getToObjs(tinh_code, huyen_code, xa_code);
        ArrayList result = new ArrayList<>();
        for (int i = 0; i < array.length(); i++) {
            JSONObject obj = (JSONObject) array.get(i);
            To t = new To(obj);
            result.add(t);
        }

        return result;
    }

    public To getTo(String tinh_code, String huyen_code, String xa_code, String to_code) throws Exception {
        HashMap objs = getTos(tinh_code, huyen_code, xa_code);
        return objs.get(to_code);
    }

    public JSONArray getTinhObjs() {
        return tinhJosnData;
    }
    
    public JSONObject getTinhObj(String tinh_code) throws Exception {
        JSONArray parent = getTinhObjs();
        for (int tinh_i = 0; tinh_i < parent.length(); tinh_i++) {
            JSONObject obj = (JSONObject) parent.get(tinh_i);
            if (obj.getString("pc").equals(tinh_code)) {
                return obj;
            }
        }
        throw new Exception("Not found:" + tinh_code);
    }
    
    public JSONArray getHuyenObjs(String tinh_code) throws Exception {
        return getTinhObj(tinh_code).getJSONArray("hs");
        
    }
    
    public JSONObject getHuyenObj(String tinh_code, String huyen_code) throws Exception {
        JSONArray parent = getHuyenObjs(tinh_code);
        for (int tinh_i = 0; tinh_i < parent.length(); tinh_i++) {
            JSONObject obj = (JSONObject) parent.get(tinh_i);
            if (obj.getString("dc").equals(huyen_code)) {
                return obj;
            }
        }
        throw new Exception("Not found:" + huyen_code);
    }
    
    public JSONArray getXaObjs(String tinh_code, String huyen_code) throws Exception {
        return getHuyenObj(tinh_code, huyen_code).getJSONArray("xs");
    }
    
    public JSONObject getXaObj(String tinh_code, String huyen_code, String xa_code) throws Exception {
        JSONArray parent = getXaObjs(tinh_code, huyen_code);
        for (int tinh_i = 0; tinh_i < parent.length(); tinh_i++) {
            JSONObject obj = (JSONObject) parent.get(tinh_i);
            if (obj.getString("xc").equals(xa_code)) {
                return obj;
            }
        }
        throw new Exception("Not found:" + xa_code);
    }
    
    public JSONArray getToObjs(String tinh_code, String huyen_code, String xa_code) throws Exception {
        return getXaObj(tinh_code, huyen_code, xa_code).getJSONArray("ts");
    }
    
    public JSONObject getToObj(String tinh_code, String huyen_code, String xa_code, String to_code) throws Exception {
        JSONArray parent = getToObjs(tinh_code, huyen_code, xa_code);
        for (int tinh_i = 0; tinh_i < parent.length(); tinh_i++) {
            JSONObject obj = (JSONObject) parent.get(tinh_i);
            if (obj.getString("tc").equals(to_code)) {
                return obj;
            }
        }
        throw new Exception("Not found:" + to_code);
    }
    
    
    public VietnamLocation genData(String ten_tinh) throws Exception {
        VietnamLocation vietnamLocation = new VietnamLocation();
        for (int tinh_i = 0; tinh_i < tinhJosnData.length(); tinh_i++) {
            JSONObject tinhObj = (JSONObject)tinhJosnData.get(tinh_i);
            if (z11.S_tring.removeAccent(tinhObj.getString("pn").trim()).equals(
                    z11.S_tring.removeAccent(ten_tinh.trim()))) {
                vietnamLocation.tinh = new Tinh(tinhObj);
                
                JSONArray huyens = (JSONArray)tinhObj.get("hs");
                JSONObject huyenObj = (JSONObject) huyens.get(z11.M_ath.getrd() % huyens.length());
                vietnamLocation.huyen = new Huyen(huyenObj);
                
                
                JSONArray xas = (JSONArray) huyenObj.get("xs");
                JSONObject xaObj = (JSONObject) xas.get(z11.M_ath.getrd() % xas.length());
                vietnamLocation.xa = new Xa(xaObj);
                
                JSONArray tos = (JSONArray) xaObj.get("ts");
                JSONObject toObj = (JSONObject) tos.get(z11.M_ath.getrd() % tos.length());
                vietnamLocation.to = new To(toObj);
                
                return vietnamLocation;
                
            }
        }
        z11.L_ogger.print("not found tinh: " + ten_tinh);
        throw new Exception("not found tinh!");
        
    }

    public VietnamLocation genData() throws Exception {
        JSONObject tinhObj = (JSONObject) tinhJosnData.get(z11.M_ath.getrd() % tinhJosnData.length());
        return genData(tinhObj.getString("pn").trim());
    }
    
    public static void main(String [] asdsd) throws Exception {
        VietnamLocation vietnamLocation = VietnamRegions.getInstance().genData("Hải Dương");
        System.out.println(vietnamLocation.xa.ten_xa);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy